// // RulesConditionHomeModePopupView.m // kneet2 // // Created by Jason Lee on 11/25/15. // Copyright (c) 2015 ntels. All rights reserved. // #import "JDObject.h" #import "RequestHandler.h" #import "JDJSONModel.h" #import "CustomLabel.h" #import "CustomTableView.h" #import "CustomButton.h" #import "CustomCheckBox.h" #import "ImageUtil.h" #import "RulesConditionHomeModePopupView.h" @implementation RulesConditionHomeModeTableViewCell @end @interface RulesConditionHomeModePopupView () { NSMutableArray *_modeList; BOOL _isNotFirstLoading; UIImage *_bgCellImage1, *_bgCellImage2; NSMutableArray *_checkedItems; } @end @implementation RulesConditionHomeModePopupView - (id)initFromNib { for (UIView *view in [CommonUtil nibViews:@"RulesConditionHomeModePopupView"]) { if ([view isKindOfClass:[RulesConditionHomeModePopupView class]]) { self = (RulesConditionHomeModePopupView *)view; //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함. self.frame = [UIScreen mainScreen].bounds; [self initTableViewAsDefaultStyle:_tableView]; UINib *nib = [UINib nibWithNibName:@"RulesConditionHomeModeTableViewCell" bundle:nil]; [_tableView registerNib:nib forCellReuseIdentifier:@"ModeCellIdentifier"]; [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)]; [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)]; [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)]; [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)]; [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)]; //Localization [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal]; [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal]; UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 2); _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_01"]]; _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_02"]]; } } return self; } - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView { tableView.dataSource = self; tableView.delegate = self; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.backgroundColor = [UIColor clearColor]; tableView.tableFooterView = [[UIView alloc] init]; //this call table events; } - (void)didMoveToSuperview { [self requestHomeModes]; } - (void)setCondition:(ItemModel *)condition { _condition = condition; if (_condition.itemSubTypeCode && [_condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeHomeMode]) { //set values. } else { _condition.itemName = @"이 홈모드에서만"; _condition.itemSubTypeCode = ksConditionSubTypeCodeHomeMode; } } #pragma mark - Main Logic - (void)requestHomeModes { NSString *path = API_GET_DASHBOARD_MODE_LIST; [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ModeListModel class] completion:^(id responseObject) { if (!responseObject) {//응답결과가 잘못되었거나 없을 경우, return; } ModeListModel *fetchedModeList = (ModeListModel *)responseObject; if (fetchedModeList && fetchedModeList.list && fetchedModeList.list.count) {//API 성공 , _modeList = (NSMutableArray *)[[NSMutableArray alloc] initWithArray:fetchedModeList.list]; if (!_isNotFirstLoading) { for (ModeModel *mode in _modeList) { [[JDFacade facade] setCheckBoxStatus:@YES object:mode]; } _isNotFirstLoading = YES; } [_tableView reloadData]; } } failure:^(id errorObject) { JDErrorModel *error = (JDErrorModel *)errorObject; [[JDFacade facade] alert:error.errorMessage]; }]; } #pragma mark - UITableView DataSource & Delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _modeList ? _modeList.count : 0; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat height = 76.0f; return height; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ModeModel *mode = _modeList[indexPath.row]; RulesConditionHomeModeTableViewCell *tcell = (RulesConditionHomeModeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ModeCellIdentifier"]; tcell.lblMode.text = mode.modeName; tcell.chkSelect.value = mode; tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue]; //set background image if (indexPath.row % 2 == 0) { tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1]; } else { tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2]; } return tcell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; RulesConditionHomeModeTableViewCell *tcell = (RulesConditionHomeModeTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath]; [tcell.chkSelect checkBoxClicked]; [_tableView reloadData]; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // Remove seperator inset if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } // Prevent the cell from inheriting the Table View's margin settings if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { [cell setPreservesSuperviewLayoutMargins:NO]; } // Explictly set your cell's layout margins if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } } #pragma mark - UI Events - (IBAction)btnConfirmTouched:(id)sender { //validate & set values NSMutableString *modeValues = [[NSMutableString alloc] init]; NSMutableString *modeValueMsgs = [[NSMutableString alloc] init]; for (ModeModel *mode in _modeList) { if ([[[JDFacade facade] getCheckBoxStatus:mode] boolValue]) { NSString *prefix = [modeValues isEmptyString] ? ksEmptyString : @","; NSString *prefix2 = [modeValues isEmptyString] ? ksEmptyString : @" / "; [modeValues appendFormat:@"%@%@", prefix, mode.modeId]; [modeValueMsgs appendFormat:@"%@%@", prefix2, mode.modeName]; } } if ([modeValues isEmptyString]) { [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")]; return; } ItemSubModel *subCondition = nil; if (_condition.subItems && _condition.subItems.count) { subCondition = _condition.subItems[0]; } else { subCondition = [[ItemSubModel alloc] init]; _condition.subItems = (NSMutableArray *)[[NSMutableArray alloc] init]; [_condition.subItems addObject:subCondition]; } subCondition.sourceId = @"VAR"; subCondition.conditionTypeCode = @"01"; subCondition.cmdclsValue = modeValues; subCondition.cmdclsValueMsg = modeValueMsgs; [super btnConfirmTouched:sender]; } @end