SettingsViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //
  2. // SettingsViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 10/8/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "CustomLabel.h"
  11. #import "CustomButton.h"
  12. #import "CustomCheckBox.h"
  13. #import "UIButton+WebCache.h"
  14. #import "CustomTableView.h"
  15. #import "WebBrowseViewController.h"
  16. #import "PwdPopupView.h"
  17. #import "ImageUtil.h"
  18. #import "SettingsViewController.h"
  19. #import "AdminAuthPopupView.h"
  20. #import "DeleteAccountPopupView.h"
  21. #import "ChangeNamePopupView.h"
  22. #import "ConfirmPasswdPopupView.h"
  23. #import "ChangeEmailPopupView.h"
  24. #define kiAccountCellHeight 473
  25. #define kiNotiCellHeight 137
  26. #define kiServiceCellHeight 143
  27. #define kiVersionCellHeight 103
  28. #define kiDeleteCellHeight 82
  29. //#define kiPasswdCellHeight 135
  30. @implementation SettingsAccountTableViewCell
  31. - (void)awakeFromNib {
  32. }
  33. - (void)didMoveToSuperview {
  34. // [_lblChangeNickname setUnderLine:_lblChangeNickname.text];
  35. // [_lblChangePassword setUnderLine:_lblChangePassword.text];
  36. // [_lblChangeEmail setUnderLine:_lblChangeEmail.text];
  37. [_lblChangePassword setHidden:YES];
  38. [_lblChangeEmail setHidden:YES];
  39. [_lblEmailWaiting setHidden:YES];
  40. _contraintEmailChangeBottom.constant = ![JDFacade facade].tmpEmailId || [[JDFacade facade].tmpEmailId isEmptyString] ? 18 : 45;
  41. _lblEmailWaiting.hidden = ![JDFacade facade].tmpEmailId || [[JDFacade facade].tmpEmailId isEmptyString];
  42. [self layoutIfNeeded];
  43. }
  44. @end
  45. @implementation SettingsNotificationTableViewCell
  46. @end
  47. @implementation SettingsServiceTableViewCell
  48. - (void)didMoveToSuperview {
  49. }
  50. @end
  51. @implementation SettingsVersionTableViewCell
  52. @end
  53. @implementation SettingsDeleteTableViewCell
  54. @end
  55. //@implementation SettingsPasswdTableViewCell
  56. //
  57. //@end
  58. @interface SettingsViewController () <UITableViewDataSource, UITableViewDelegate, ImageUtilDelegate> {
  59. CustomCheckBox *_chkAutoLogin, *_chkHomeModeChange, *_chkHomeEmptyChange;
  60. CustomButton *_btnProfile;
  61. BOOL _isNotFirstLoading;
  62. ImageUtil *_imageUtil;
  63. }
  64. @end
  65. #pragma mark - Class Definition
  66. @implementation SettingsViewController
  67. - (void)viewDidLoad {
  68. [super viewDidLoad];
  69. // Do any additional setup after loading the view.
  70. [self initUI];
  71. [self prepareViewDidLoad];
  72. }
  73. - (void)viewWillAppear:(BOOL)animated {
  74. [super viewWillAppear:animated];
  75. }
  76. - (void)initUI {
  77. //set tableview option
  78. [self initTableViewAsDefaultStyle:_tableView];
  79. }
  80. - (void)prepareViewDidLoad {
  81. }
  82. #pragma mark - Main Logic
  83. - (void)requestUpdatePushSetting:(NSDictionary *)parameter {
  84. //parameters
  85. NSString *path = [NSString stringWithFormat:API_POST_PUSH_SETTINGS];
  86. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  87. NSString *msg = nil;
  88. if ([parameter hasKey:@"push_type_mode_chg_yn"]) {
  89. BOOL isNotifyHomeModeChange = [[parameter valueForKey:@"push_type_mode_chg_yn"] boolValue];
  90. if ([[JDFacade facade].loginUser.pushTypeModeChgYn boolValue] != isNotifyHomeModeChange) {
  91. msg = isNotifyHomeModeChange ? @"지금부터 홈모드가 바뀔 때\n푸시 알림을 받습니다" : @"홈모드 변경 알림을 해제합니다";
  92. [JDFacade facade].loginUser.pushTypeModeChgYn = [parameter valueForKey:@"push_type_mode_chg_yn"];
  93. }
  94. } else if ([parameter hasKey:@"push_type_prsnc_chg_yn"]) {//와이파이,
  95. BOOL isNotifyExecutingRules = [[parameter valueForKey:@"push_type_prsnc_chg_yn"] boolValue];
  96. if ([[JDFacade facade].loginUser.pushTypePrsncChgYn boolValue] != isNotifyExecutingRules) {
  97. msg = isNotifyExecutingRules ? @"지금부터 집이 빌 때\n푸시 알림을 받습니다" : @"빈 집 알림을 해제합니다";
  98. [JDFacade facade].loginUser.pushTypePrsncChgYn = [parameter valueForKey:@"push_type_prsnc_chg_yn"];
  99. }
  100. }
  101. [[JDFacade facade] toast:msg];
  102. } failure:^(id errorObject) {
  103. JDErrorModel *error = (JDErrorModel *)errorObject;
  104. [[JDFacade facade] alert:error.errorMessage];
  105. }];
  106. }
  107. - (void)requestUpdateProfileImage:(UIImage *)profileImage {
  108. //parameters
  109. NSDictionary *parameter = @{@"image_file": profileImage};
  110. NSString *path = [NSString stringWithFormat:API_POST_MEMBER_UPDATE_IMAGE];
  111. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  112. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  113. return;
  114. }
  115. LoginModel *response = (LoginModel *) responseObject;
  116. if (response) {//API 성공 ,
  117. [_btnProfile sd_setBackgroundImageWithURL:[NSURL URLWithString:response.profileImage] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  118. [[JDFacade facade] toast:@"프로필 이미지를 변경했습니다"];
  119. }
  120. } failure:^(id errorObject) {
  121. JDErrorModel *error = (JDErrorModel *)errorObject;
  122. [[JDFacade facade] alert:error.errorMessage];
  123. }];
  124. }
  125. #pragma mark - UITableView DataSource & Delegate
  126. #pragma mark - UITableView DataSource & Delegate
  127. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  128. return 5;
  129. }
  130. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  131. return 1;
  132. }
  133. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  134. CGFloat height = 0.0f;
  135. if (indexPath.section == 0) {
  136. // height = ![JDFacade facade].tmpEmailId || [[JDFacade facade].tmpEmailId isEmptyString] ? 331-27 : 331;
  137. height = kiAccountCellHeight;
  138. } else if (indexPath.section == 1) {//notification view
  139. // height = [JDFacade facade].loginUser.isHomehubOnline ? 317-56 : 317;
  140. height = kiNotiCellHeight;
  141. } else if (indexPath.section == 2) {
  142. height = kiServiceCellHeight;
  143. } else if (indexPath.section == 3) {
  144. height = kiVersionCellHeight;
  145. } else if (indexPath.section == 4) {
  146. height = kiDeleteCellHeight;
  147. }
  148. // else if (indexPath.section == 5) {
  149. // height = kiPasswdCellHeight;
  150. // }
  151. return height;
  152. }
  153. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  154. UITableViewCell *cell = nil;//[super tableView:tableView cellForRowAtIndexPath:indexPath];
  155. if (indexPath.section == 0) {//account cell
  156. SettingsAccountTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"AccountCellIdentifier"];
  157. tcell.lblNickname.text = [JDFacade facade].loginUser.nickname;
  158. tcell.lblEmail.text = [JDFacade facade].loginUser.emailId;
  159. if (!_btnProfile) {
  160. _btnProfile = tcell.btnProfile;
  161. }
  162. [_btnProfile sd_setBackgroundImageWithURL:[NSURL URLWithString:[JDFacade facade].loginUser.profileImage] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  163. //set checkbox
  164. if (!_chkAutoLogin) {
  165. _chkAutoLogin = tcell.chkAutoLogin;
  166. _chkAutoLogin.checked = [[[JDFacade facade] objectForKeyFromUserDefaults:USDEF_APP_AUTO_LOGIN] boolValue];
  167. }
  168. //set checkbox action
  169. if (![_chkAutoLogin actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  170. [_chkAutoLogin addTarget:self action:@selector(chkAutoLoginTouched:) forControlEvents:UIControlEventTouchUpInside];
  171. }
  172. //set label action
  173. if (!tcell.lblChangeNickname.touchHandler) {
  174. [tcell.lblChangeNickname addTouchEventHandler:^(id label) {
  175. [self lblChangeNicknameTouched];
  176. }];
  177. }
  178. //set label action
  179. if (!tcell.lblChangePassword.touchHandler) {
  180. [tcell.lblChangePassword addTouchEventHandler:^(id label) {
  181. [self lblChangePasswordTouched];
  182. }];
  183. }
  184. //set email change action
  185. if (!tcell.lblChangeEmail.touchHandler) {
  186. [tcell.lblChangeEmail addTouchEventHandler:^(id label) {
  187. [self lblChangeEmailTouched];
  188. }];
  189. }
  190. //set Phone Number change action
  191. if (![tcell.btnChangeNum actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target
  192. [tcell.btnChangeNum addTarget:self action:@selector(btnChangeNumTouched:) forControlEvents:UIControlEventTouchUpInside];
  193. }
  194. if (![tcell.btnLogout actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target
  195. [tcell.btnLogout addTarget:self action:@selector(btnLogoutTouched:) forControlEvents:UIControlEventTouchUpInside];
  196. }
  197. if (![tcell.btnProfile actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target
  198. [tcell.btnProfile addTarget:self action:@selector(btnProfileTouched:) forControlEvents:UIControlEventTouchUpInside];
  199. }
  200. if (![tcell.btnProfileSetting actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  201. [tcell.btnProfileSetting addTarget:self action:@selector(btnProfileTouched:) forControlEvents:UIControlEventTouchUpInside];
  202. }
  203. cell = tcell;
  204. } else if (indexPath.section == 1) {//notification cell
  205. SettingsNotificationTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"NotificationCellIdentifier"];
  206. if ([JDFacade facade].loginUser.level < 90) {
  207. tcell.viewThingsSet.hidden = YES;
  208. } else {
  209. tcell.viewThingsSet.hidden = NO;
  210. }
  211. if (!_chkHomeModeChange) {
  212. _chkHomeModeChange = tcell.chkHomeModeChange;
  213. }
  214. // if (!_chkHomeEmptyChange) {
  215. // _chkHomeEmptyChange = tcell.chkHomeEmptyChange;
  216. // }
  217. if (!_isNotFirstLoading) {
  218. _isNotFirstLoading = YES;
  219. _chkHomeModeChange.checked = [[JDFacade facade].loginUser.pushTypeModeChgYn boolValue];
  220. // _chkHomeEmptyChange.checked = [[JDFacade facade].loginUser.pushTypePrsncChgYn boolValue];
  221. }
  222. if (![tcell.chkHomeModeChange actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  223. [tcell.chkHomeModeChange addTarget:self action:@selector(chkHomeModeChangeTouched:) forControlEvents:UIControlEventTouchUpInside];
  224. }
  225. if (![tcell.btnThingsSet actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  226. [tcell.btnThingsSet addTarget:self action:@selector(btnThingsSetTouched:) forControlEvents:UIControlEventTouchUpInside];
  227. }
  228. // if (![tcell.chkHomeEmptyChange actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  229. // [tcell.chkHomeEmptyChange addTarget:self action:@selector(chkHomeEmptyChangeTouched:) forControlEvents:UIControlEventTouchUpInside];
  230. // }
  231. // tcell.homehubInfoView.hidden = [JDFacade facade].loginUser.isHomehubOnline;
  232. // if (tcell.homehubInfoView.hidden) {
  233. // tcell.constraintHomehubInfoViewHeight.constant = 0;
  234. // } else {
  235. // _chkHomeModeChange.enabled = NO;
  236. // _chkHomeEmptyChange.enabled = NO;
  237. // }
  238. cell = tcell;
  239. } else if (indexPath.section == 2) {//service info cell
  240. SettingsServiceTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"ServiceCellIdentifier"];
  241. if (![tcell.btnService actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  242. [tcell.btnService addTarget:self action:@selector(btnServiceTouched:) forControlEvents:UIControlEventTouchUpInside];
  243. }
  244. if (![tcell.btnPolicy actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  245. [tcell.btnPolicy addTarget:self action:@selector(btnPolicyTouched:) forControlEvents:UIControlEventTouchUpInside];
  246. }
  247. cell = tcell;
  248. } else if (indexPath.section == 3) {//version info cell
  249. SettingsVersionTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"VersionCellIdentifier"];
  250. tcell.lblVersion.text = [NSString stringWithFormat:@"현재 버전 : %@", [CommonUtil applicationVersion]];
  251. cell = tcell;
  252. } else if (indexPath.section == 4) {//Delete cell
  253. SettingsDeleteTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"DeleteCellIdentifier"];
  254. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  255. [tcell.btnDelete addTarget:self action:@selector(btnDeleteTouched:) forControlEvents:UIControlEventTouchUpInside];
  256. }
  257. cell = tcell;
  258. }
  259. // else if (indexPath.section == 5) {//Passwd info cell
  260. // SettingsPasswdTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"PasswdCellIdentifier"];
  261. //
  262. // if (![tcell.btnPassword actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  263. // [tcell.btnPassword addTarget:self action:@selector(btnPasswordTouched:) forControlEvents:UIControlEventTouchUpInside];
  264. // }
  265. //
  266. // cell = tcell;
  267. // }
  268. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  269. return cell;
  270. }
  271. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  272. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  273. }
  274. #pragma mark - ImageUtil Delegate
  275. - (void)didFinishPickingImage:(UIImage *)image {
  276. [self requestUpdateProfileImage:image];
  277. }
  278. #pragma mark - UI Events
  279. - (void)lblChangeNicknameTouched {
  280. ChangeNamePopupView *popup = [[ChangeNamePopupView alloc] initFromNib];
  281. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  282. if (buttonIndex == 0) {//이름 변경 시,
  283. [_tableView reloadData];
  284. }
  285. }];
  286. }
  287. - (void)lblChangePasswordTouched {
  288. ConfirmPasswdPopupView *popup = [[ConfirmPasswdPopupView alloc] initFromNib];
  289. [popup show];
  290. }
  291. - (void)lblChangeEmailTouched {
  292. ChangeEmailPopupView *popup = [[ChangeEmailPopupView alloc] initFromNib];
  293. [popup show];
  294. }
  295. - (void)btnProfileTouched:(id)sender {
  296. if (!_imageUtil) {
  297. _imageUtil = [[ImageUtil alloc] init];
  298. _imageUtil.delegate = self;
  299. }
  300. [_imageUtil prepareImagePicker:self];
  301. }
  302. - (void)chkAutoLoginTouched:(id)sender {
  303. [[JDFacade facade] storeObjectToUserDefaults:@(_chkAutoLogin.checked) forKey:USDEF_APP_AUTO_LOGIN];
  304. if (_chkAutoLogin.checked) {//자동로그인 설정 시, 인증 토큰 저장
  305. [[JDFacade facade] storeObjectToKeychain:[JDFacade facade].loginUser.authToken forKey:USDEF_SESSION_AUTOTOKEN];
  306. } else {;
  307. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
  308. }
  309. }
  310. - (void)btnLogoutTouched:(id)sender {
  311. [[JDFacade facade] logout];
  312. }
  313. - (void)btnChangeNumTouched:(id)sender {
  314. NSLog(@"휴대폰 번호 변경");
  315. }
  316. //NoticationcationCell's action
  317. - (void)chkHomeModeChangeTouched:(id)sender {
  318. NSDictionary *parameter = @{@"push_type_mode_chg_yn" : _chkHomeModeChange.checked ? ksYES : ksNO};
  319. [self requestUpdatePushSetting:parameter];
  320. }
  321. //- (void)chkHomeEmptyChangeTouched:(id)sender {
  322. //
  323. // NSDictionary *parameter = @{@"push_type_prsnc_chg_yn" : _chkHomeEmptyChange.checked ? ksYES : ksNO};
  324. // [self requestUpdatePushSetting:parameter];
  325. //}
  326. - (void)btnThingsSetTouched:(id)sender{
  327. NSLog(@"홈모드별 동작할 장치 설정");
  328. }
  329. - (void)btnServiceTouched:(id)sender {
  330. NSString *URLString = [NSString stringWithFormat:@"%@%@", kWebLinkServer, URL_PATH_TERMS];
  331. [[JDFacade facade] loadURLExternalBrowser:URLString];
  332. }
  333. - (void)btnPolicyTouched:(id)sender {
  334. NSString *URLString = [NSString stringWithFormat:@"%@%@", kWebLinkServer, URL_PATH_POLICY];
  335. [[JDFacade facade] loadURLExternalBrowser:URLString];
  336. }
  337. //service passwd cell's action
  338. - (void)btnPasswordTouched:(id)sender {// 셋탑 설정
  339. AdminAuthPopupView *popup = [[AdminAuthPopupView alloc] initFromNib];
  340. [popup show];
  341. }
  342. - (void)btnDeleteTouched:(id)sender { // 계정 삭제
  343. DeleteAccountPopupView *popup = [[DeleteAccountPopupView alloc]initFromNib];
  344. [popup show];
  345. }
  346. - (IBAction)btnCloseTouched:(id)sender {
  347. [self dismissViewControllerAnimated:YES completion:nil];
  348. }
  349. #pragma mark - MemoryWarning
  350. - (void)didReceiveMemoryWarning
  351. {
  352. [super didReceiveMemoryWarning];
  353. // Dispose of any resources that can be recreated.
  354. }
  355. @end