RulesConditionViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. //
  2. // RulesConditionViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/25/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "ItemModel.h"
  10. #import "CustomTableView.h"
  11. #import "CustomLabel.h"
  12. #import "CustomButton.h"
  13. #import "CustomImageView.h"
  14. #import "RulesConditionHomeModePopupView.h"
  15. #import "RulesAddViewController.h"
  16. #import "DeviceSelectPopupView.h"
  17. #import "DeviceNodePopupView.h"
  18. #import "UIImageView+WebCache.h"
  19. #import "RulesConditionViewController.h"
  20. @implementation RulesConditionHeaderTableViewCell
  21. @end
  22. @implementation RulesConditionDeviceTableViewCell
  23. @end
  24. @implementation RulesConditionTableViewCell
  25. @end
  26. @implementation RulesConditionFooterTableViewCell
  27. @end
  28. @interface RulesConditionViewController () <UITableViewDataSource, UITableViewDelegate> {
  29. CustomButton *_btnModeAdd, *_btnDurationAdd;
  30. NSMutableArray *_arrayForHeader, *_arrayForFooter;
  31. RulesConditionHomeModePopupView *_mpopup;
  32. }
  33. @property (weak, nonatomic, readonly) NSMutableArray<ItemSubModel> *modes;
  34. @property (weak, nonatomic, readonly) NSMutableArray<ItemSubModel> *durations;
  35. @property (weak, nonatomic, readonly) NSMutableArray<ItemSubModel> *pdevices;
  36. @end
  37. #pragma mark - Class Definition
  38. @implementation RulesConditionViewController
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. // Do any additional setup after loading the view.
  42. [self initUI];
  43. [self prepareViewDidLoad];
  44. }
  45. - (void)initUI {
  46. [self initTableViewAsDefaultStyle:_tableView];
  47. _tableView.scrollEnabled = NO;
  48. }
  49. - (void)prepareViewDidLoad {
  50. }
  51. - (CGFloat)tableHeight {
  52. CGFloat height = _isShowingMode ? 0 : 255;
  53. if (_isShowingMode) {
  54. height = self.modes.count > 0 ? height + 95 + 60 : height; //add row height + header height
  55. height = self.durations.count > 0 ? height + 95 + 60: height;
  56. height = self.pdevices.count > 0 ? height + (95 * self.pdevices.count) + 60 : height;
  57. } else {
  58. height = self.modes.count > 0 ? height + 95 : height;
  59. height = self.durations.count > 0 ? height + 95 : height;
  60. height = self.pdevices.count > 0 ? height + (95 * self.pdevices.count) : height;
  61. }
  62. return height;
  63. }
  64. #pragma mark - Main Logic
  65. - (NSMutableArray<ItemSubModel> *)modes {
  66. ItemModel *mode = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
  67. return mode.subItems;
  68. }
  69. - (NSMutableArray<ItemSubModel> *)durations {
  70. ItemModel *duration = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDueDate];
  71. return duration.subItems;
  72. }
  73. - (NSMutableArray<ItemSubModel> *)pdevices {
  74. ItemModel *pdevice = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  75. return pdevice.subItems;
  76. }
  77. #pragma mark - UITableView DataSource & Delegate
  78. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  79. return 3;
  80. }
  81. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  82. NSInteger count = 0;
  83. if (section == 0) {//mode
  84. count = self.modes.count;
  85. } else if (section == 1) {//duration
  86. count = self.durations.count;
  87. } else if (section == 2) {//device
  88. count = self.pdevices.count;
  89. }
  90. return count;
  91. }
  92. - (NSString *)headerTitleForSection:(NSInteger)section {
  93. NSString *title = nil;
  94. if (section == 0) {//mode
  95. title = @"이 홈모드에서만";
  96. } else if (section == 1) {//duration
  97. title = @"이 기간동안만";
  98. } else if (section == 2) {//devices
  99. title = @"이 장치가 특정 상태일 때만";
  100. }
  101. return title;
  102. }
  103. - (void)addTargetToHeaderAddButton:(CustomButton *)btnAdd section:(NSInteger)section {
  104. if (section == 0) {
  105. _btnModeAdd = btnAdd;
  106. [btnAdd addTarget:self action:@selector(btnAddModeTouched:) forControlEvents:UIControlEventTouchUpInside];
  107. } else if (section == 1) {
  108. [btnAdd addTarget:self action:@selector(btnAddDurationTouched:) forControlEvents:UIControlEventTouchUpInside];
  109. } else if (section == 2) {
  110. _btnDurationAdd = btnAdd;
  111. [btnAdd addTarget:self action:@selector(btnAddDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
  112. }
  113. }
  114. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  115. BOOL hasHeader = !_isShowingMode;
  116. if (section == 0) {
  117. hasHeader = self.modes.count || !_isShowingMode;
  118. } else if (section == 1) {
  119. hasHeader = self.durations.count || !_isShowingMode;
  120. } else if (section == 2) {
  121. hasHeader = self.pdevices.count || !_isShowingMode;
  122. }
  123. UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
  124. if (!view) {
  125. if (hasHeader) {
  126. RulesConditionHeaderTableViewCell *hcell = (RulesConditionHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  127. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  128. [view addSubview:hcell];
  129. hcell.lblTitle.text = [self headerTitleForSection:section];
  130. hcell.btnAdd.hidden = _isShowingMode;
  131. if (!hcell.btnAdd.hidden && ![hcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  132. [self addTargetToHeaderAddButton:hcell.btnAdd section:section];
  133. }
  134. } else {
  135. view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, CGFLOAT_MIN)];
  136. }
  137. if (_arrayForHeader.count == section) {
  138. [_arrayForHeader insertObject:view atIndex:section];
  139. }
  140. } else {
  141. if (hasHeader) {
  142. RulesConditionHeaderTableViewCell *hcell = (RulesConditionHeaderTableViewCell *)view.subviews[0];
  143. hcell.lblTitle.text = hcell.lblTitle.text = [self headerTitleForSection:section];
  144. }
  145. }
  146. return view;
  147. }
  148. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  149. BOOL hasHeader = !_isShowingMode;
  150. if (section == 0) {
  151. hasHeader = self.modes.count || !_isShowingMode;
  152. } else if (section == 1) {
  153. hasHeader = self.durations.count || !_isShowingMode;
  154. } else if (section == 2) {
  155. hasHeader = self.pdevices.count || !_isShowingMode;
  156. }
  157. return hasHeader ? 60.0f : CGFLOAT_MIN;
  158. }
  159. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  160. BOOL hasFooter = !_isShowingMode;
  161. if (section == 0) {
  162. hasFooter = self.modes.count || !_isShowingMode;
  163. } else if (section == 1) {
  164. hasFooter = self.durations.count || !_isShowingMode;
  165. } else if (section == 2) {
  166. hasFooter = self.pdevices.count || !_isShowingMode;
  167. }
  168. UIView *view = _arrayForFooter.count > section ? _arrayForFooter[section] : nil;
  169. if (!view) {
  170. if (hasFooter) {
  171. RulesConditionFooterTableViewCell *hcell = (RulesConditionFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FooterCellIdentifier"];
  172. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  173. [view addSubview:hcell];
  174. } else {
  175. view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, CGFLOAT_MIN)];
  176. }
  177. if (_arrayForHeader.count == section) {
  178. [_arrayForHeader insertObject:view atIndex:section];
  179. }
  180. }
  181. return view;
  182. }
  183. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  184. BOOL hasFooter = !_isShowingMode;
  185. if (section == 0) {
  186. hasFooter = self.modes.count || !_isShowingMode;
  187. } else if (section == 1) {
  188. hasFooter = self.durations.count || !_isShowingMode;
  189. } else if (section == 2) {
  190. hasFooter = self.pdevices.count || !_isShowingMode;
  191. }
  192. return hasFooter ? 15.0f : CGFLOAT_MIN;
  193. }
  194. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  195. CGFloat height = 95.0f;
  196. return height;
  197. }
  198. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  199. UITableViewCell *cell = nil;
  200. NSInteger section = indexPath.section;
  201. if (section == 0) {//mode
  202. ItemSubModel *subCondition = self.modes[0];
  203. RulesConditionTableViewCell *tcell = (RulesConditionTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
  204. tcell.lblItem.text = subCondition.cmdclsValueMsg;
  205. tcell.btnDelete.hidden = _isShowingMode;
  206. if (!tcell.btnDelete.hidden) {
  207. tcell.btnDelete.value = subCondition;
  208. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  209. [tcell.btnDelete addTarget:self action:@selector(btnDeleteModeTouched:) forControlEvents:UIControlEventTouchUpInside];
  210. }
  211. }
  212. cell = tcell;
  213. } else if (section == 1) {//duration
  214. RulesConditionTableViewCell *tcell = (RulesConditionTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
  215. cell = tcell;
  216. } else if (section == 2) {//action
  217. RulesConditionDeviceTableViewCell *tcell = (RulesConditionDeviceTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"DeviceCellIdentifier"];
  218. ItemSubModel *subItem = self.pdevices[indexPath.row];
  219. tcell.lblItem.text = subItem.sourceName;
  220. if ([subItem.deleteYn boolValue]) {//삭제된 노드가 있을 경우,
  221. [tcell.lblItem setStrikethrough:tcell.lblItem.text];
  222. }
  223. tcell.lblSubItem.text = subItem.sourceSubName;
  224. [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  225. //노드의 액션 값을 세팅함.
  226. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  227. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  228. tcell.lblCondition.text = cmdclsValue.cmdclsValueMsg;
  229. } else {
  230. tcell.lblCondition.text = subItem.cmdclsValueMsg;
  231. }
  232. tcell.btnDelete.hidden = _isShowingMode;
  233. if (!tcell.btnDelete.hidden) {
  234. tcell.btnDelete.value = subItem;
  235. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  236. [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
  237. }
  238. } else {
  239. tcell.constraintConditionLabelRight.constant = 15.0f;
  240. }
  241. cell = tcell;
  242. }
  243. return cell;
  244. }
  245. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  246. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  247. if (indexPath.section == 2) {//actions
  248. ItemSubModel *subItem = self.pdevices[indexPath.row];
  249. [RulesAddViewController modifyAction:subItem refDevices:_refDevices tableView:_tableView];
  250. }
  251. }
  252. #pragma mark - Table Events
  253. - (void)btnAddModeTouched:(id)sender {
  254. ItemModel *mode = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
  255. BOOL isNewTrigger = mode == nil;
  256. mode = isNewTrigger ? [[ItemModel alloc] init] : mode;
  257. if (!_mpopup) {
  258. _mpopup = [[RulesConditionHomeModePopupView alloc] initFromNib];
  259. }
  260. _mpopup.condition = mode;
  261. [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  262. if (buttonIndex == 0) {//OK
  263. if (isNewTrigger) {
  264. [_conditions addObject:mode];
  265. }
  266. _btnModeAdd.hidden = YES;
  267. [self updateTableView];
  268. }
  269. }];
  270. }
  271. - (void)btnAddDurationTouched:(id)sender {
  272. _btnDurationAdd.hidden = YES;
  273. }
  274. - (void)btnAddDeviceTouched:(id)sender {
  275. ItemModel *deviceCondition = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  276. BOOL isNewTrigger = deviceCondition == nil;
  277. deviceCondition = isNewTrigger ? [[ItemModel alloc] init] : deviceCondition;
  278. DeviceSelectPopupView *dpopup = [[DeviceSelectPopupView alloc] initFromNib];
  279. dpopup.refDevices = _refDevices;
  280. dpopup.typeCode = ksItemTypeCodeTrigger;
  281. [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  282. if (buttonIndex == 0) {//ok
  283. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  284. npopup.refDevice = dpopup.selectedDevices[0];
  285. npopup.typeCode = ksItemTypeCodeTrigger;
  286. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  287. //action add
  288. if (buttonIndex == 0) {//OK
  289. if (isNewTrigger) {
  290. deviceCondition.itemName = @"이 장치가 특정 상태일 때만";
  291. deviceCondition.itemSubTypeCode = ksItemSubTypeCodeDevice; //트리거 타입 설정
  292. deviceCondition.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  293. [_conditions addObject:deviceCondition];
  294. }
  295. ItemSubModel *snode = npopup.selectedNode;
  296. //존재 여부 확인
  297. if (![deviceCondition.subItems objectByUsingPredicateFormat:@"sourceId == %@ && sourceSubId == %@", snode.sourceId, snode.sourceSubId]) {
  298. [deviceCondition.subItems addObject: snode];
  299. }
  300. [self updateTableView];
  301. }
  302. }];
  303. }
  304. }];
  305. }
  306. - (void)btnDeleteModeTouched:(id)sender {
  307. CustomButton *btnDelete = (CustomButton *)sender;
  308. ItemSubModel *mode = btnDelete.value;
  309. [self.modes removeObject:mode];
  310. _btnModeAdd.hidden = NO;
  311. [self updateTableView];
  312. }
  313. - (void)btnDeleteDeviceTouched:(id)sender {
  314. CustomButton *btnDelete = (CustomButton *)sender;
  315. ItemSubModel *pdevice = btnDelete.value;
  316. //선택 설정을 초기화
  317. [[JDFacade facade] setRadioButtonStatus:@NO object:pdevice];
  318. for (CmdClsValueModel *cmdclsValue in pdevice.cmdclsValueList) {
  319. cmdclsValue.isSelected = NO;
  320. }
  321. [self.pdevices removeObject:pdevice];
  322. [self updateTableView];
  323. }
  324. - (void)updateTableView {
  325. CGFloat height = 255;
  326. height = self.modes.count > 0 ? height + 95 : height;
  327. height = self.durations.count > 0 ? height + 95 : height;
  328. height = self.pdevices.count > 0 ? height + (95 * self.pdevices.count) : height;
  329. self.view.height = height;
  330. [_tableView reloadData];
  331. if ([self.parentViewController isKindOfClass:[RulesAddViewController class]]) {
  332. RulesAddViewController *rvc = (RulesAddViewController *)self.parentViewController;
  333. [rvc.tableView reloadData];
  334. }
  335. }
  336. #pragma mark - UI Events
  337. #pragma mark - MemoryWarning
  338. - (void)didReceiveMemoryWarning
  339. {
  340. [super didReceiveMemoryWarning];
  341. // Dispose of any resources that can be recreated.
  342. }
  343. @end