RulesAddViewController.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. //
  2. // RulesAddViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/20/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "ItemModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomButton.h"
  13. #import "CustomImageView.h"
  14. #import "CustomTableView.h"
  15. #import "UIImageView+WebCache.h"
  16. #import "RulesAddViewController.h"
  17. #import "CustomCheckBox.h"
  18. #import "DeviceSelectPopupView.h"
  19. #import "DeviceNodePopupView.h"
  20. #import "TriggerSelectPopupView.h"
  21. #import "DeviceModel.h"
  22. #import "CustomTextView.h"
  23. #import "ValidateUtil.h"
  24. #import "CustomTextField.h"
  25. #import "RuleModel.h"
  26. @implementation RulesAddTitleTableViewCell
  27. @end
  28. @implementation RulesAddHeaderTableViewCell
  29. @end
  30. @implementation RulesAddTableViewCell
  31. @end
  32. @implementation RulesAddPushTableViewCell
  33. @end
  34. @implementation RulesAddConditionHeaderTableViewCell
  35. @end
  36. @implementation RulesAddConditionTableViewCell
  37. @end
  38. @implementation RulesAddFooterTableViewCell
  39. @end
  40. @interface RulesAddViewController () <CustomTextViewDelegate> {
  41. NSMutableArray<ItemModel> *_triggers, *_actions, *_conditions, *_pushes;
  42. NSMutableArray<ItemModel> *_triggerDevices, *_actionDevices;
  43. BOOL _isNotFirstLoading, _isCustomCreation;
  44. NSMutableArray *_arrayForHeader, *_arrayForFooter;
  45. CustomTextField *_txtRuleTitle;
  46. CustomButton *_btnTriggerAdd, *_btnPushAdd;
  47. CustomCheckBox *_chkConditions;
  48. }
  49. @end
  50. #pragma mark - Class Definition
  51. @implementation RulesAddViewController
  52. - (void)viewDidLoad {
  53. [super viewDidLoad];
  54. _arrayForHeader = [[NSMutableArray alloc] init];
  55. _arrayForFooter = [[NSMutableArray alloc] init];
  56. [self initUI];
  57. [self prepareViewDidLoad];
  58. }
  59. - (void)initUI {
  60. [self initTableViewAsDefaultStyle:_tableView];
  61. }
  62. - (void)prepareViewDidLoad {
  63. [self requestDeviceListForHome];
  64. }
  65. #pragma mark - Main Logic
  66. - (void)requestDeviceListForHome {
  67. //parameters
  68. NSDictionary *parameter = @{@"item_type_code": ksItemTypeCodeAction};
  69. NSString *path = [NSString stringWithFormat:API_GET_ITEM_DEVICES, ksItemTypeCodeAction];
  70. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[ItemListModel class] completion:^(id responseObject) {
  71. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  72. return;
  73. }
  74. ItemListModel *fetchedItemList = (ItemListModel *)responseObject;
  75. if (fetchedItemList && fetchedItemList.list && fetchedItemList.list.count) {//API 성공 ,
  76. _triggerDevices = fetchedItemList.list;
  77. _actionDevices = [(NSMutableArray<ItemModel> *)[NSMutableArray alloc] initWithArray:_triggerDevices copyItems:YES];
  78. }
  79. } failure:^(id errorObject) {
  80. JDErrorModel *error = (JDErrorModel *)errorObject;
  81. [[JDFacade facade] alert:error.errorMessage];
  82. }];
  83. }
  84. - (void)requestRegisterRule {
  85. NSArray *triggers = [self items:_triggers];
  86. NSArray *actions = [self items:_actions];
  87. NSArray *conditions = [self conditions:_triggers];
  88. //validate
  89. if (!triggers || !triggers.count) {
  90. [[JDFacade facade] alert:NSLocalizedString(@"선택된 트리거가 없습니다", @"선택된 트리거가 없습니다")];
  91. return;
  92. }
  93. if (!actions || !actions.count) {
  94. [[JDFacade facade] alert:NSLocalizedString(@"액션이 없습니다", @"액션이 없습니다")];
  95. return;
  96. }
  97. //parameters
  98. NSDictionary *parameter = @{@"rule_name" : _txtRuleTitle.text,
  99. @"use_yn" : ksYES,
  100. @"triggers": triggers ? triggers : [NSNull null],
  101. @"actions": actions ? actions : [NSNull null],
  102. @"conditions": conditions ? conditions : [NSNull null]};
  103. NSString *path = [NSString stringWithFormat:API_POST_RULE];
  104. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[RuleModel class] completion:^(id responseObject) {
  105. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  106. return;
  107. }
  108. RuleModel *result = (RuleModel *) responseObject;
  109. if (result) {//API 성공 ,
  110. [[JDFacade facade] dismissModalStack:YES completion:^{
  111. [[JDFacade facade] toast:NSLocalizedString(@"등록되었습니다", @"등록되었습니다")];
  112. }];
  113. }
  114. } failure:^(id errorObject) {
  115. JDErrorModel *error = (JDErrorModel *)errorObject;
  116. [[JDFacade facade] alert:error.errorMessage];
  117. }];
  118. }
  119. //아이템 배열을 리턴함.
  120. - (NSArray *)items:(NSArray<ItemModel> *)items {
  121. //triggers, actions, conditions
  122. NSMutableArray *rItems = [[NSMutableArray alloc] init];
  123. for (ItemModel *item in items) {
  124. NSDictionary *dic = nil;
  125. NSMutableArray *subItems = [[NSMutableArray alloc] init];
  126. NSArray *pdevices = [self subItemsForType:item.subItems itemSubType:ksItemSubTypeCodeDevice];
  127. NSArray *pushes = [self subItemsForType:item.pushes itemSubType:ksItemSubTypeCodeAppPush];
  128. NSArray *timers = [self subItemsForType:item.timers itemSubType:ksItemSubTypeCodeTimer];
  129. NSArray *daylights = [self subItemsForType:item.timers itemSubType:ksItemSubTypeCodeDaylight];
  130. if (pdevices && pdevices.count) {//device
  131. [subItems addObjectsFromArray:pdevices];
  132. }
  133. if (pushes && pushes.count) {//push
  134. [subItems addObjectsFromArray:pushes];
  135. }
  136. if (timers && timers.count) {//timer
  137. [subItems addObjectsFromArray:timers];
  138. }
  139. if (daylights && daylights.count) {//해뜰때/질때
  140. [subItems addObject:daylights];
  141. }
  142. if (subItems.count) {
  143. dic = @{@"item_name": item.itemName,
  144. @"item_sub_type_code": item.itemSubTypeCode,
  145. @"item_sub": subItems};
  146. [rItems addObject:dic];
  147. }
  148. }
  149. return rItems;
  150. }
  151. //서브아이템 배열을 리턴함.
  152. - (NSArray *)subItemsForType:(NSArray *)subItems itemSubType:(NSString *)itemSubTypeCode {
  153. NSMutableArray *rSubItems = [[NSMutableArray alloc] init];
  154. if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  155. for (ItemSubModel *pdevice in subItems) {
  156. NSDictionary *rSubItem = @{@"source_id": pdevice.sourceId,
  157. @"source_sub_id": pdevice.sourceSubId,
  158. @"cmdcls_value": pdevice.cmdclsValue,
  159. @"data_type_code": pdevice.dataTypeCode,
  160. };
  161. [rSubItems addObject:rSubItem];
  162. }
  163. } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
  164. //at once.
  165. for (ItemSubModel *subItem in subItems) {
  166. NSDictionary *rSubItem = @{@"cmdcls_value": subItem.cmdclsValue};
  167. [rSubItems addObject:rSubItem];
  168. }
  169. } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  170. for (ItemSubModel *subItem in subItems) {
  171. NSDictionary *rSubItem = @{@"hour": subItem.hour,
  172. @"minute": subItem.minute,
  173. @"dayofweek": subItem.cmdclsValue,
  174. @"repeatable": ksYES};
  175. [rSubItems addObject:rSubItem];
  176. }
  177. } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 일 경우,
  178. for (ItemSubModel *subItem in subItems) {
  179. NSDictionary *rSubItem = @{@"sourceId": subItem.sourceId,
  180. @"sourceSubId": subItem.sourceSubId};
  181. [rSubItems addObject:rSubItem];
  182. }
  183. }
  184. return rSubItems;
  185. }
  186. - (NSArray *)conditions:(NSArray<ItemModel> *)items {//trigger만 해당됨.
  187. //conditions
  188. NSMutableArray *rConditions = [[NSMutableArray alloc] init];
  189. for (ItemModel *item in items) {
  190. ItemSubModel *subItem = nil;
  191. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  192. subItem = item.predDevices && item.predDevices.count ? item.predDevices[0] : nil;
  193. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeMode]) {//모드일 경우,
  194. subItem = item.modes[0];
  195. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  196. subItem = item.timers[0];
  197. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeGeoFencing]) {//지오펜싱일 경우,
  198. subItem = item.mobileDevices && item.mobileDevices.count ? item.mobileDevices[0] : nil;
  199. }
  200. for (ItemModel *condition in subItem.conditions) {//컨디션 여부
  201. ItemModel *copyCondition = [condition copy];
  202. for (ItemSubModel *subCondition in copyCondition.subItems) {
  203. subCondition.cmdclsValueMsg = nil;
  204. subCondition.dueDate = nil;
  205. subCondition.daysOfWeek = nil;
  206. subCondition.dueTime = nil;
  207. subCondition.homeModes = nil;
  208. subCondition.externHeat = nil;
  209. }
  210. [rConditions addObject:[copyCondition toDictionary]];
  211. }
  212. }
  213. return rConditions;
  214. }
  215. #pragma mark - UITableView DataSource & Delegate
  216. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  217. return 5;
  218. }
  219. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  220. NSInteger count = 0;
  221. if (section == 0) {//title
  222. count = 1;
  223. } else if (section == 1) {//triggers
  224. count = _triggers && _triggers.count ? _triggers.count : 0;
  225. } else if (section == 2) {//actions
  226. NSArray *matchedSubItems = [_actions matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  227. count = matchedSubItems && matchedSubItems.count ? matchedSubItems.count : 0;
  228. } else if (section == 3) {//push-message
  229. count = _pushes && _pushes.count ? _pushes.count : 0;
  230. } else if (section == 4) {//conditions
  231. count = _chkConditions.checked ? 1 : 0;
  232. }
  233. return count;
  234. }
  235. - (NSString *)headerTitleForSection:(NSInteger)section {
  236. NSString *title = nil;
  237. if (section == 1) {//trigger
  238. title = @"이 때가 되면";
  239. if (_triggers && _triggers.count) {
  240. ItemModel *item = _triggers[0];
  241. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  242. title = @"장치 상태가 바뀔 때";
  243. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
  244. title = @"이 시간마다";
  245. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
  246. title = @"더울때 / 추울때";
  247. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
  248. title = @"해뜰때 / 질때";
  249. }
  250. }
  251. } else if (section == 2) {//action
  252. title = @"이 장치를 실행";
  253. // title = _actions && _actions.count ? @"선택됨" : title;
  254. } else if (section == 3) {//pushes
  255. title = @"알림 메시지";
  256. // title = _pushes && _pushes.count ? @"선택됨" : title;
  257. }
  258. // else if (section == 3) {//conditions
  259. // title = @"추가 조건";
  260. // title = _triggers && triggers.count ? @"선택됨" : title;
  261. // }
  262. return title;
  263. }
  264. - (void)addTargetToHeaderAddButton:(CustomButton *)btnAdd section:(NSInteger)section {
  265. if (section == 1) {
  266. _btnTriggerAdd = btnAdd;
  267. [btnAdd addTarget:self action:@selector(btnAddTriggerTouched:) forControlEvents:UIControlEventTouchUpInside];
  268. } else if (section == 2) {
  269. [btnAdd addTarget:self action:@selector(btnAddActionTouched:) forControlEvents:UIControlEventTouchUpInside];
  270. } else if (section == 3) {
  271. _btnPushAdd = btnAdd;
  272. [btnAdd addTarget:self action:@selector(btnAddPushMessageTouched:) forControlEvents:UIControlEventTouchUpInside];
  273. }
  274. }
  275. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  276. UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
  277. if (!view) {
  278. if (section == 0) {
  279. view = [[UIView alloc] init];
  280. } else if (section == 1 || section == 2 || section == 3) {//TODO : hide add button or not;
  281. RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  282. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  283. [view addSubview:hcell];
  284. hcell.lblTitle.text = [self headerTitleForSection:section];
  285. if (![hcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  286. [self addTargetToHeaderAddButton:hcell.btnAdd section:section];
  287. }
  288. } else if (section == 4) {
  289. RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ConditionHeaderCellIdentifier"];
  290. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  291. [view addSubview:hcell];
  292. }
  293. if (_arrayForHeader.count == section) {
  294. [_arrayForHeader insertObject:view atIndex:section];
  295. }
  296. } else {
  297. if (section == 1 || section == 2 || section == 3) {
  298. RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)view.subviews[0];
  299. hcell.lblTitle.text = hcell.lblTitle.text = [self headerTitleForSection:section];
  300. } else if (section == 4) {
  301. RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)view.subviews[0];
  302. }
  303. }
  304. return view;
  305. }
  306. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  307. if (section == 0) {
  308. return 0.01f;
  309. }
  310. return 90.0f;
  311. }
  312. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  313. UIView *view = _arrayForFooter.count > section ? _arrayForFooter[section] : nil;
  314. if (!view) {
  315. RulesAddFooterTableViewCell *hcell = (RulesAddFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FooterCellIdentifier"];
  316. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  317. [view addSubview:hcell];
  318. if (_arrayForHeader.count == section) {
  319. [_arrayForHeader insertObject:view atIndex:section];
  320. }
  321. }
  322. return view;
  323. }
  324. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  325. if (section == 0) {
  326. return 0.01f;
  327. }
  328. return 15.0f;
  329. }
  330. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  331. CGFloat height = 0.0f;
  332. NSInteger section = indexPath.section;
  333. if (section == 0) {//title
  334. height = 50;
  335. } else if (section == 1) {//triggers
  336. height = 95;
  337. } else if (section == 2) {//actions
  338. height = 95;
  339. } else if (section == 3) {//push-message
  340. height = 115;
  341. } else if (section == 4) {//conditions
  342. height = 255;
  343. }
  344. return height;
  345. }
  346. - (void)configureTriggerCell:(RulesAddTableViewCell *)cell item:(ItemModel *)item {
  347. ItemSubModel *subItem = nil;
  348. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  349. subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  350. cell.lblItem.text = item.sourceName;
  351. cell.lblSubItem.text = subItem.sourceSubName;
  352. //노드의 액션 값을 세팅함.
  353. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  354. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  355. cell.lblCondition.text = [DeviceModel contentValueMsgByCmdClsCode:subItem.cmdclsCode cmdclsTypeId:(NSString *)subItem.cmdclsTypeId contentValue:cmdclsValue.cmdclsValue];
  356. } else {
  357. cell.lblCondition.text = subItem.cmdclsValueMsg;
  358. }
  359. cell.lblCondition.hidden = NO;
  360. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
  361. subItem = item.timers && item.timers.count ? item.timers[0] : nil;
  362. if (subItem) {
  363. NSInteger phour = [subItem.hour integerValue];
  364. NSString *period = phour < 12 ? @"AM" : @"PM";//NSLocalizedString(@"오전", @"오전") : NSLocalizedString(@"오후", @"오후");
  365. if ([period isEqualToString:NSLocalizedString(@"오후", @"오후")]) {
  366. if (phour > 12) {
  367. phour -= 12;
  368. }
  369. }
  370. NSString *title = [NSString stringWithFormat:@"%@ %zd:%@", period, phour, subItem.minute];
  371. // NSDictionary *titleColor = @{NSForegroundColorAttributeName : kUITextColor02};
  372. cell.lblItem.text = title;
  373. cell.lblSubItem.text = subItem.daysOfWeek;
  374. cell.lblCondition.hidden = YES;
  375. }
  376. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
  377. subItem = item.daylights && item.daylights.count ? item.daylights[0] : nil;
  378. if (subItem) {
  379. NSString *title = [subItem.sourceId isEqualToString:@"sunriseUtcTime"] ? @"해뜰때" : @"해질때";
  380. cell.lblItem.text = title;
  381. cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@", subItem.sourceName, subItem.daysOfWeek];
  382. cell.lblCondition.hidden = YES;
  383. }
  384. }
  385. [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  386. }
  387. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  388. NSInteger section = indexPath.section;
  389. UITableViewCell *cell = nil;
  390. if (section == 0) {//title
  391. RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  392. if (!_txtRuleTitle) {
  393. _txtRuleTitle = tcell.txtRuleTitle;
  394. }
  395. cell = tcell;
  396. } else if (section == 1) {//triggers
  397. ItemModel *item = _triggers[indexPath.row];
  398. RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
  399. [self configureTriggerCell:tcell item:item];
  400. tcell.btnDelete.value = item;
  401. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  402. [tcell.btnDelete addTarget:self action:@selector(btnDeleteTriggerTouched:) forControlEvents:UIControlEventTouchUpInside];
  403. }
  404. cell = tcell;
  405. } else if (section == 2) {//actions
  406. ItemModel *item = _actions[indexPath.row];
  407. //노드 액션이 선택된 노드만 출력함.
  408. NSInteger fcount = indexPath.row; //선택된 액션만 찾기 위한 인덱스
  409. NSArray *matchedSubItems = [_actions matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
  410. ItemSubModel *subItem = nil;
  411. for (subItem in matchedSubItems) {
  412. if (fcount == 0) {
  413. break;
  414. } else {
  415. fcount--;
  416. }
  417. }
  418. RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
  419. tcell.lblItem.text = item.sourceName;
  420. [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  421. //노드의 액션 값을 세팅함.
  422. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  423. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  424. tcell.lblCondition.text = cmdclsValue.cmdclsValueMsg;
  425. } else {
  426. tcell.lblCondition.text = subItem.cmdclsValueMsg;
  427. }
  428. tcell.btnDelete.value = subItem;
  429. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  430. [tcell.btnDelete addTarget:self action:@selector(btnDeleteActionTouched:) forControlEvents:UIControlEventTouchUpInside];
  431. }
  432. cell = tcell;
  433. } else if (section == 3) {//push-message
  434. RulesAddPushTableViewCell *tcell = (RulesAddPushTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"PushCellIdentifier"];
  435. tcell.txvMessage.delegate = self;
  436. cell = tcell;
  437. } else if (section == 4) {//conditions
  438. RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
  439. cell = tcell;
  440. }
  441. return cell;
  442. }
  443. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  444. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  445. }
  446. #pragma mark - TableView UI Events
  447. - (void)btnAddTriggerTouched:(id)sender {
  448. if (!_triggers) {
  449. _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
  450. }
  451. TriggerSelectPopupView *tpopup = [[TriggerSelectPopupView alloc] initFromNib];
  452. tpopup.refTriggers = _triggers;
  453. tpopup.refDevices = _triggerDevices;
  454. [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  455. if (buttonIndex == 0) {
  456. [_tableView reloadData];
  457. }
  458. }];
  459. }
  460. - (void)btnAddActionTouched:(id)sender {
  461. DeviceSelectPopupView *dpopup = [[DeviceSelectPopupView alloc] initFromNib];
  462. dpopup.refDevices = _actionDevices;
  463. dpopup.typeCode = ksItemTypeCodeTrigger;
  464. [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  465. if (buttonIndex == 0) {//ok
  466. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  467. npopup.refDevice = dpopup.selectedDevices[0];
  468. npopup.typeCode = ksItemTypeCodeTrigger;
  469. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  470. //action add
  471. if (buttonIndex == 0) {//OK
  472. if (!_actions) {
  473. _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
  474. }
  475. npopup.refDevice.itemName = npopup.refDevice.sourceName;
  476. npopup.refDevice.itemSubTypeCode = ksItemSubTypeCodeDevice;
  477. [_actions addObject:npopup.refDevice];
  478. [_tableView reloadData];
  479. }
  480. }];
  481. }
  482. }];
  483. }
  484. - (void)btnAddPushMessageTouched:(id)sender {
  485. if (!_pushes) {
  486. _pushes = [(NSMutableArray<ItemModel> *)[NSMutableArray alloc] init];
  487. }
  488. if (!_pushes.count) {
  489. ItemModel *push = [[ItemModel alloc] init];
  490. push.itemSubTypeCode = ksItemSubTypeCodeAppPush;
  491. [_pushes addObject:push];
  492. }
  493. [_tableView reloadData];
  494. }
  495. - (void)btnDeleteTriggerTouched:(id)sender {
  496. if (_triggers && _triggers.count) {
  497. [_triggers removeAllObjects];
  498. }
  499. [_tableView reloadData];
  500. }
  501. - (void)btnDeleteActionTouched:(id)sender {
  502. CustomButton *btnDelete = (CustomButton *)sender;
  503. ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
  504. //선택 설정을 초기화
  505. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  506. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  507. cmdclsValue.isSelected = NO;
  508. }
  509. //액션과 일치하는 서브아이템을 삭제해줌.
  510. [_actions enumerateObjectsUsingBlock:^(ItemModel *action, NSUInteger idx, BOOL * _Nonnull stop) {
  511. NSArray *matchedArray = [action.subItems matchedArrayByObjectName:ksCustomRadioButtonStatus condition:YES];
  512. if (!matchedArray.count) {
  513. [_actions removeObject:action];
  514. }
  515. }];
  516. [_tableView reloadData];
  517. }
  518. #pragma mark - CustomTextView Delegate
  519. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  520. _btnPushAdd.hidden = textView.text.length > 0;
  521. NSLog(@"%s\n %zd", __PRETTY_FUNCTION__, textView.text.length);
  522. return textView.text.length < 120;
  523. }
  524. #pragma mark - UI Events
  525. - (IBAction)btnConfirmTouched:(id)sender {
  526. [self.view endEditing:YES];
  527. //1.Validate Title
  528. RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; //첫번째 섹션
  529. if (tcell && ![ValidateUtil validateTextfiled:tcell.txtRuleTitle type:ValidateTypeNull title:NSLocalizedString(@"규칙 이름", @"규칙 이름")]) {
  530. return;
  531. }
  532. if (!_tmpRuleDetail) {//생성
  533. [self requestRegisterRule];
  534. } else {//수정
  535. // [self requestModifyRule];
  536. }
  537. }
  538. - (IBAction)btnCancelTouched:(id)sender {
  539. [[JDFacade facade] dismissModalStack:YES completion:nil];
  540. }
  541. #pragma mark - MemoryWarning
  542. - (void)didReceiveMemoryWarning
  543. {
  544. [super didReceiveMemoryWarning];
  545. // Dispose of any resources that can be recreated.
  546. }
  547. @end