ActionPopTableView.m 9.8 KB

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