SettingsViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 504
  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. _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. //set Phone Number change action
  186. if (![tcell.btnChangeNum actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target
  187. [tcell.btnChangeNum addTarget:self action:@selector(btnChangeNumTouched:) forControlEvents:UIControlEventTouchUpInside];
  188. }
  189. if (![tcell.btnLogout actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target
  190. [tcell.btnLogout addTarget:self action:@selector(btnLogoutTouched:) forControlEvents:UIControlEventTouchUpInside];
  191. }
  192. if (![tcell.btnProfile actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target
  193. [tcell.btnProfile addTarget:self action:@selector(btnProfileTouched:) forControlEvents:UIControlEventTouchUpInside];
  194. }
  195. if (![tcell.btnProfileSetting actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  196. [tcell.btnProfileSetting addTarget:self action:@selector(btnProfileTouched:) forControlEvents:UIControlEventTouchUpInside];
  197. }
  198. cell = tcell;
  199. } else if (indexPath.section == 1) {//notification cell
  200. SettingsNotificationTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"NotificationCellIdentifier"];
  201. if (!_chkHomeModeChange) {
  202. _chkHomeModeChange = tcell.chkHomeModeChange;
  203. }
  204. // if (!_chkHomeEmptyChange) {
  205. // _chkHomeEmptyChange = tcell.chkHomeEmptyChange;
  206. // }
  207. if (!_isNotFirstLoading) {
  208. _isNotFirstLoading = YES;
  209. _chkHomeModeChange.checked = [[JDFacade facade].loginUser.pushTypeModeChgYn boolValue];
  210. // _chkHomeEmptyChange.checked = [[JDFacade facade].loginUser.pushTypePrsncChgYn boolValue];
  211. }
  212. if (![tcell.chkHomeModeChange actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  213. [tcell.chkHomeModeChange addTarget:self action:@selector(chkHomeModeChangeTouched:) forControlEvents:UIControlEventTouchUpInside];
  214. }
  215. if (![tcell.btnThingsSet actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  216. [tcell.btnThingsSet addTarget:self action:@selector(btnThingsSetTouched:) forControlEvents:UIControlEventTouchUpInside];
  217. }
  218. // if (![tcell.chkHomeEmptyChange actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  219. // [tcell.chkHomeEmptyChange addTarget:self action:@selector(chkHomeEmptyChangeTouched:) forControlEvents:UIControlEventTouchUpInside];
  220. // }
  221. // tcell.homehubInfoView.hidden = [JDFacade facade].loginUser.isHomehubOnline;
  222. // if (tcell.homehubInfoView.hidden) {
  223. // tcell.constraintHomehubInfoViewHeight.constant = 0;
  224. // } else {
  225. // _chkHomeModeChange.enabled = NO;
  226. // _chkHomeEmptyChange.enabled = NO;
  227. // }
  228. cell = tcell;
  229. } else if (indexPath.section == 2) {//service info cell
  230. SettingsServiceTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"ServiceCellIdentifier"];
  231. if (![tcell.btnService actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  232. [tcell.btnService addTarget:self action:@selector(btnServiceTouched:) forControlEvents:UIControlEventTouchUpInside];
  233. }
  234. if (![tcell.btnPolicy actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  235. [tcell.btnPolicy addTarget:self action:@selector(btnPolicyTouched:) forControlEvents:UIControlEventTouchUpInside];
  236. }
  237. cell = tcell;
  238. } else if (indexPath.section == 3) {//version info cell
  239. SettingsVersionTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"VersionCellIdentifier"];
  240. tcell.lblVersion.text = [NSString stringWithFormat:@"현재 버전 : %@", [CommonUtil applicationVersion]];
  241. cell = tcell;
  242. } else if (indexPath.section == 4) {//Delete cell
  243. SettingsDeleteTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"DeleteCellIdentifier"];
  244. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  245. [tcell.btnDelete addTarget:self action:@selector(btnDeleteTouched:) forControlEvents:UIControlEventTouchUpInside];
  246. }
  247. cell = tcell;
  248. } else if (indexPath.section == 5) {//Passwd info cell
  249. SettingsPasswdTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"PasswdCellIdentifier"];
  250. if (![tcell.btnPassword actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  251. [tcell.btnPassword addTarget:self action:@selector(btnPasswordTouched:) forControlEvents:UIControlEventTouchUpInside];
  252. }
  253. cell = tcell;
  254. }
  255. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  256. return cell;
  257. }
  258. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  259. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  260. }
  261. #pragma mark - ImageUtil Delegate
  262. - (void)didFinishPickingImage:(UIImage *)image {
  263. [self requestUpdateProfileImage:image];
  264. }
  265. #pragma mark - UI Events
  266. - (void)lblChangeNicknameTouched {
  267. ChangeNamePopupView *popup = [[ChangeNamePopupView alloc] initFromNib];
  268. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  269. if (buttonIndex == 0) {//이름 변경 시,
  270. [_tableView reloadData];
  271. }
  272. }];
  273. }
  274. - (void)lblChangePasswordTouched {
  275. ConfirmPasswdPopupView *popup = [[ConfirmPasswdPopupView alloc] initFromNib];
  276. [popup show];
  277. }
  278. - (void)lblChangeEmailTouched {
  279. ChangeEmailPopupView *popup = [[ChangeEmailPopupView alloc] initFromNib];
  280. [popup show];
  281. }
  282. - (void)btnProfileTouched:(id)sender {
  283. if (!_imageUtil) {
  284. _imageUtil = [[ImageUtil alloc] init];
  285. _imageUtil.delegate = self;
  286. }
  287. [_imageUtil prepareImagePicker:self];
  288. }
  289. - (void)chkAutoLoginTouched:(id)sender {
  290. [[JDFacade facade] storeObjectToUserDefaults:@(_chkAutoLogin.checked) forKey:USDEF_APP_AUTO_LOGIN];
  291. if (_chkAutoLogin.checked) {//자동로그인 설정 시, 인증 토큰 저장
  292. [[JDFacade facade] storeObjectToKeychain:[JDFacade facade].loginUser.authToken forKey:USDEF_SESSION_AUTOTOKEN];
  293. } else {;
  294. [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
  295. }
  296. }
  297. - (void)btnLogoutTouched:(id)sender {
  298. [[JDFacade facade] logout];
  299. }
  300. - (void)btnChangeNumTouched:(id)sender {
  301. NSLog(@"휴대폰 번호 변경");
  302. }
  303. //NoticationcationCell's action
  304. - (void)chkHomeModeChangeTouched:(id)sender {
  305. NSDictionary *parameter = @{@"push_type_mode_chg_yn" : _chkHomeModeChange.checked ? ksYES : ksNO};
  306. [self requestUpdatePushSetting:parameter];
  307. }
  308. //- (void)chkHomeEmptyChangeTouched:(id)sender {
  309. //
  310. // NSDictionary *parameter = @{@"push_type_prsnc_chg_yn" : _chkHomeEmptyChange.checked ? ksYES : ksNO};
  311. // [self requestUpdatePushSetting:parameter];
  312. //}
  313. - (void)btnThingsSetTouched:(id)sender{
  314. NSLog(@"홈모드별 동작할 장치 설정");
  315. }
  316. - (void)btnServiceTouched:(id)sender {
  317. NSString *URLString = [NSString stringWithFormat:@"%@%@", kWebLinkServer, URL_PATH_TERMS];
  318. [[JDFacade facade] loadURLExternalBrowser:URLString];
  319. }
  320. - (void)btnPolicyTouched:(id)sender {
  321. NSString *URLString = [NSString stringWithFormat:@"%@%@", kWebLinkServer, URL_PATH_POLICY];
  322. [[JDFacade facade] loadURLExternalBrowser:URLString];
  323. }
  324. //service passwd cell's action
  325. - (void)btnPasswordTouched:(id)sender {// 셋탑 설정
  326. AdminAuthPopupView *popup = [[AdminAuthPopupView alloc] initFromNib];
  327. [popup show];
  328. }
  329. - (void)btnDeleteTouched:(id)sender { // 계정 삭제
  330. DeleteAccountPopupView *popup = [[DeleteAccountPopupView alloc]initFromNib];
  331. [popup show];
  332. }
  333. - (IBAction)btnCloseTouched:(id)sender {
  334. [self dismissViewControllerAnimated:YES completion:nil];
  335. }
  336. #pragma mark - MemoryWarning
  337. - (void)didReceiveMemoryWarning
  338. {
  339. [super didReceiveMemoryWarning];
  340. // Dispose of any resources that can be recreated.
  341. }
  342. @end