SettingsViewController.m 17 KB

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