SettingsViewController.m 16 KB

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