// // SettingsViewController.m // kneet2 // // Created by Jason Lee on 10/8/15. // Copyright (c) 2015 ntels. All rights reserved. // #import "JDObject.h" #import "RequestHandler.h" #import "CustomLabel.h" #import "CustomButton.h" #import "CustomCheckBox.h" #import "UIButton+WebCache.h" #import "CustomTableView.h" #import "WebBrowseViewController.h" #import "SettingsViewController.h" #import "PwdPopupView.h" #import "ChangeNamePopupView.h" #import "ImageUtil.h" @implementation SettingsAccountTableViewCell - (void)awakeFromNib { } - (void)didMoveToSuperview { [_lblChangeNickname setUnderLine:_lblChangeNickname.text]; [_lblChangePassword setUnderLine:_lblChangePassword.text]; } @end @implementation SettingsNotificationTableViewCell @end @implementation SettingsServiceTableViewCell - (void)didMoveToSuperview { [_lblService setUnderLine:_lblService.text]; [_lblPolicy setUnderLine:_lblPolicy.text]; } @end @implementation SettingsVersionTableViewCell @end @interface SettingsViewController () { CustomCheckBox *_chkAutoLogin, *_chkHomeModeChange, *_chkHomeEmptyChange; CustomButton *_btnProfile; BOOL _isNotFirstLoading; ImageUtil *_imageUtil; } @end #pragma mark - Class Definition @implementation SettingsViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self initUI]; [self prepareViewDidLoad]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)initUI { //set tableview option // _tableView.dataSource = self; // _tableView.delegate = self; // // _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // _tableView.backgroundColor = [UIColor clearColor]; // // _tableView.tableFooterView = [[UIView alloc] init]; //this call table events; [self initTableViewAsDefaultStyle:_tableView]; //Localization // _lblAccount.text = NSLocalizedString(@"계정 정보", @"계정 정보"); // _lblMobile.text = NSLocalizedString(@"단말 관리", @"단말 관리"); // _lblHome.text = NSLocalizedString(@"홈 관리", @"홈 관리"); // _lblService.text = NSLocalizedString(@"서비스 안내", @"서비스 안내"); // _lblApp.text = NSLocalizedString(@"앱 정보", @"앱 정보"); } - (void)prepareViewDidLoad { } #pragma mark - Main Logic - (void)requestUpdatePushSetting:(NSDictionary *)parameter { //parameters NSString *path = [NSString stringWithFormat:API_POST_PUSH_SETTINGS]; [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) { NSString *msg = nil; if ([parameter hasKey:@"push_type_mode_chg_yn"]) { BOOL isNotifyHomeModeChange = [[parameter valueForKey:@"push_type_mode_chg_yn"] boolValue]; if ([[JDFacade facade].loginUser.pushTypeModeChgYn boolValue] != isNotifyHomeModeChange) { msg = isNotifyHomeModeChange ? @"지금부터 홈모드가 바뀔 때\n푸시 알림을 받습니다" : @"홈모드 변경 알림을 해제합니다"; } } else if ([parameter hasKey:@"push_type_rule_exc_yn"]) { BOOL isNotifyExecutingRules = [[parameter valueForKey:@"push_type_rule_exc_yn"] boolValue]; if ([[JDFacade facade].loginUser.pushTypeModeChgYn boolValue] != isNotifyExecutingRules) { msg = isNotifyExecutingRules ? @"지금부터 집이 빌 때\n푸시 알림을 받습니다" : @"빈 집 알림을 해제합니다"; } } [[JDFacade facade] toast:msg]; } failure:^(id errorObject) { JDErrorModel *error = (JDErrorModel *)errorObject; [[JDFacade facade] alert:error.errorMessage]; }]; } - (void)requestUpdateProfileImage:(UIImage *)profileImage { //parameters NSDictionary *parameter = @{@"image_file": profileImage}; NSString *path = [NSString stringWithFormat:API_POST_MEMBER_UPDATE_IMAGE]; [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) { if (!responseObject) {//응답결과가 잘못되었거나 없을 경우, return; } LoginModel *response = (LoginModel *) responseObject; if (response) {//API 성공 , [_btnProfile sd_setImageWithURL:[NSURL URLWithString:response.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached]; [[JDFacade facade] toast:@"프로필 이미지를 변경했습니다"]; } } failure:^(id errorObject) { JDErrorModel *error = (JDErrorModel *)errorObject; [[JDFacade facade] alert:error.errorMessage]; }]; } #pragma mark - UITableView DataSource & Delegate #pragma mark - UITableView DataSource & Delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 4; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat height = 44.0f; if (indexPath.section == 0) { height = 200.0f; } else if (indexPath.section == 1) { height = 200.0f; } else if (indexPath.section == 2) { height = 100.0f; } else if (indexPath.section == 3) { height = 100.0f; } return height; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil;//[super tableView:tableView cellForRowAtIndexPath:indexPath]; if (indexPath.section == 0) {//account cell SettingsAccountTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"AccountCellIdentifier"]; tcell.lblNickname.text = [JDFacade facade].loginUser.nickname; tcell.lblEmail.text = [JDFacade facade].loginUser.emailId; if (!_btnProfile) { _btnProfile = tcell.btnProfile; } //set checkbox if (!_chkAutoLogin) { _chkAutoLogin = tcell.chkAutoLogin; _chkAutoLogin.checked = [[[JDFacade facade] objectForKeyFromUserDefaults:USER_DEF_AUTO_LOGIN] boolValue]; } //set checkbox action if (![_chkAutoLogin actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) { [_chkAutoLogin addTarget:self action:@selector(chkAutoLoginTouched:) forControlEvents:UIControlEventTouchUpInside]; } //set label action if (!tcell.lblChangeNickname.touchHandler) { [tcell.lblChangeNickname addTouchEventHandler:^{ [self lblChangeNicknameTouched]; }]; } //set label action if (!tcell.lblChangePassword.touchHandler) { [tcell.lblChangePassword addTouchEventHandler:^{ [self lblChangePasswordTouched]; }]; } if (![tcell.btnLogout actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target [tcell.btnLogout addTarget:self action:@selector(btnLogoutTouched:) forControlEvents:UIControlEventTouchUpInside]; } if (![tcell.btnProfile actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {//set button target [tcell.btnProfile addTarget:self action:@selector(btnProfileTouched:) forControlEvents:UIControlEventTouchUpInside]; } cell = tcell; } else if (indexPath.section == 1) {//notification cell SettingsNotificationTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"NotificationCellIdentifier"]; if (!_chkHomeModeChange) { _chkHomeModeChange = tcell.chkHomeModeChange; } if (!_chkHomeEmptyChange) { _chkHomeEmptyChange = tcell.chkHomeEmptyChange; } if (!_isNotFirstLoading) { _isNotFirstLoading = YES; _chkHomeModeChange.checked = [[JDFacade facade].loginUser.pushTypeModeChgYn boolValue]; _chkHomeEmptyChange.checked = [[JDFacade facade].loginUser.pushTypeRuleExcYn boolValue]; } if (![tcell.chkHomeModeChange actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) { [tcell.chkHomeModeChange addTarget:self action:@selector(chkHomeModeChangeTouched:) forControlEvents:UIControlEventTouchUpInside]; } if (![tcell.chkHomeEmptyChange actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) { [tcell.chkHomeEmptyChange addTarget:self action:@selector(chkHomeEmptyChangeTouched:) forControlEvents:UIControlEventTouchUpInside]; } cell = tcell; } else if (indexPath.section == 2) {//service info cell SettingsServiceTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"ServiceCellIdentifier"]; if (!tcell.lblService.touchHandler) { [tcell.lblService addTouchEventHandler:^{ [self lblServiceTouched]; }]; } if (!tcell.lblPolicy.touchHandler) { [tcell.lblPolicy addTouchEventHandler:^{ [self lblPolicyTouched]; }]; } cell = tcell; } else if (indexPath.section == 3) {//version info cell SettingsVersionTableViewCell *tcell = [_tableView dequeueReusableCellWithIdentifier:@"VersionCellIdentifier"]; tcell.lblVersion.text = [NSString stringWithFormat:@"버전 : %@", [CommonUtil applicationVersion]]; cell = tcell; } // cell.selectionStyle = UITableViewCellSelectionStyleNone; // // //set background image // if (indexPath.row % 2 == 1) { // cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1]; // } else { // cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2]; // } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [super tableView:tableView didSelectRowAtIndexPath:indexPath]; // UIViewController *vc = nil; // switch (indexPath.row) { // case 0: // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"AccountViewController" storyboardName:@"Settings"]; // break; // case 1: // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"MobilesViewController" storyboardName:@"Settings"]; // break; // case 2: // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeListViewController" storyboardName:@"Settings"]; // break; // case 3: // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ServiceViewController" storyboardName:@"Settings"]; // break; // case 4: // vc = [CommonUtil instantiateViewControllerWithIdentifier:@"AppInfoViewController" storyboardName:@"Settings"]; // break; // } // [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - ImageUtil Delegate - (void)didFinishPickingImage:(UIImage *)image { [self requestUpdateProfileImage:image]; } #pragma mark - UI Events - (void)lblChangeNicknameTouched { ChangeNamePopupView *popup = [[ChangeNamePopupView alloc] initFromNib]; [popup show]; } - (void)lblChangePasswordTouched { PwdPopupView *popup = [[PwdPopupView alloc] initFromNib]; [popup show]; } - (IBAction)btnProfileTouched:(id)sender { if (!_imageUtil) { _imageUtil = [[ImageUtil alloc] init]; _imageUtil.delegate = self; } [_imageUtil prepareImagePicker]; } - (IBAction)chkAutoLoginTouched:(id)sender { [[JDFacade facade] storeObjectToUserDefaults:@(_chkAutoLogin.checked) forKey:USER_DEF_AUTO_LOGIN]; if (_chkAutoLogin.checked) {//자동로그인 설정 시, 인증 토큰 저장 [[JDFacade facade] storeObjectToKeychain:[JDFacade facade].loginUser.authToken forKey:USER_DEF_AUTOTOKEN]; } else {; [[JDFacade facade] removeObjectAtKeychain:USER_DEF_AUTOTOKEN]; } } - (IBAction)btnLogoutTouched:(id)sender { [[JDFacade facade] logout]; } //NoticationcationCell's action - (void)chkHomeModeChangeTouched:(id)sender { NSDictionary *parameter = @{@"push_type_mode_chg_yn" : _chkHomeModeChange.checked ? ksYES : ksNO}; [self requestUpdatePushSetting:parameter]; } - (void)chkHomeEmptyChangeTouched:(id)sender { NSDictionary *parameter = @{@"push_type_rule_exc_yn" : _chkHomeEmptyChange.checked ? ksYES : ksNO}; [self requestUpdatePushSetting:parameter]; } //service info cell's action - (void)lblServiceTouched { } - (void)lblPolicyTouched { } - (IBAction)btnCloseTouched:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - MemoryWarning - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end