RulesConditionHomeModePopupView.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. //Localization
  37. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  38. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  39. UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 2);
  40. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_01"]];
  41. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_02"]];
  42. }
  43. }
  44. return self;
  45. }
  46. - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
  47. tableView.dataSource = self;
  48. tableView.delegate = self;
  49. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  50. tableView.backgroundColor = [UIColor clearColor];
  51. tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  52. }
  53. - (void)didMoveToSuperview {
  54. [self requestHomeModes];
  55. }
  56. - (void)setCondition:(ItemModel *)condition {
  57. _condition = condition;
  58. if (_condition.itemSubTypeCode && [_condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeHomeMode]) {
  59. //set values.
  60. } else {
  61. _condition.itemName = @"이 홈모드에서만";
  62. _condition.itemSubTypeCode = ksConditionSubTypeCodeHomeMode;
  63. }
  64. }
  65. #pragma mark - Main Logic
  66. - (void)requestHomeModes {
  67. NSString *path = API_GET_DASHBOARD_MODE_LIST;
  68. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ModeListModel class] completion:^(id responseObject) {
  69. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  70. return;
  71. }
  72. ModeListModel *fetchedModeList = (ModeListModel *)responseObject;
  73. if (fetchedModeList && fetchedModeList.list && fetchedModeList.list.count) {//API 성공 ,
  74. _modeList = (NSMutableArray<ModeModel> *)[[NSMutableArray alloc] initWithArray:fetchedModeList.list];
  75. if (!_isNotFirstLoading) {
  76. for (ModeModel *mode in _modeList) {
  77. [[JDFacade facade] setCheckBoxStatus:@YES object:mode];
  78. }
  79. _isNotFirstLoading = YES;
  80. }
  81. [_tableView reloadData];
  82. }
  83. } failure:^(id errorObject) {
  84. JDErrorModel *error = (JDErrorModel *)errorObject;
  85. [[JDFacade facade] alert:error.errorMessage];
  86. }];
  87. }
  88. #pragma mark - UITableView DataSource & Delegate
  89. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  90. return 1;
  91. }
  92. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  93. return _modeList ? _modeList.count : 0;
  94. }
  95. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  96. CGFloat height = 77.0f;
  97. return height;
  98. }
  99. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  100. ModeModel *mode = _modeList[indexPath.row];
  101. RulesConditionHomeModeTableViewCell *tcell = (RulesConditionHomeModeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ModeCellIdentifier"];
  102. tcell.lblMode.text = mode.modeName;
  103. tcell.chkSelect.value = mode;
  104. tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
  105. //set background image
  106. if (indexPath.row % 2 == 0) {
  107. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  108. } else {
  109. tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  110. }
  111. return tcell;
  112. }
  113. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  114. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  115. RulesConditionHomeModeTableViewCell *tcell = (RulesConditionHomeModeTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  116. [tcell.chkSelect checkBoxClicked];
  117. [_tableView reloadData];
  118. }
  119. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  120. // Remove seperator inset
  121. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  122. [cell setSeparatorInset:UIEdgeInsetsZero];
  123. }
  124. // Prevent the cell from inheriting the Table View's margin settings
  125. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  126. [cell setPreservesSuperviewLayoutMargins:NO];
  127. }
  128. // Explictly set your cell's layout margins
  129. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  130. [cell setLayoutMargins:UIEdgeInsetsZero];
  131. }
  132. }
  133. #pragma mark - UI Events
  134. - (IBAction)btnConfirmTouched:(id)sender {
  135. //validate & set values
  136. NSMutableString *modeValues = [[NSMutableString alloc] init];
  137. NSMutableString *modeValueMsgs = [[NSMutableString alloc] init];
  138. for (ModeModel *mode in _modeList) {
  139. if ([[[JDFacade facade] getCheckBoxStatus:mode] boolValue]) {
  140. NSString *prefix = [modeValues isEmptyString] ? ksEmptyString : @",";
  141. NSString *prefix2 = [modeValues isEmptyString] ? ksEmptyString : @" / ";
  142. [modeValues appendFormat:@"%@%@", prefix, mode.modeId];
  143. [modeValueMsgs appendFormat:@"%@%@", prefix2, mode.modeName];
  144. }
  145. }
  146. if ([modeValues isEmptyString]) {
  147. [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
  148. return;
  149. }
  150. ItemSubModel *subCondition = nil;
  151. if (_condition.subItems && _condition.subItems.count) {
  152. subCondition = _condition.subItems[0];
  153. } else {
  154. subCondition = [[ItemSubModel alloc] init];
  155. _condition.subItems = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  156. [_condition.subItems addObject:subCondition];
  157. }
  158. subCondition.sourceId = @"VAR";
  159. subCondition.conditionTypeCode = @"01";
  160. subCondition.cmdclsValue = modeValues;
  161. subCondition.cmdclsValueMsg = modeValueMsgs;
  162. [super btnConfirmTouched:sender];
  163. }
  164. @end