ActionPopTableView.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // ActionPopTableView.h
  3. // kneet
  4. //
  5. // Created by Jason Lee on 5/15/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "CustomLabel.h"
  10. #import "CustomRadioGroup.h"
  11. #import "CustomImageView.h"
  12. #import "CustomButton.h"
  13. #import "RequestHandler.h"
  14. #import "ItemModel.h"
  15. #import "ActionPopTableView.h"
  16. #import "PredefinedDeviceViewController.h"
  17. #import "RegisterModeViewController.h"
  18. #import "RegisterPushViewController.h"
  19. #import "ImageUtil.h"
  20. #define kfPopTableViewCellHeight 100.0f
  21. @implementation ActionPopTableViewCell
  22. - (void)awakeFromNib
  23. {
  24. self.selectionStyle = UITableViewCellSelectionStyleNone;
  25. }
  26. @end
  27. @interface ActionPopTableView () <UITableViewDataSource, UITableViewDelegate> {
  28. NSMutableArray<ItemModel> *_itemList;
  29. UIImage *_bgCellImage;
  30. BOOL _isNotFirstLoading;
  31. }
  32. @end
  33. @implementation ActionPopTableView
  34. - (id)initFromNib {
  35. for (UIView *view in [CommonUtil nibViews:@"ActionPopTableView"]) {
  36. if ([view isKindOfClass:[ActionPopTableView class]]) {
  37. self = (ActionPopTableView *)view;
  38. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  39. self.frame = [UIScreen mainScreen].bounds;
  40. self.lblTitle.text = NSLocalizedString(@"무엇을 실행할까요?",nil);
  41. if (!self.tableView.delegate) {
  42. _tableView.dataSource = self;
  43. _tableView.delegate = self;
  44. }
  45. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  46. _tableView.separatorColor = kUILineColor2;
  47. _tableView.backgroundColor = [UIColor clearColor];
  48. _tableView.scrollEnabled = YES;
  49. _tableView.tableFooterView = [[UIView alloc] init];
  50. UINib *nib = [UINib nibWithNibName:@"ActionPopTableViewCell" bundle:nil];
  51. [_tableView registerNib:nib forCellReuseIdentifier:@"CellIdentifier"];
  52. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  53. _bgCellImage = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  54. [self performSelector:@selector(requestCommandClassList) withObject:nil afterDelay:0.0f];
  55. [self.btnConfirm setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  56. }
  57. }
  58. return self;
  59. }
  60. - (void)show {
  61. [super show];
  62. //FIXME : 수정!!
  63. if (!self.tableView.delegate) {
  64. self.tableView.delegate = self;
  65. self.tableView.dataSource= self;
  66. }
  67. [_tableView reloadData];
  68. [_tableView scrollsToTop];
  69. }
  70. - (void)setActions:(NSArray<ItemModel> *)actions {
  71. _actions = actions;
  72. [self matchActionsForCommnadClassList];
  73. }
  74. - (void)awakeFromNib {
  75. }
  76. #pragma mark - Main Logic
  77. //여길 호출해줘야됨.
  78. - (void)requestCommandClassList {
  79. //parameters
  80. // NSDictionary *parameter = @{@"homegrp_scene_id": _homeGroupSceneId ? _homeGroupSceneId : ksEmptyString};
  81. NSString *path = nil;
  82. if (_isRuleMode) {
  83. path = [NSString stringWithFormat:API_GET_RULE_CMDCLS, @"02"];
  84. } else {
  85. path = [NSString stringWithFormat:API_GET_SCENE_CMDCLS];
  86. }
  87. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ItemListModel class] completion:^(id responseObject) {
  88. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  89. return;
  90. }
  91. ItemListModel *result = (ItemListModel *) responseObject;
  92. if (result) {//API 성공 ,
  93. _itemList = (NSMutableArray<ItemModel> *)[NSMutableArray arrayWithArray:result.list];
  94. [self matchActionsForCommnadClassList];
  95. }
  96. } failure:^(id errorObject) {
  97. JDErrorModel *error = (JDErrorModel *)errorObject;
  98. [[JDFacade facade] alert:error.errorMessage];
  99. }];
  100. }
  101. - (void)matchActionsForCommnadClassList {
  102. //이전에 선택된 액션과 액션리스트를 매칭시킴.
  103. for (ItemModel *action in _actions) {
  104. NSInteger index = [_itemList indexOfObjectPassingTest:^BOOL(ItemModel *listAction, NSUInteger idx, BOOL *stop) {
  105. NSInteger findex = NSNotFound;
  106. if (_isRuleMode) {
  107. findex = [action.predItemSequence isEqualToString:listAction.predItemSequence];
  108. } else {
  109. findex = [action.predActionSequence isEqualToString:listAction.predActionSequence];
  110. }
  111. return findex;
  112. }];
  113. if (index != NSNotFound) {//FIXME : 교체를 해버리면,, 결국,, 이전데이터가 남아있음.
  114. ItemModel *listAction = _itemList[index];
  115. action.totalCnt = listAction.totalCnt;
  116. [_itemList replaceObjectAtIndex:index withObject:action];
  117. }
  118. }
  119. [_tableView reloadData];
  120. }
  121. //- (void)resetAction:(ItemModel *)action {
  122. // for (ItemModel *listAction in _itemList) {
  123. // if ([listAction isEqual:action]) {
  124. // listAction.predDevices = nil;
  125. // listAction.cnt = @"0";
  126. // break;
  127. // }
  128. // }
  129. //}
  130. #pragma mark - UITableView DataSource & Delegate
  131. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  132. return 1;
  133. }
  134. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  135. return _itemList.count;
  136. }
  137. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  138. static NSString *CellIdentifier = @"CellIdentifier";
  139. ActionPopTableViewCell *cell = (ActionPopTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  140. ItemModel *item = _itemList[indexPath.row];
  141. cell.lblActionName.text = item.itemName;
  142. NSString *subItemsCount = nil;
  143. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  144. subItemsCount = [NSString stringWithFormat:@"%zd / %@", item.sumPredDeviceCount, item.totalCnt];
  145. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeMode]) {//모드일 경우,
  146. subItemsCount = [NSString stringWithFormat:@"%zd / %@", item.sumModesCount, item.totalCnt];
  147. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
  148. subItemsCount = [NSString stringWithFormat:@"%zd / %@", item.sumPushesCount, item.totalCnt];
  149. }
  150. cell.lblActionCount.text = subItemsCount;
  151. cell.btnSelect.hidden = ![subItemsCount integerValue];
  152. if (!cell.btnSelect.hidden) {
  153. [cell.lblActionName setColor:kUITextColor02 text:cell.lblActionName.text];
  154. [cell.lblActionCount setColor:kUITextColor02 text:cell.lblActionCount.text];
  155. }
  156. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage];
  157. return cell;
  158. }
  159. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  160. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  161. ItemModel *item = _itemList[indexPath.row];
  162. UIViewController *vc = nil;
  163. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스
  164. PredefinedDeviceViewController *dvc = (PredefinedDeviceViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"PredefinedDeviceViewController" storyboardName:@"Rules"];
  165. if (!_actions || [_actions indexOfObject:item] == NSNotFound) {//자유 선택 - 최초 생성 시 플래그 설정
  166. dvc.isCustomCreation = YES;
  167. }
  168. dvc.title = [ItemModel itemSubTypeString:item.itemSubTypeCode];
  169. if (_isRuleMode) {
  170. dvc.predRuleId = item.predRuleId;
  171. } else {
  172. dvc.predSceneId = item.predSceneId;
  173. }
  174. dvc.tmpItem = item;
  175. vc = dvc;
  176. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeMode]) {//모드
  177. RegisterModeViewController *mvc = (RegisterModeViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RegisterModeViewController" storyboardName:@"Rules"];
  178. if (!_actions || [_actions indexOfObject:item] == NSNotFound) {//자유 선택 - 최초 생성 시 플래그 설정
  179. mvc.isCustomCreation = YES;
  180. }
  181. mvc.title = [ItemModel itemSubTypeString:item.itemSubTypeCode];
  182. mvc.tmpItem = item;
  183. vc = mvc;
  184. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
  185. RegisterPushViewController *pvc = (RegisterPushViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RegisterPushViewController" storyboardName:@"Rules"];
  186. if (!_actions || [_actions indexOfObject:item] == NSNotFound) {//자유 선택 - 최초 생성 시 플래그 설정
  187. pvc.isCustomCreation = YES;
  188. }
  189. pvc.title = [ItemModel itemSubTypeString:item.itemSubTypeCode];
  190. pvc.tmpItem = item;
  191. vc = pvc;
  192. }
  193. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  194. [self btnConfirmTouched:nil];
  195. }
  196. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  197. // Remove seperator inset
  198. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  199. [cell setSeparatorInset:UIEdgeInsetsZero];
  200. }
  201. // Prevent the cell from inheriting the Table View's margin settings
  202. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  203. [cell setPreservesSuperviewLayoutMargins:NO];
  204. }
  205. // Explictly set your cell's layout margins
  206. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  207. [cell setLayoutMargins:UIEdgeInsetsZero];
  208. }
  209. }
  210. - (void)btnConfirmTouched:(id)sender {
  211. self.tableView.delegate = nil;
  212. self.tableView.dataSource = nil;
  213. [super btnConfirmTouched:sender];
  214. }
  215. - (void)btnCancelTouched:(id)sender {
  216. self.tableView.delegate = nil;
  217. self.tableView.dataSource = nil;
  218. [super btnCancelTouched:sender];
  219. }
  220. @end