RegisterCmdClsViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //
  2. // RegisterCmdClsViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 5/22/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "RequestHandler.h"
  9. #import "RuleModel.h"
  10. #import "DeviceModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomImageView.h"
  13. #import "UIButton+WebCache.h"
  14. #import "CustomButton.h"
  15. #import "RulesTriggerOptionViewController.h"
  16. #import "RegisterCmdClsViewController.h"
  17. #import "RegisterModeViewController.h"
  18. #import "CommandClassListPopupView.h"
  19. //tableview
  20. @interface RegisterCmdClsTableViewCell () {
  21. }
  22. @end
  23. @implementation RegisterCmdClsTableViewCell
  24. - (void)awakeFromNib {
  25. self.selectionStyle = UITableViewCellSelectionStyleNone;
  26. self.collectionView.scrollEnabled = NO;
  27. }
  28. - (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
  29. {
  30. self.collectionView.dataSource = dataSourceDelegate;
  31. self.collectionView.delegate = dataSourceDelegate;
  32. self.collectionView.indexPath = indexPath;
  33. [self.collectionView reloadData];
  34. }
  35. @end
  36. @interface RegisterCmdClsCollectionView () {
  37. }
  38. @end
  39. @implementation RegisterCmdClsCollectionView
  40. @end
  41. //CollectionViewCell
  42. @interface RegisterCmdClsCollectionViewCell () {
  43. UIImage *_btnBgImageNormal, *_btnBgImageHighlight;
  44. }
  45. @end
  46. @implementation RegisterCmdClsCollectionViewCell
  47. - (void)setBtnItemImage:(BOOL)isActive {
  48. if (isActive) {
  49. [_btnItem setBackgroundImage:_btnBgImageHighlight forState:UIControlStateNormal];
  50. } else {
  51. [_btnItem setBackgroundImage:_btnBgImageNormal forState:UIControlStateNormal];
  52. }
  53. }
  54. - (void)awakeFromNib {
  55. _btnBgImageNormal = [UIImage imageNamed:@"tp_01_img_rule_triggericon_bg"];
  56. _btnBgImageHighlight = [UIImage imageNamed:@"tp_01_img_rule_triggericon_bg_active"];
  57. }
  58. @end
  59. //viewController
  60. @interface RegisterCmdClsViewController () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate> {
  61. NSMutableArray<ItemModel> *_itemList;
  62. BOOL _isNotFirstLoading;
  63. NSInteger _sectionCount;
  64. RulesTriggerOptionViewController *_triggervc;
  65. ItemModel *_currentItem;
  66. }
  67. @end
  68. #pragma mark - Class Definition
  69. @implementation RegisterCmdClsViewController
  70. - (void)viewDidLoad {
  71. [super viewDidLoad];
  72. // Do any additional setup after loading the view.
  73. [self initUI];
  74. [self prepareViewDidLoad];
  75. }
  76. - (void)initUI {
  77. _tableView.delegate = self;
  78. _tableView.dataSource= self;
  79. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  80. _tableView.backgroundColor = [UIColor clearColor];
  81. if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {//iOS 7인 경우, Seperate 설정
  82. [_tableView setSeparatorInset:UIEdgeInsetsZero];
  83. }
  84. _tableView.tableFooterView = [[UIView alloc] init];
  85. //Localization
  86. [_btnSelect setTitle:NSLocalizedString(@"선택", @"선택") forState:UIControlStateNormal];
  87. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  88. }
  89. - (void)prepareViewDidLoad {
  90. ItemSubModel *subItem = _tmpItem.predDevices && _tmpItem.predDevices.count ? _tmpItem.predDevices[0] : nil;
  91. _sectionCount = subItem.conditions && subItem.conditions.count ? 4 : 3;
  92. [self requestRuelsCommandClassList];
  93. }
  94. #pragma mark - Main Logic
  95. - (void)requestRuelsCommandClassList {
  96. NSString *path = [NSString stringWithFormat:API_GET_RULE_CMDCLS, _tmpItem.itemTypeCode];
  97. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ItemListModel class] completion:^(id responseObject) {
  98. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  99. return;
  100. }
  101. ItemListModel *result = (ItemListModel *)responseObject;
  102. if (result) {//API 성공 ,
  103. _itemList = (NSMutableArray<ItemModel> *)[NSMutableArray arrayWithArray:result.list];
  104. [self matchActionsForCommnadClassList];
  105. }
  106. } failure:^(id errorObject) {
  107. JDErrorModel *error = (JDErrorModel *)errorObject;
  108. [[JDFacade facade] alert:error.errorMessage];
  109. }];
  110. }
  111. - (void)matchActionsForCommnadClassList {
  112. //이전에 선택된 액션과 액션리스트를 매칭시킴.
  113. NSInteger index = [_itemList indexOfObjectPassingTest:^BOOL(ItemModel *cmdcls, NSUInteger idx, BOOL *stop) {
  114. return [_tmpItem.predItemSequence isEqualToString:cmdcls.predItemSequence];
  115. }];
  116. if (index != NSNotFound) {
  117. if (_tmpItem.predDevices) {
  118. ItemModel *cmdcls = _itemList[index];
  119. cmdcls.predDevices = _tmpItem.predDevices;
  120. _currentItem = cmdcls;
  121. }
  122. }
  123. [_tableView reloadData];
  124. }
  125. #pragma mark - UITableView DataSource & Delegate
  126. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  127. return _itemList.count ? _sectionCount : 0;
  128. }
  129. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  130. NSInteger count = 1;
  131. return count;
  132. }
  133. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  134. CGFloat height = 0;
  135. if (indexPath.section == 0) {//title
  136. height = 65.0f;
  137. } else if (indexPath.section == 1) {//collection cells
  138. RegisterCmdClsTableViewCell *cell = (RegisterCmdClsTableViewCell *)[self tableView:_tableView cellForRowAtIndexPath:indexPath];
  139. cell.collectionView.delegate = self;
  140. cell.collectionView.dataSource = self;
  141. CGFloat adjustHeight = 0.0f;
  142. if (cell.collectionView.collectionViewLayout) {
  143. CGSize cs = cell.collectionView.collectionViewLayout.collectionViewContentSize;
  144. adjustHeight = cs.height;
  145. }
  146. height += adjustHeight + 10 + 40;
  147. } else if (indexPath.section == 2) {//activation
  148. height = 80.0f;
  149. } else if (indexPath.section == 3) {//container
  150. height = 300;
  151. }
  152. return height;
  153. }
  154. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  155. UITableViewCell *cell = nil;
  156. NSLog(@"%s\n %zd", __PRETTY_FUNCTION__, indexPath.section);
  157. if (indexPath.section == 0) {//title
  158. RulesStaticTitleTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"StaticTitleCellIdentifier"];
  159. if (tcell == nil) {
  160. tcell = [[RulesStaticTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StaticTitleCellIdentifier"];
  161. }
  162. tcell.lblItemName.text = NSLocalizedString(@"집에 변화가 일어날 때", @"집에 변화가 일어날 때");
  163. cell = tcell;
  164. } else if (indexPath.section == 1) {//커맨드클래스 컬렉션뷰
  165. RegisterCmdClsTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"CollectionCellIdentifier"];
  166. if (tcell == nil) {
  167. tcell = [[RegisterCmdClsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CollectionCellIdentifier"];
  168. }
  169. cell = tcell;
  170. } else if (indexPath.section == 2) {//activation
  171. // RegisterTriggerActivationTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"ActivationCellIdentifier"];
  172. // if (!tcell) {
  173. // tcell = (RegisterTriggerActivationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ActivationCellIdentifier"];
  174. // }
  175. //
  176. // if (![tcell.btnTriggerOption actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  177. // [tcell.btnTriggerOption addTarget:self action:@selector(btnAddTriggerOptionTouched:) forControlEvents:UIControlEventTouchUpInside];
  178. // }
  179. //
  180. // //이미지 전환
  181. // [tcell setTriggerOptionEnable:_sectionCount == 4];
  182. // cell = tcell;
  183. } else if (indexPath.section == 3) {
  184. // RegisterContainerTableViewCell *tcell = (RegisterContainerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ContainerCellIdentifier"];
  185. //
  186. // if (!_triggervc) {//트리거뷰를 로드함.
  187. // _triggervc = (RulesTriggerOptionViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesTriggerOptionViewController" storyboardName:@"Rules"];
  188. // _triggervc.tmpItem = _tmpItem;
  189. //
  190. // [self addChildViewController:_triggervc];
  191. // [_triggervc didMoveToParentViewController:self];
  192. //
  193. // if ([self isViewLoaded] && tcell.triggerOptionContainer) {
  194. // [_triggervc beginAppearanceTransition:YES animated:NO];
  195. // [tcell.triggerOptionContainer addSubview:_triggervc.view];
  196. // [_triggervc endAppearanceTransition];
  197. // [_triggervc.view mas_makeConstraints:^(MASConstraintMaker *make) {
  198. // make.size.mas_equalTo(tcell.triggerOptionContainer);
  199. // make.center.equalTo(tcell.triggerOptionContainer);
  200. // }];
  201. // }
  202. // }
  203. //
  204. // cell = tcell;
  205. }
  206. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  207. return cell;
  208. }
  209. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  210. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  211. //
  212. // if (indexPath.section == 2) {
  213. // RegisterTriggerActivationTableViewCell *tcell = (RegisterTriggerActivationTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  214. // [self btnAddTriggerOptionTouched:tcell.btnTriggerOption];
  215. // }
  216. // [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  217. }
  218. - (void)tableView:(UITableView *)tableView willDisplayCell:(RegisterCmdClsTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  219. if (indexPath.section == 1) {
  220. //set indexpath at collectionview
  221. [cell setCollectionViewDataSourceDelegate:self indexPath:indexPath];
  222. }
  223. // Remove seperator inset
  224. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  225. [cell setSeparatorInset:UIEdgeInsetsZero];
  226. }
  227. // Prevent the cell from inheriting the Table View's margin settings
  228. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  229. [cell setPreservesSuperviewLayoutMargins:NO];
  230. }
  231. // Explictly set your cell's layout margins
  232. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  233. [cell setLayoutMargins:UIEdgeInsetsZero];
  234. }
  235. }
  236. #pragma mark - UICollectionView Delegate
  237. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  238. return 1;
  239. }
  240. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  241. NSInteger cellCount = _itemList.count;
  242. return cellCount;
  243. }
  244. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  245. static NSString *kCellID = @"CmdClsCollectionCellIdentifier";
  246. RegisterCmdClsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
  247. ItemModel *item = _itemList[indexPath.row];
  248. [cell setBtnItemImage:item.predDevices && item.predDevices.count];
  249. cell.btnItem.value = item;
  250. cell.btnItem.value2 = indexPath;
  251. [cell.btnItem sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] forState:UIControlStateNormal];
  252. if (![cell.btnItem actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  253. [cell.btnItem addTarget:self action:@selector(btnItemTouched:) forControlEvents:UIControlEventTouchUpInside];
  254. }
  255. CGFloat width = ((IPHONE_WIDTH - 48.0f) / 3.0f) - 18.0f;
  256. cell.constraintBtnItemWidth.constant = (int)width;
  257. cell.constraintBtnItemHeight.constant = (int)width;
  258. cell.lblItem.text = item.itemName;
  259. return cell;
  260. }
  261. - (void)btnItemTouched:(id)sender {
  262. CustomButton *btnItem = (CustomButton *)sender;
  263. ItemModel *item = btnItem.value;
  264. //popup - 사전정의 규칙 디바이스 목록 조회
  265. CommandClassListPopupView *popup = [[CommandClassListPopupView alloc] initFromNib];
  266. popup.tmpItem = item;
  267. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  268. if (buttonIndex == 0) {//OK
  269. if (![_currentItem isEqual:item]) {//동일 아이템이 아닐 경우, 변경함.
  270. _currentItem.predDevices = nil;
  271. [btnItem faceOffImage];
  272. [_tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
  273. _currentItem = item;
  274. }
  275. }
  276. }];
  277. }
  278. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  279. // CGRect cr = collectionView.frame;
  280. // CGFloat width = IPHONE_WIDTH - 48.0f;
  281. // if (cr.size.width != width) {//not appear yet
  282. // cr.size.width = width;
  283. // collectionView.frame = cr;
  284. // }
  285. //
  286. // RegisterCmdClsCollectionView *rcollectionView = (RegisterCmdClsCollectionView *)collectionView;
  287. // ItemModel *item = _itemList[rcollectionView.indexPath.row];
  288. //
  289. CGFloat width = ((IPHONE_WIDTH - 48.0f) / 3.0f) - 18.0f;
  290. return CGSizeMake(width, 107);
  291. }
  292. #pragma mark - UI Events
  293. - (IBAction)btnAddTriggerOptionTouched:(id)sender {
  294. _sectionCount = _sectionCount == 3 ? 4 : 3;
  295. [_tableView reloadData];
  296. if (_sectionCount == 4) {
  297. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
  298. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  299. }
  300. }
  301. - (void)btnSelectTouched:(id)sender {
  302. //IMPORTANT : 수정모드 일 경우, 기존 컨디션이 사라지는 경우 생김. 아래까지 스크롤이 안된경우,
  303. //컨디션을 설정함 - 이전 선택된 컨디션을 유지하기 위해 먼저 확인 - 테이블 뷰의 특성상.
  304. NSMutableArray< ItemModel> *conditions = nil;
  305. if (_triggervc && [_triggervc hasCondition]) {
  306. if (!_triggervc.conditions) {
  307. return;
  308. }
  309. conditions = _triggervc.conditions;
  310. } else {
  311. if (_tmpItem.predDevices && _tmpItem.predDevices.count && !_triggervc) {
  312. PredefinedDeviceModel *pdevice = _tmpItem.predDevices[0];
  313. conditions = pdevice.conditions;
  314. }
  315. }
  316. _tmpItem.predDevices = nil;
  317. //현재 선택된 아이템을 반환해야함.
  318. for (ItemModel *item in _itemList) {
  319. if (item.predDevices && item.predDevices.count) {
  320. _tmpItem.predItemSequence = item.predItemSequence;
  321. _tmpItem.predDevices = (NSMutableArray<PredefinedDeviceModel> *)[[NSMutableArray alloc] initWithArray:item.predDevices];
  322. break;
  323. }
  324. }
  325. //1.validation
  326. if (!_tmpItem.predDevices || !_tmpItem.predDevices.count) {
  327. [[JDFacade facade] alert:NSLocalizedString(@"선택된 장치가 없습니다", @"선택된 장치가 없습니다")];
  328. return;
  329. }
  330. if (conditions) {
  331. PredefinedDeviceModel *pdevice = _tmpItem.predDevices[0];
  332. pdevice.conditions = conditions;
  333. }
  334. [self.navigationController popViewControllerAnimated:YES];
  335. }
  336. - (void)btnCancelTouched:(id)sender {
  337. [self.navigationController popViewControllerAnimated:YES];
  338. }
  339. #pragma mark - MemoryWarning
  340. - (void)didReceiveMemoryWarning
  341. {
  342. [super didReceiveMemoryWarning];
  343. // Dispose of any resources that can be recreated.
  344. }
  345. @end