RulesConditionHomeModePopupView.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. BOOL _isNotFirstLoading;
  22. UIImage *_bgCellImage1, *_bgCellImage2;
  23. NSMutableArray<ItemModel> *_checkedItems;
  24. }
  25. @end
  26. @implementation RulesConditionHomeModePopupView
  27. - (id)initFromNib {
  28. for (UIView *view in [CommonUtil nibViews:@"RulesConditionHomeModePopupView"]) {
  29. if ([view isKindOfClass:[RulesConditionHomeModePopupView class]]) {
  30. self = (RulesConditionHomeModePopupView *)view;
  31. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  32. self.frame = [UIScreen mainScreen].bounds;
  33. [self initTableViewAsDefaultStyle:_tableView];
  34. UINib *nib = [UINib nibWithNibName:@"RulesConditionHomeModeTableViewCell" bundle:nil];
  35. [_tableView registerNib:nib forCellReuseIdentifier:@"ModeCellIdentifier"];
  36. [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  37. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  38. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  39. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  40. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  41. //Localization
  42. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  43. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  44. UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 2);
  45. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_01"]];
  46. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_02"]];
  47. }
  48. }
  49. return self;
  50. }
  51. - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
  52. tableView.dataSource = self;
  53. tableView.delegate = self;
  54. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  55. tableView.backgroundColor = [UIColor clearColor];
  56. tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  57. }
  58. - (void)didMoveToSuperview {
  59. [self requestHomeModes];
  60. }
  61. - (void)setCondition:(ItemModel *)condition {
  62. _condition = condition;
  63. if (_condition.itemSubTypeCode && [_condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeHomeMode]) {
  64. //set values.
  65. } else {
  66. _condition.itemName = @"이 홈모드에서만";
  67. _condition.itemSubTypeCode = ksConditionSubTypeCodeHomeMode;
  68. }
  69. }
  70. #pragma mark - Main Logic
  71. - (void)requestHomeModes {
  72. NSString *path = API_GET_DASHBOARD_MODE_LIST;
  73. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ModeListModel class] completion:^(id responseObject) {
  74. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  75. return;
  76. }
  77. ModeListModel *fetchedModeList = (ModeListModel *)responseObject;
  78. if (fetchedModeList && fetchedModeList.list && fetchedModeList.list.count) {//API 성공 ,
  79. _modeList = (NSMutableArray<ModeModel> *)[[NSMutableArray alloc] initWithArray:fetchedModeList.list];
  80. if (!_isNotFirstLoading) {
  81. for (ModeModel *mode in _modeList) {
  82. [[JDFacade facade] setCheckBoxStatus:@YES object:mode];
  83. }
  84. _isNotFirstLoading = YES;
  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.lblMode.text = mode.modeName;
  108. tcell.chkSelect.value = mode;
  109. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  110. //set background image
  111. if (indexPath.row % 2 == 0) {
  112. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  113. } else {
  114. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  115. }
  116. return tcell;
  117. }
  118. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  119. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  120. RulesConditionHomeModeTableViewCell *tcell = (RulesConditionHomeModeTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  121. [tcell.chkSelect checkBoxClicked];
  122. [_tableView reloadData];
  123. }
  124. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  125. // Remove seperator inset
  126. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  127. [cell setSeparatorInset:UIEdgeInsetsZero];
  128. }
  129. // Prevent the cell from inheriting the Table View's margin settings
  130. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  131. [cell setPreservesSuperviewLayoutMargins:NO];
  132. }
  133. // Explictly set your cell's layout margins
  134. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  135. [cell setLayoutMargins:UIEdgeInsetsZero];
  136. }
  137. }
  138. #pragma mark - UI Events
  139. - (IBAction)btnConfirmTouched:(id)sender {
  140. //validate & set values
  141. NSMutableString *modeValues = [[NSMutableString alloc] init];
  142. NSMutableString *modeValueMsgs = [[NSMutableString alloc] init];
  143. for (ModeModel *mode in _modeList) {
  144. if ([[[JDFacade facade] getCheckBoxStatus:mode] boolValue]) {
  145. NSString *prefix = [modeValues isEmptyString] ? ksEmptyString : @",";
  146. NSString *prefix2 = [modeValues isEmptyString] ? ksEmptyString : @" / ";
  147. [modeValues appendFormat:@"%@%@", prefix, mode.modeId];
  148. [modeValueMsgs appendFormat:@"%@%@", prefix2, mode.modeName];
  149. }
  150. }
  151. if ([modeValues isEmptyString]) {
  152. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  153. return;
  154. }
  155. ItemSubModel *subCondition = nil;
  156. if (_condition.subItems && _condition.subItems.count) {
  157. subCondition = _condition.subItems[0];
  158. } else {
  159. subCondition = [[ItemSubModel alloc] init];
  160. _condition.subItems = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  161. [_condition.subItems addObject:subCondition];
  162. }
  163. subCondition.sourceId = @"VAR";
  164. subCondition.conditionTypeCode = @"01";
  165. subCondition.cmdclsValue = modeValues;
  166. subCondition.cmdclsValueMsg = modeValueMsgs;
  167. [super btnConfirmTouched:sender];
  168. }
  169. @end