SettingsViewController.m 15 KB

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