RulesConditionHomeModePopupView.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // RulesConditionHomeModePopupView.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/25/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "JDJSONModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomTableView.h"
  13. #import "CustomButton.h"
  14. #import "CustomCheckBox.h"
  15. #import "ImageUtil.h"
  16. #import "RulesConditionHomeModePopupView.h"
  17. @implementation RulesConditionHomeModeTableViewCell
  18. @end
  19. @interface RulesConditionHomeModePopupView () <UITableViewDataSource, UITableViewDelegate> {
  20. NSMutableArray<ModeModel> *_modeList;
  21. UIImage *_bgCellImage1, *_bgCellImage2;
  22. NSMutableArray<ItemModel> *_checkedItems;
  23. }
  24. @end
  25. @implementation RulesConditionHomeModePopupView
  26. - (id)initFromNib {
  27. for (UIView *view in [CommonUtil nibViews:@"RulesConditionHomeModePopupView"]) {
  28. if ([view isKindOfClass:[RulesConditionHomeModePopupView class]]) {
  29. self = (RulesConditionHomeModePopupView *)view;
  30. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  31. self.frame = [UIScreen mainScreen].bounds;
  32. [self initTableViewAsDefaultStyle:_tableView];
  33. UINib *nib = [UINib nibWithNibName:@"RulesConditionHomeModeTableViewCell" bundle:nil];
  34. [_tableView registerNib:nib forCellReuseIdentifier:@"ModeCellIdentifier"];
  35. [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  36. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  37. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  38. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  39. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  40. //Localization
  41. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  42. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  43. UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 2);
  44. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_01"]];
  45. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_02"]];
  46. }
  47. }
  48. return self;
  49. }
  50. - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
  51. tableView.dataSource = self;
  52. tableView.delegate = self;
  53. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  54. tableView.backgroundColor = [UIColor clearColor];
  55. tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  56. }
  57. - (void)didMoveToSuperview {
  58. [self requestHomeModes];
  59. }
  60. - (void)setCondition:(ItemModel *)condition {
  61. _condition = condition;
  62. if (_condition.itemSubTypeCode && [_condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeHomeMode]) {
  63. //set values.
  64. } else {
  65. _condition.itemName = @"이 홈모드에서만";
  66. _condition.itemSubTypeCode = ksConditionSubTypeCodeHomeMode;
  67. }
  68. }
  69. #pragma mark - Main Logic
  70. - (void)requestHomeModes {
  71. NSDictionary *parameter = @{@"member_id": [JDFacade facade].loginUser.memberId};
  72. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_POST_DASHBOARD arguments:nil];
  73. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path
  74. parameters:parameter
  75. modelClass:[DashboardModel class]
  76. completion:^(id responseObject) {
  77. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  78. return;
  79. }
  80. DashboardModel *dashboard = (DashboardModel*)responseObject;
  81. if (dashboard.modeList.count > 0) {//API 성공 ,
  82. _modeList = [dashboard.modeList mutableCopy];
  83. for (ModeModel *mode in _modeList) {
  84. [[JDFacade facade] setCheckBoxStatus:@YES object:mode];
  85. }
  86. [_tableView reloadData];
  87. }
  88. } failure:^(id errorObject) {
  89. JDErrorModel *error = (JDErrorModel *)errorObject;
  90. [[JDFacade facade] alert:error.errorMessage];
  91. }];
  92. }
  93. #pragma mark - UITableView DataSource & Delegate
  94. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  95. return 1;
  96. }
  97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  98. return _modeList ? _modeList.count : 0;
  99. }
  100. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  101. CGFloat height = 76.0f;
  102. return height;
  103. }
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  105. ModeModel *mode = _modeList[indexPath.row];
  106. RulesConditionHomeModeTableViewCell *tcell = (RulesConditionHomeModeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ModeCellIdentifier"];
  107. [tcell.chkSelect setDaySelectBgImage:4];
  108. tcell.lblMode.text = mode.modeName;
  109. tcell.chkSelect.value = mode;
  110. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  111. tcell.backgroundView = tcell.chkSelect.checked ? [[UIImageView alloc] initWithImage:_bgCellImage1] : [[UIImageView alloc] initWithImage:_bgCellImage2];
  112. // //set background image
  113. // if (indexPath.row % 2 == 0) {
  114. // tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  115. // } else {
  116. // tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  117. // }
  118. return tcell;
  119. }
  120. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  121. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  122. RulesConditionHomeModeTableViewCell *tcell = (RulesConditionHomeModeTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  123. [tcell.chkSelect checkBoxClicked];
  124. [_tableView reloadData];
  125. }
  126. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  127. // Remove seperator inset
  128. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  129. [cell setSeparatorInset:UIEdgeInsetsZero];
  130. }
  131. // Prevent the cell from inheriting the Table View's margin settings
  132. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  133. [cell setPreservesSuperviewLayoutMargins:NO];
  134. }
  135. // Explictly set your cell's layout margins
  136. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  137. [cell setLayoutMargins:UIEdgeInsetsZero];
  138. }
  139. }
  140. #pragma mark - UI Events
  141. - (IBAction)btnConfirmTouched:(id)sender {
  142. //validate & set values
  143. NSMutableString *modeValues = [[NSMutableString alloc] init];
  144. NSMutableString *modeValueMsgs = [[NSMutableString alloc] init];
  145. for (ModeModel *mode in _modeList) {
  146. if ([[[JDFacade facade] getCheckBoxStatus:mode] boolValue]) {
  147. NSString *prefix = [modeValues isEmptyString] ? ksEmptyString : @",";
  148. NSString *prefix2 = [modeValues isEmptyString] ? ksEmptyString : @" / ";
  149. [modeValues appendFormat:@"%@%@", prefix, mode.modeId];
  150. [modeValueMsgs appendFormat:@"%@%@", prefix2, mode.modeName];
  151. }
  152. }
  153. if ([modeValues isEmptyString]) {
  154. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  155. return;
  156. }
  157. ItemSubModel *subCondition = nil;
  158. if (_condition.subItems && _condition.subItems.count) {
  159. subCondition = _condition.subItems[0];
  160. } else {
  161. subCondition = [[ItemSubModel alloc] init];
  162. _condition.subItems = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  163. [_condition.subItems addObject:subCondition];
  164. }
  165. subCondition.sourceId = @"VAR";
  166. subCondition.conditionTypeCode = @"01";
  167. subCondition.cmdclsValue = modeValues;
  168. subCondition.cmdclsValueMsg = modeValueMsgs;
  169. [super btnConfirmTouched:sender];
  170. }
  171. @end