RegisterCmdClsViewController.m 15 KB

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