SettingsViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "SettingsViewController.h"
  17. #import "PwdPopupView.h"
  18. #import "ChangeNamePopupView.h"
  19. #import "ImageUtil.h"
  20. @implementation SettingsAccountTableViewCell
  21. - (void)awakeFromNib {
  22. }
  23. - (void)didMoveToSuperview {
  24. [_lblChangeNickname setUnderLine:_lblChangeNickname.text];
  25. [_lblChangePassword setUnderLine:_lblChangePassword.text];
  26. }
  27. @end
  28. @implementation SettingsNotificationTableViewCell
  29. @end
  30. @implementation SettingsServiceTableViewCell
  31. - (void)didMoveToSuperview {
  32. [_lblService setUnderLine:_lblService.text];
  33. [_lblPolicy setUnderLine:_lblPolicy.text];
  34. }
  35. @end
  36. @implementation SettingsVersionTableViewCell
  37. @end
  38. @interface SettingsViewController () <UITableViewDataSource, UITableViewDelegate, ImageUtilDelegate> {
  39. CustomCheckBox *_chkAutoLogin, *_chkHomeModeChange, *_chkHomeEmptyChange;
  40. CustomButton *_btnProfile;
  41. BOOL _isNotFirstLoading;
  42. ImageUtil *_imageUtil;
  43. }
  44. @end
  45. #pragma mark - Class Definition
  46. @implementation SettingsViewController
  47. - (void)viewDidLoad {
  48. [super viewDidLoad];
  49. // Do any additional setup after loading the view.
  50. [self initUI];
  51. [self prepareViewDidLoad];
  52. }
  53. - (void)viewWillAppear:(BOOL)animated {
  54. [super viewWillAppear:animated];
  55. }
  56. - (void)initUI {
  57. //set tableview option
  58. // _tableView.dataSource = self;
  59. // _tableView.delegate = self;
  60. //
  61. // _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  62. // _tableView.backgroundColor = [UIColor clearColor];
  63. //
  64. // _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  65. [self initTableViewAsDefaultStyle:_tableView];
  66. //Localization
  67. // _lblAccount.text = NSLocalizedString(@"계정 정보", @"계정 정보");
  68. // _lblMobile.text = NSLocalizedString(@"단말 관리", @"단말 관리");
  69. // _lblHome.text = NSLocalizedString(@"홈 관리", @"홈 관리");
  70. // _lblService.text = NSLocalizedString(@"서비스 안내", @"서비스 안내");
  71. // _lblApp.text = NSLocalizedString(@"앱 정보", @"앱 정보");
  72. }
  73. - (void)prepareViewDidLoad {
  74. }
  75. #pragma mark - Main Logic
  76. - (void)requestUpdatePushSetting:(NSDictionary *)parameter {
  77. //parameters
  78. NSString *path = [NSString stringWithFormat:API_POST_PUSH_SETTINGS];
  79. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  80. NSString *msg = nil;
  81. if ([parameter hasKey:@"push_type_mode_chg_yn"]) {
  82. BOOL isNotifyHomeModeChange = [[parameter valueForKey:@"push_type_mode_chg_yn"] boolValue];
  83. if ([[JDFacade facade].loginUser.pushTypeModeChgYn boolValue] != isNotifyHomeModeChange) {
  84. msg = isNotifyHomeModeChange ? @"지금부터 홈모드가 바뀔 때\n푸시 알림을 받습니다" : @"홈모드 변경 알림을 해제합니다";
  85. }
  86. } else if ([parameter hasKey:@"push_type_rule_exc_yn"]) {
  87. BOOL isNotifyExecutingRules = [[parameter valueForKey:@"push_type_rule_exc_yn"] boolValue];
  88. if ([[JDFacade facade].loginUser.pushTypeModeChgYn boolValue] != isNotifyExecutingRules) {
  89. msg = isNotifyExecutingRules ? @"지금부터 집이 빌 때\n푸시 알림을 받습니다" : @"빈 집 알림을 해제합니다";
  90. }
  91. }
  92. [[JDFacade facade] toast:msg];
  93. } failure:^(id errorObject) {
  94. JDErrorModel *error = (JDErrorModel *)errorObject;
  95. [[JDFacade facade] alert:error.errorMessage];
  96. }];
  97. }
  98. - (void)requestUpdateProfileImage:(UIImage *)profileImage {
  99. //parameters
  100. NSDictionary *parameter = @{@"image_file": profileImage};
  101. NSString *path = [NSString stringWithFormat:API_POST_MEMBER_UPDATE_IMAGE];
  102. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  103. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  104. return;
  105. }
  106. LoginModel *response = (LoginModel *) responseObject;
  107. if (response) {//API 성공 ,
  108. [_btnProfile sd_setImageWithURL:[NSURL URLWithString:response.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  109. [[JDFacade facade] toast:@"프로필 이미지를 변경했습니다"];
  110. }
  111. } failure:^(id errorObject) {
  112. JDErrorModel *error = (JDErrorModel *)errorObject;
  113. [[JDFacade facade] alert:error.errorMessage];
  114. }];
  115. }
  116. #pragma mark - UITableView DataSource & Delegate
  117. #pragma mark - UITableView DataSource & Delegate
  118. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  119. return 4;
  120. }
  121. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  122. return 1;
  123. }
  124. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  125. CGFloat height = 44.0f;
  126. if (indexPath.section == 0) {
  127. height = 200.0f;
  128. } else if (indexPath.section == 1) {
  129. height = 200.0f;
  130. } else if (indexPath.section == 2) {
  131. height = 100.0f;
  132. } else if (indexPath.section == 3) {
  133. height = 100.0f;
  134. }
  135. return height;
  136. }
  137. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  138. UITableViewCell *cell = nil;//[super tableView:tableView cellForRowAtIndexPath:indexPath];
  139. if (indexPath.section == 0) {//account cell
  140. SettingsAccountTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"AccountCellIdentifier"];
  141. tcell.lblNickname.text = [JDFacade facade].loginUser.nickname;
  142. tcell.lblEmail.text = [JDFacade facade].loginUser.emailId;
  143. if (!_btnProfile) {
  144. _btnProfile = tcell.btnProfile;
  145. }
  146. //set checkbox
  147. if (!_chkAutoLogin) {
  148. _chkAutoLogin = tcell.chkAutoLogin;
  149. _chkAutoLogin.checked = [[[JDFacade facade] objectForKeyFromUserDefaults:USDEF_APP_AUTO_LOGIN] boolValue];
  150. }
  151. //set checkbox action
  152. if (![_chkAutoLogin actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  153. [_chkAutoLogin addTarget:self action:@selector(chkAutoLoginTouched:) forControlEvents:UIControlEventTouchUpInside];
  154. }
  155. //set label action
  156. if (!tcell.lblChangeNickname.touchHandler) {
  157. [tcell.lblChangeNickname addTouchEventHandler:^(id label) {
  158. [self lblChangeNicknameTouched];
  159. }];
  160. }
  161. //set label action
  162. if (!tcell.lblChangePassword.touchHandler) {
  163. [tcell.lblChangePassword addTouchEventHandler:^(id label) {
  164. [self lblChangePasswordTouched];
  165. }];
  166. }
  167. if (![tcell.btnLogout actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target
  168. [tcell.btnLogout addTarget:self action:@selector(btnLogoutTouched:) forControlEvents:UIControlEventTouchUpInside];
  169. }
  170. if (![tcell.btnProfile actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target
  171. [tcell.btnProfile addTarget:self action:@selector(btnProfileTouched:) forControlEvents:UIControlEventTouchUpInside];
  172. }
  173. cell = tcell;
  174. } else if (indexPath.section == 1) {//notification cell
  175. SettingsNotificationTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"NotificationCellIdentifier"];
  176. if (!_chkHomeModeChange) {
  177. _chkHomeModeChange = tcell.chkHomeModeChange;
  178. }
  179. if (!_chkHomeEmptyChange) {
  180. _chkHomeEmptyChange = tcell.chkHomeEmptyChange;
  181. }
  182. if (!_isNotFirstLoading) {
  183. _isNotFirstLoading = YES;
  184. _chkHomeModeChange.checked = [[JDFacade facade].loginUser.pushTypeModeChgYn boolValue];
  185. _chkHomeEmptyChange.checked = [[JDFacade facade].loginUser.pushTypeRuleExcYn boolValue];
  186. }
  187. if (![tcell.chkHomeModeChange actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  188. [tcell.chkHomeModeChange addTarget:self action:@selector(chkHomeModeChangeTouched:) forControlEvents:UIControlEventTouchUpInside];
  189. }
  190. if (![tcell.chkHomeEmptyChange actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  191. [tcell.chkHomeEmptyChange addTarget:self action:@selector(chkHomeEmptyChangeTouched:) forControlEvents:UIControlEventTouchUpInside];
  192. }
  193. cell = tcell;
  194. } else if (indexPath.section == 2) {//service info cell
  195. SettingsServiceTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"ServiceCellIdentifier"];
  196. if (!tcell.lblService.touchHandler) {
  197. [tcell.lblService addTouchEventHandler:^(id label) {
  198. [self lblServiceTouched];
  199. }];
  200. }
  201. if (!tcell.lblPolicy.touchHandler) {
  202. [tcell.lblPolicy addTouchEventHandler:^(id label) {
  203. [self lblPolicyTouched];
  204. }];
  205. }
  206. cell = tcell;
  207. } else if (indexPath.section == 3) {//version info cell
  208. SettingsVersionTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"VersionCellIdentifier"];
  209. tcell.lblVersion.text = [NSString stringWithFormat:@"버전 : %@", [CommonUtil applicationVersion]];
  210. cell = tcell;
  211. }
  212. // cell.selectionStyle = UITableViewCellSelectionStyleNone;
  213. //
  214. // //set background image
  215. // if (indexPath.row % 2 == 1) {
  216. // cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  217. // } else {
  218. // cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  219. // }
  220. return cell;
  221. }
  222. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  223. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  224. // UIViewController *vc = nil;
  225. // switch (indexPath.row) {
  226. // case 0:
  227. // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"AccountViewController" storyboardName:@"Settings"];
  228. // break;
  229. // case 1:
  230. // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MobilesViewController" storyboardName:@"Settings"];
  231. // break;
  232. // case 2:
  233. // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeListViewController" storyboardName:@"Settings"];
  234. // break;
  235. // case 3:
  236. // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ServiceViewController" storyboardName:@"Settings"];
  237. // break;
  238. // case 4:
  239. // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"AppInfoViewController" storyboardName:@"Settings"];
  240. // break;
  241. // }
  242. // [self.navigationController pushViewController:vc animated:YES];
  243. }
  244. #pragma mark - ImageUtil Delegate
  245. - (void)didFinishPickingImage:(UIImage *)image {
  246. [self requestUpdateProfileImage:image];
  247. }
  248. #pragma mark - UI Events
  249. - (void)lblChangeNicknameTouched {
  250. ChangeNamePopupView *popup = [[ChangeNamePopupView alloc] initFromNib];
  251. [popup show];
  252. }
  253. - (void)lblChangePasswordTouched {
  254. PwdPopupView *popup = [[PwdPopupView alloc] initFromNib];
  255. [popup show];
  256. }
  257. - (IBAction)btnProfileTouched:(id)sender {
  258. if (!_imageUtil) {
  259. _imageUtil = [[ImageUtil alloc] init];
  260. _imageUtil.delegate = self;
  261. }
  262. [_imageUtil prepareImagePicker];
  263. }
  264. - (IBAction)chkAutoLoginTouched:(id)sender {
  265. [[JDFacade facade] storeObjectToUserDefaults:@(_chkAutoLogin.checked) forKey:USDEF_APP_AUTO_LOGIN];
  266. if (_chkAutoLogin.checked) {//자동로그인 설정 시, 인증 토큰 저장
  267. [[JDFacade facade] storeObjectToKeychain:[JDFacade facade].loginUser.authToken forKey:USDEF_SESSION_AUTOTOKEN];
  268. } else {;
  269. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
  270. }
  271. }
  272. - (IBAction)btnLogoutTouched:(id)sender {
  273. [[JDFacade facade] logout];
  274. }
  275. //NoticationcationCell's action
  276. - (void)chkHomeModeChangeTouched:(id)sender {
  277. NSDictionary *parameter = @{@"push_type_mode_chg_yn" : _chkHomeModeChange.checked ? ksYES : ksNO};
  278. [self requestUpdatePushSetting:parameter];
  279. }
  280. - (void)chkHomeEmptyChangeTouched:(id)sender {
  281. NSDictionary *parameter = @{@"push_type_rule_exc_yn" : _chkHomeEmptyChange.checked ? ksYES : ksNO};
  282. [self requestUpdatePushSetting:parameter];
  283. }
  284. //service info cell's action
  285. - (void)lblServiceTouched {
  286. }
  287. - (void)lblPolicyTouched {
  288. }
  289. - (IBAction)btnCloseTouched:(id)sender {
  290. [self dismissViewControllerAnimated:YES completion:nil];
  291. }
  292. #pragma mark - MemoryWarning
  293. - (void)didReceiveMemoryWarning
  294. {
  295. [super didReceiveMemoryWarning];
  296. // Dispose of any resources that can be recreated.
  297. }
  298. @end