RulesAddViewController.m 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  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 "RuleModel.h"
  12. #import "DeviceModel.h"
  13. #import "CustomLabel.h"
  14. #import "CustomButton.h"
  15. #import "CustomImageView.h"
  16. #import "CustomTableView.h"
  17. #import "CustomCheckBox.h"
  18. #import "CustomTextView.h"
  19. #import "CustomTextField.h"
  20. #import "UIImageView+WebCache.h"
  21. #import "ValidateUtil.h"
  22. #import "DeviceSelectPopupView.h"
  23. #import "DeviceNodePopupView.h"
  24. #import "TriggerSelectPopupView.h"
  25. #import "ConditionSelectPopupView.h"
  26. #import "RulesConditionHomeModePopupView.h"
  27. #import "RulesConditionViewController.h"
  28. #import "RulesAddViewController.h"
  29. #import "RulesAddCompleteViewController.h"
  30. #import "RulesDetailViewController.h"
  31. #import "RulesViewController.h"
  32. #import "TimePickerPopupView.h"
  33. #import "DaylightPopupView.h"
  34. #import "ExternHeatPopupView.h"
  35. #import "ActionDeviceCommandPopupView.h"
  36. //규칙이름
  37. @implementation RulesAddTitleTableViewCell
  38. #pragma mark - textfield delegate
  39. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  40. return YES;
  41. }
  42. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  43. NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
  44. BOOL input = text.length == 0;
  45. _btnConfirm.hidden = input;
  46. _btnAdd.hidden = !input;
  47. return YES;
  48. }
  49. @end
  50. //알림 메시지
  51. @interface RulesAddTextTableViewCell() <UITextViewDelegate>
  52. @property (nonatomic, strong) void (^statusChangeBlock)(NSInteger tag, CGFloat height) ;
  53. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *constTxtHeight;
  54. @property CGRect previousRect;
  55. @end
  56. @implementation RulesAddTextTableViewCell
  57. - (void)awakeFromNib {
  58. [super awakeFromNib];
  59. _previousRect = CGRectZero;
  60. _previousRect.origin.y = 11.f;
  61. }
  62. - (void)registTvStatusChangeBlock:(void (^)(NSInteger tag, CGFloat height)) statusChangeBlock {
  63. _statusChangeBlock = statusChangeBlock ;
  64. }
  65. - (void)invokeChangeHeight:(CGFloat)height {
  66. if( _statusChangeBlock != nil )
  67. _statusChangeBlock(1, height) ;
  68. }
  69. #pragma mark - textView delegate
  70. - (void)textViewDidChange:(UITextView *)textView {
  71. UITextPosition* pos = _txtView.endOfDocument;
  72. CGRect currentRect = [_txtView caretRectForPosition:pos];
  73. CGFloat previousY = _previousRect.origin.y;
  74. if (previousY != currentRect.origin.y) {
  75. CGFloat height = currentRect.origin.y - previousY;
  76. _constTxtHeight.constant = _constTxtHeight.constant + height < 48.f ? 48.f : _constTxtHeight.constant + height;
  77. [self invokeChangeHeight:height];
  78. }
  79. _previousRect = currentRect;
  80. }
  81. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
  82. NSString *string = [textView.text stringByReplacingCharactersInRange:range withString:text];
  83. BOOL input = string.length == 0;
  84. _btnConfirm.hidden = input;
  85. _btnAdd.hidden = !input;
  86. return YES;
  87. }
  88. - (void)textViewDidBeginEditing:(UITextView *)textView {
  89. _imgTxtBg.highlighted = YES;
  90. if( _statusChangeBlock != nil )
  91. _statusChangeBlock(0, 0) ;
  92. }
  93. - (void)textViewDidEndEditing:(UITextView *)textView {
  94. _imgTxtBg.highlighted = NO;
  95. }
  96. @end
  97. @implementation RulesAddHeaderTableViewCell
  98. @end
  99. @implementation RulesAddTableViewCell
  100. @end
  101. @implementation RulesAddPushTableViewCell
  102. @end
  103. @implementation RulesAddConditionHeaderTableViewCell
  104. @end
  105. @implementation RulesAddConditionTableViewCell
  106. @end
  107. @implementation RulesAddFooterTableViewCell
  108. @end
  109. @interface RulesAddViewController () <KeyboardFrameChangedDelegate> {
  110. NSMutableArray<ItemModel> *_triggerDevices, *_actionDevices, *_conditionDevices;
  111. BOOL _isNotFirstLoading, _hasCondition;
  112. NSMutableArray *_arrayForHeader, *_arrayForFooter;
  113. CGFloat textCellChangeHeight; //알림 메세지 셀 사이즈 변동
  114. CustomTextField *_txtRuleTitle;
  115. CustomTextView *_txvMessage;
  116. CustomButton *_btnTriggerAdd, *_btnPushAdd;
  117. CustomCheckBox *_chkConditions;
  118. RulesConditionHomeModePopupView *_mpopup;
  119. RulesConditionViewController *_rcv;
  120. __weak IBOutlet NSLayoutConstraint *constTableBottom;
  121. }
  122. @property (strong, nonatomic) NSMutableArray<ItemModel> *triggers;
  123. @property (strong, nonatomic) NSMutableArray<ItemModel> *actions;
  124. @property (strong, nonatomic) NSMutableArray<ItemModel> *conditions;
  125. @end
  126. @implementation RulesAddViewController
  127. - (void)viewDidLoad {
  128. [super viewDidLoad];
  129. [self initProperties];
  130. [self initUI];
  131. [self prepareViewDidLoad];
  132. }
  133. - (void)viewWillAppear:(BOOL)animated {
  134. [super viewWillAppear:animated];
  135. [self addObserverKeyboardFrameChanged:self] ;
  136. }
  137. - (void)viewWillDisappear:(BOOL)animated {
  138. [super viewWillDisappear:animated];
  139. [self removeObserverKeyboard] ;
  140. }
  141. #pragma mark - Keyboard Frame Changed Delegate
  142. - (void)keyboardFrameChanged:(CGRect)frame duration:(CGFloat)duration display:(BOOL)display {
  143. [UIView animateWithDuration:duration animations:^{
  144. if( display ) {
  145. constTableBottom.constant = frame.size.height;
  146. }
  147. else {
  148. constTableBottom.constant = 50.0f;
  149. }
  150. [self.view layoutIfNeeded];
  151. }] ;
  152. }
  153. #pragma mark - init
  154. - (void)initProperties {
  155. _arrayForHeader = [[NSMutableArray alloc] init];
  156. _arrayForFooter = [[NSMutableArray alloc] init];
  157. if (!_triggers) {
  158. _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
  159. }
  160. if (!_actions) {
  161. _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
  162. }
  163. if (!_conditions) {
  164. _conditions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
  165. }
  166. }
  167. - (void)initUI {
  168. [self initTableViewAsDefaultStyle:_tableView];
  169. if (_refRuleDetail) {
  170. _lblTitle.text = @"규칙 편집";
  171. }
  172. }
  173. - (void)prepareViewDidLoad {
  174. [self requestDeviceListForAction:ksItemTypeCodeTrigger];
  175. [self requestDeviceListForAction:ksItemTypeCodeAction];
  176. }
  177. //말줄임표 conv
  178. - (NSString*)convMaxLengthName:(NSString*)name len:(NSInteger)len {
  179. if (name.length > len) {
  180. return [NSString stringWithFormat:@"%@..", [name substringToIndex:len]];
  181. }
  182. return name;
  183. }
  184. #pragma mark - Main Logic
  185. - (void)requestDeviceListForAction:(NSString *)typeCode {
  186. //parameters
  187. // NSDictionary *parameter = @{@"item_type_code": typeCode};
  188. NSArray *arr = @[typeCode];
  189. // NSString *path = [NSString stringWithFormat:API_GET_ITEM_DEVICES, ksItemTypeCodeAction];
  190. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_GET_ITEM_DEVICES arguments:arr];
  191. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ItemListModel class] completion:^(id responseObject) {
  192. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  193. return;
  194. }
  195. ItemListModel *fetchedItemList = (ItemListModel *)responseObject;
  196. if (fetchedItemList && fetchedItemList.list && fetchedItemList.list.count) {//API 성공 ,
  197. if ([typeCode isEqualToString:ksItemTypeCodeAction]) {
  198. _actionDevices = fetchedItemList.list;
  199. if (_refRuleDetail) {//편집일 경우,
  200. ItemModel *actionDevice = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  201. [self matchValueOfDevices:actionDevice devices:_actionDevices];
  202. }
  203. } else {
  204. _triggerDevices = fetchedItemList.list;
  205. _conditionDevices = [(NSMutableArray<ItemModel> *)[NSMutableArray alloc] initWithArray:_triggerDevices copyItems:YES];
  206. if (_refRuleDetail) {//편집일 경우,
  207. ItemModel *triggerDevice = [_triggers objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  208. ItemModel *conditionDevice = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  209. [self matchValueOfDevices:triggerDevice devices:_triggerDevices];
  210. [self matchValueOfDevices:conditionDevice devices:_conditionDevices];
  211. }
  212. [self requestDeviceListForAction:ksItemTypeCodeAction];
  213. }
  214. }
  215. [_tableView reloadData];
  216. } failure:^(id errorObject) {
  217. JDErrorModel *error = (JDErrorModel *)errorObject;
  218. [[JDFacade facade] alert:error.errorMessage];
  219. }];
  220. }
  221. - (void)matchValueOfDevices:(ItemModel *)deviceItem devices:(NSMutableArray<ItemModel> *)devices {
  222. for (ItemSubModel *subItem in deviceItem.subItems) {
  223. NSArray *matchedArray = [devices matchedArrayInSubArrays:@"subItems" predicateFormat:@"sourceId == %@ && sourceSubId == %@", subItem.sourceId, subItem.sourceSubId];
  224. if (matchedArray.count) {
  225. ItemSubModel *matchedSubItem = matchedArray.firstObject;
  226. [[JDFacade facade] setRadioButtonStatus:@YES object:matchedSubItem];
  227. CmdClsValueModel *pCmdClsValue = [matchedSubItem.cmdclsValueList objectKey:@"cmdclsValue" eqaulToString:subItem.cmdclsValue];
  228. [[JDFacade facade] setRadioButtonStatus:@YES object:pCmdClsValue];
  229. }
  230. }
  231. }
  232. - (NSArray<ItemModel> *)makePushData{
  233. ItemModel *model = [ItemModel new];
  234. model.itemSubTypeCode = @"03";
  235. model.itemName = @"Notification message";
  236. NSMutableArray *pushes = [NSMutableArray new];
  237. ItemSubModel *subModel = [ItemSubModel new];
  238. subModel.conditionTypeCode = @"02";
  239. subModel.cmdclsValue = _txvMessage.text;
  240. subModel.actionTypeCode = @"00";
  241. [pushes addObject:subModel];
  242. [model.pushes arrayByAddingObjectsFromArray:pushes];
  243. NSArray *itemModels = @[model];
  244. return (NSArray<ItemModel> *)itemModels;
  245. //수정
  246. // if (addPush && _txvMessage.text.length > 0) {
  247. //
  248. // NSMutableArray *rSubItems = [[NSMutableArray alloc] init];
  249. // NSDictionary *rSubItem = @{@"condition_type_code": @"01",
  250. // @"cmdcls_value": @"푸시알림"};
  251. //
  252. // [rSubItems addObject:rSubItem];
  253. // [subItems addObjectsFromArray:rSubItems];
  254. // }
  255. }
  256. - (void)requestRegisterRule {
  257. NSArray *triggers = [self items:_triggers];
  258. NSArray *actions = [self items:_actions];
  259. //추가 푸시
  260. if (_txvMessage.text.length > 0) {
  261. NSMutableArray *mutableActions = [actions mutableCopy];
  262. [mutableActions addObjectsFromArray:[self items:[self makePushData]]];
  263. actions = [NSArray arrayWithArray:mutableActions];
  264. }
  265. NSArray *conditions = [self conditions:_conditions];
  266. //validate
  267. if (!triggers || !triggers.count) {
  268. [[JDFacade facade] alert:NSLocalizedString(@"규칙을 실행할 조건을 설정하세요.", @"규칙을 실행할 조건을 설정하세요.")];
  269. return;
  270. }
  271. if (!actions || !actions.count) {
  272. [[JDFacade facade] alert:NSLocalizedString(@"실행 조건에 맞춰 동작할 장치나 알림 메시지 중 최소 하나 이상을 설정하세요.", @"실행 조건에 맞춰 동작할 장치나 알림 메시지 중 최소 하나 이상을 설정하세요.")];
  273. return;
  274. }
  275. //parameters
  276. NSMutableDictionary *parameter = [NSMutableDictionary
  277. dictionaryWithDictionary:@{@"rule_name" : _txtRuleTitle.text,
  278. @"use_yn" : ksYES,
  279. @"triggers": triggers ? triggers : [NSNull null],
  280. @"actions": actions ? actions : [NSNull null]}];
  281. if (conditions) [parameter setObject:conditions forKey:@"conditions"];
  282. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_POST_RULE_ADD aditional:@""];
  283. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path
  284. parameters:parameter
  285. modelClass:[RuleModel class]
  286. completion:^(id responseObject) {
  287. if (!responseObject) return;
  288. RuleModel *newRule = (RuleModel *) responseObject;
  289. if (newRule) {//API 성공 ,
  290. ItemModel *item = _triggers.firstObject;
  291. if (item) {
  292. newRule.ruleName = _txtRuleTitle.text;
  293. newRule.triggerItemSubTypeCode = item.itemSubTypeCode;
  294. }
  295. RulesAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesAddCompleteViewController" storyboardName:@"Rules"];
  296. vc.refRule = newRule;
  297. vc.providesPresentationContextTransitionStyle = YES;
  298. vc.definesPresentationContext = YES;
  299. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  300. [self presentViewController:vc animated:YES completion:nil];
  301. }
  302. } failure:^(id errorObject) {
  303. JDErrorModel *error = (JDErrorModel *)errorObject;
  304. [[JDFacade facade] alert:error.errorMessage];
  305. }];
  306. }
  307. - (void)requestModifyRule {
  308. NSArray *triggers = [self items:_triggers];
  309. NSArray *actions = [self items:_actions];
  310. NSArray *conditions = [self conditions:_conditions];
  311. // BOOL hasConditionForRegister = [_conditions matchedArrayInSubArrays:@"subItems" predicateFormat:@"conditionTypeCode == %@", @"09"].count || _chkConditions.checked;
  312. // NSArray *conditions = hasConditionForRegister ? [self conditions:_conditions] : nil;
  313. //validate
  314. if (!triggers || !triggers.count) {
  315. [[JDFacade facade] alert:NSLocalizedString(@"실행할 때를 선택하세요", @"실행할 때를 선택하세요")];
  316. return;
  317. }
  318. if (!actions || !actions.count) {
  319. [[JDFacade facade] alert:NSLocalizedString(@"실행할 장치를 선택하세요", @"실행할 장치를 선택하세요")];
  320. return;
  321. }
  322. //parameters
  323. NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"rule_name" : _txtRuleTitle.text,
  324. @"use_yn" : ksYES,
  325. @"triggers": triggers ? triggers : [NSNull null],
  326. @"actions": actions ? actions : [NSNull null]}];
  327. if (conditions) {
  328. [parameter setObject:conditions forKey:@"conditions"];
  329. }
  330. NSArray *arr = @[_refRuleDetail.ruleId];
  331. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_PUT_RULE_MODIFY arguments:arr];
  332. [[RequestHandler handler] sendAsyncPutRequestAPIPath:path parameters:parameter modelClass:[RuleModel class] completion:^(id responseObject) {
  333. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  334. return;
  335. }
  336. RuleModel *rule = (RuleModel *) responseObject;
  337. if (rule) {//API 성공
  338. RulesDetailViewController *vc = [[JDFacade facade] viewControllerOnPresentingViewController:self viewControllerClass:[RulesDetailViewController class]];
  339. if (vc) {
  340. [vc requestRuleDetail];
  341. } else {
  342. RulesViewController *rvc = [[JDFacade facade] viewControllerOnPresentingViewController:self viewControllerClass:[RulesViewController class]];
  343. [rvc prepareViewDidLoad];
  344. }
  345. [self dismissViewControllerAnimated:YES completion:^{
  346. [[JDFacade facade] toast:@"편집을 완료합니다"];
  347. }];
  348. }
  349. } failure:^(id errorObject) {
  350. JDErrorModel *error = (JDErrorModel *)errorObject;
  351. [[JDFacade facade] alert:error.errorMessage];
  352. }];
  353. }
  354. //아이템 배열을 리턴함.
  355. - (NSArray *)items:(NSArray<ItemModel> *)items{
  356. //triggers, actions, conditions
  357. NSMutableArray *rItems = [[NSMutableArray alloc] init];
  358. for (ItemModel *item in items) {
  359. NSDictionary *dic = nil;
  360. NSMutableArray *subItems = [[NSMutableArray alloc] init];
  361. NSArray *pdevices = [self subItemsForType:item.subItems itemSubType:ksItemSubTypeCodeDevice];
  362. NSArray *pushes = [self subItemsForType:item.pushes itemSubType:ksItemSubTypeCodeAppPush];
  363. NSArray *timers = [self subItemsForType:item.timers itemSubType:ksItemSubTypeCodeTimer];
  364. NSArray *daylights = [self subItemsForType:item.daylights itemSubType:ksItemSubTypeCodeDaylight];
  365. NSArray *heats = [self subItemsForType:item.heats itemSubType:ksItemSubTypeCodeHeat];
  366. if (pdevices && pdevices.count) {//device
  367. [subItems addObjectsFromArray:pdevices];
  368. }
  369. if (pushes && pushes.count) {//push
  370. [subItems addObjectsFromArray:pushes];
  371. }
  372. if (timers && timers.count) {//timer
  373. [subItems addObjectsFromArray:timers];
  374. }
  375. if (daylights && daylights.count) {//해뜰때/질때
  376. [subItems addObjectsFromArray:daylights];
  377. }
  378. if (heats && heats.count) {//더울떄 / 추울때
  379. [subItems addObjectsFromArray:heats];
  380. }
  381. if (subItems.count) {
  382. dic = @{@"item_name": item.itemName,
  383. @"item_sub_type_code": item.itemSubTypeCode,
  384. @"item_sub": subItems};
  385. [rItems addObject:dic];
  386. NSLog(@"dic : %@ %@", dic, item.itemName);
  387. }
  388. }
  389. return rItems;
  390. }
  391. //서브아이템 배열을 리턴함.
  392. - (NSArray *)subItemsForType:(NSArray *)subItems itemSubType:(NSString *)itemSubTypeCode {
  393. NSMutableArray *rSubItems = [NSMutableArray new];
  394. if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  395. NSArray<ItemSubModel> *pdevices = (NSArray<ItemSubModel> *)[subItems matchedArrayByObjectName:ksCustomRadioButtonStatus condition:YES];
  396. for (ItemSubModel *pdevice in pdevices) {
  397. NSDictionary *rSubItem = @{@"source_id": pdevice.sourceId,
  398. @"source_sub_id": pdevice.sourceSubId,
  399. @"cmdcls_value": pdevice.cmdclsValue,
  400. @"condition_type_code": pdevice.conditionTypeCode,
  401. @"data_type_code": pdevice.dataTypeCode};
  402. [rSubItems addObject:rSubItem];
  403. }
  404. }
  405. else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
  406. //at once.
  407. for (ItemSubModel *subItem in subItems) {
  408. NSDictionary *rSubItem = @{@"condition_type_code": subItem.conditionTypeCode,
  409. @"cmdcls_value": subItem.cmdclsValue,
  410. @"action_type_code": subItem.actionTypeCode};
  411. [rSubItems addObject:rSubItem];
  412. }
  413. }
  414. else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  415. for (ItemSubModel *subItem in subItems) {
  416. NSDictionary *rSubItem = @{@"hour": subItem.hour,
  417. @"minute": subItem.minute,
  418. @"dayofweek": subItem.dayofweek};
  419. [rSubItems addObject:rSubItem];
  420. }
  421. }
  422. else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 일 경우,
  423. for (ItemSubModel *subItem in subItems) {
  424. NSDictionary *rSubItem = @{@"source_id": subItem.sourceId,
  425. @"source_sub_id": subItem.sourceSubId,
  426. @"dayofweek": subItem.dayofweek};
  427. [rSubItems addObject:rSubItem];
  428. }
  429. }
  430. else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//온도
  431. for (ItemSubModel *subItem in subItems) {
  432. NSDictionary *rSubItem = @{@"source_id": subItem.sourceId,
  433. @"condition_type_code": subItem.conditionTypeCode,
  434. @"cmdcls_value": subItem.cmdclsValue,
  435. @"dayofweek": subItem.dayofweek};
  436. [rSubItems addObject:rSubItem];
  437. }
  438. }
  439. return rSubItems;
  440. }
  441. - (NSArray *)conditions:(NSArray<ItemModel> *)items {//trigger만 해당됨.
  442. //conditions
  443. NSMutableArray *rConditions = [[NSMutableArray alloc] init];
  444. for (ItemModel *condition in items) {//item = condition
  445. NSMutableArray *rSubItems = [[NSMutableArray alloc] init];
  446. if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeHomeMode]) {
  447. for (ItemSubModel *mode in condition.subItems) {
  448. NSDictionary *rSubItem = @{@"source_id": mode.sourceId,
  449. @"cmdcls_value": mode.cmdclsValue,
  450. @"condition_type_code": mode.conditionTypeCode};
  451. [rSubItems addObject:rSubItem];
  452. }
  453. }
  454. else if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDueDate]) {
  455. for (ItemSubModel *subItem in condition.subItems) {
  456. NSDictionary *rSubItem = @{@"condition_type_code": subItem.conditionTypeCode,
  457. @"cmdcls_value": subItem.cmdclsValue};
  458. [rSubItems addObject:rSubItem];
  459. }
  460. }
  461. else if ([condition.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  462. for (ItemSubModel *pdevice in condition.subItems) {
  463. NSDictionary *rSubItem = @{@"source_id": pdevice.sourceId,
  464. @"source_sub_id": pdevice.sourceSubId,
  465. @"cmdcls_value": pdevice.cmdclsValue,
  466. @"data_type_code": pdevice.dataTypeCode,
  467. @"condition_type_code": pdevice.conditionTypeCode};
  468. [rSubItems addObject:rSubItem];
  469. }
  470. }
  471. else if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
  472. for (ItemSubModel *subItem in condition.subItems) {
  473. NSDictionary *rSubItem = @{@"cmdcls_value": subItem.cmdclsValue,
  474. @"condition_type_code": subItem.conditionTypeCode};
  475. [rSubItems addObject:rSubItem];
  476. }
  477. }
  478. if (rSubItems.count) {
  479. NSDictionary *dic = @{@"item_name": condition.itemName,
  480. @"item_sub_type_code": condition.itemSubTypeCode,
  481. @"item_sub": rSubItems};
  482. [rConditions addObject:dic];
  483. NSLog(@"dic : %@ %@", dic, condition.itemName);
  484. }
  485. }
  486. return rConditions;
  487. }
  488. - (void)cloneSubItemsForItemSubType:(NSArray<ItemModel> *)items {
  489. for (ItemModel *item in items) {
  490. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  491. //기존 아이템을 그대로 사용함.
  492. for (ItemSubModel *subItem in item.subItems) {
  493. [[JDFacade facade] setRadioButtonStatus:@YES object:subItem];
  494. [[JDFacade facade] setCheckBoxStatus:@YES object:subItem];
  495. }
  496. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
  497. item.pushes = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
  498. item.subItems = nil;
  499. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  500. item.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
  501. item.subItems = nil;
  502. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
  503. item.daylights = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
  504. item.subItems = nil;
  505. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
  506. item.heats = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
  507. item.subItems = nil;
  508. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeMode]) {//더울때/추울때 경우,
  509. //기존 아이템을 그대로 사용함.
  510. }
  511. }
  512. }
  513. - (void)setRefRuleDetail:(RuleDetailModel *)refRuleDetail {
  514. _refRuleDetail = refRuleDetail;
  515. _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.triggers copyItems:YES];
  516. _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.actions copyItems:YES];
  517. _conditions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.conditions copyItems:YES];
  518. [self cloneSubItemsForItemSubType:_triggers];
  519. [self cloneSubItemsForItemSubType:_actions];
  520. [self cloneSubItemsForItemSubType:_conditions];
  521. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
  522. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDueDate] ? YES : _hasCondition;
  523. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice] ? YES : _hasCondition;
  524. }
  525. #pragma mark - UITableView DataSource & Delegate
  526. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  527. return 5;
  528. }
  529. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  530. NSInteger count = 0;
  531. if (section == 0) {//title
  532. count = 1;
  533. } else if (section == 1) {//triggers
  534. count = _triggers && _triggers.count ? _triggers.count : 0;
  535. } else if (section == 2) {//actions
  536. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  537. count = deviceItem.subItems && deviceItem.subItems.count ? deviceItem.subItems.count : 0;
  538. } else if (section == 3) {//push-message
  539. count = 1;
  540. } else if (section == 4) {//conditions
  541. count = 1;//_chkConditions.checked || _hasCondition ? 1 : 0;
  542. }
  543. return count;
  544. }
  545. - (NSString *)headerTitleForSection:(NSInteger)section {
  546. NSString *title = nil;
  547. if (section == 1) {//trigger
  548. title = @"실행 조건";
  549. if (_triggers && _triggers.count) {
  550. ItemModel *item = _triggers[0];
  551. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  552. title = @"장치 상태가 바뀔 때";
  553. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
  554. title = @"이 시간마다";
  555. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
  556. title = @"더울때 / 추울때";
  557. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
  558. title = @"해뜰때 / 질때";
  559. }
  560. }
  561. } else if (section == 2) {//action
  562. title = @"동작할 장치";
  563. // title = _actions && _actions.count ? @"선택됨" : title;
  564. }
  565. // else if (section == 3) {//conditions
  566. // title = @"추가 조건";
  567. // title = _triggers && triggers.count ? @"선택됨" : title;
  568. // }
  569. return title;
  570. }
  571. - (void)addTargetToHeaderAddButton:(CustomButton *)btnAdd section:(NSInteger)section {
  572. if (section == 1) {
  573. _btnTriggerAdd = btnAdd;
  574. [btnAdd addTarget:self action:@selector(btnAddTriggerTouched:) forControlEvents:UIControlEventTouchUpInside];
  575. } else if (section == 2) {
  576. [btnAdd addTarget:self action:@selector(btnAddActionTouched:) forControlEvents:UIControlEventTouchUpInside];
  577. }
  578. }
  579. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  580. UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
  581. if (!view) {
  582. if (section == 0 || section == 3) {//title
  583. view = [[UIView alloc] init];
  584. }
  585. else if (section == 1 || section == 2) {//hide add button or not;
  586. RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  587. hcell.width = IPHONE_WIDTH;
  588. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  589. [view addSubview:hcell];
  590. hcell.lblTitle.text = [self headerTitleForSection:section];
  591. if (![hcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  592. [self addTargetToHeaderAddButton:hcell.btnAdd section:section];
  593. }
  594. }
  595. else if (section == 4) {//conditions
  596. RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ConditionHeaderCellIdentifier"];
  597. hcell.width = IPHONE_WIDTH;
  598. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  599. [view addSubview:hcell];
  600. _chkConditions = hcell.chkConditions;
  601. if (!_isNotFirstLoading) {
  602. _chkConditions.checked = _hasCondition;
  603. _isNotFirstLoading = YES;
  604. }
  605. if (![_chkConditions actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  606. [_chkConditions addTarget:self action:@selector(chkConditionsTouched:) forControlEvents:UIControlEventTouchUpInside];
  607. }
  608. if (![hcell.btnConditions actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  609. [hcell.btnConditions addTarget:self action:@selector(btnConditionsTouched:) forControlEvents:UIControlEventTouchUpInside];
  610. }
  611. }
  612. if (_arrayForHeader.count == section) {
  613. [_arrayForHeader insertObject:view atIndex:section];
  614. }
  615. } else {
  616. if (section == 1 || section == 2) {
  617. RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)view.subviews[0];
  618. hcell.lblTitle.text = hcell.lblTitle.text = [self headerTitleForSection:section];
  619. }
  620. else if (section == 4) {
  621. // RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)view.subviews[0];
  622. }
  623. }
  624. [self checkHeaderButton:section];
  625. return view;
  626. }
  627. - (void)checkHeaderButton:(NSInteger)section {
  628. if (section == 1) {
  629. _btnTriggerAdd.hidden = self.triggers.count > 0;
  630. }
  631. }
  632. #pragma mark - conditions
  633. //rule controll vc section 4 활성화 비활성화
  634. - (void)chkConditionsTouched:(id)sender {
  635. [_tableView reloadData];
  636. CustomCheckBox *chkCondition = (CustomCheckBox *)sender;
  637. if (chkCondition.checked) {
  638. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:4];
  639. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  640. }
  641. }
  642. - (void)btnConditionsTouched:(id)sender {
  643. ConditionSelectPopupView *popup = [[ConditionSelectPopupView alloc] initFromNib];
  644. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  645. switch (buttonIndex) {
  646. case 10:
  647. [_rcv btnAddModeTouched:nil];
  648. break;
  649. case 11:
  650. [_rcv btnAddDurationTouched:nil];
  651. break;
  652. case 12:
  653. [_rcv btnAddDeviceTouched:nil];
  654. break;
  655. }
  656. }];
  657. };
  658. //- (void)btnAddModeTouched:(id)sender {
  659. //
  660. // ItemModel *mode = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
  661. // BOOL isNewTrigger = mode == nil;
  662. // mode = isNewTrigger ? [[ItemModel alloc] init] : mode;
  663. //
  664. // if (!_mpopup) {
  665. // _mpopup = [[RulesConditionHomeModePopupView alloc] initFromNib];
  666. // }
  667. //
  668. // _mpopup.condition = mode;
  669. // [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  670. // if (buttonIndex == 0) {//OK
  671. // if (isNewTrigger) {
  672. // [_conditions addObject:mode];
  673. // }
  674. //
  675. // [self.tableView reloadData];
  676. // }
  677. // }];
  678. //}
  679. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  680. if (section == 0 || section == 3) {
  681. return 0.01f;
  682. }
  683. else if (section == 4) {
  684. return 56.0f;
  685. }
  686. return 56.0f;
  687. }
  688. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  689. UIView *view = _arrayForFooter.count > section ? _arrayForFooter[section] : nil;
  690. if (!view) {
  691. RulesAddFooterTableViewCell *hcell = (RulesAddFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FooterCellIdentifier"];
  692. hcell.width = IPHONE_WIDTH;
  693. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  694. [view addSubview:hcell];
  695. if (_arrayForHeader.count == section) {
  696. [_arrayForHeader insertObject:view atIndex:section];
  697. }
  698. }
  699. return view;
  700. }
  701. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  702. // if (section == 0) {
  703. // return 0.01f;
  704. // }
  705. return 0.01f;
  706. }
  707. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  708. CGFloat height = 0.0f;
  709. NSInteger section = indexPath.section;
  710. if (section == 0) {//title
  711. height = 122;
  712. } else if (section == 1) {//triggers
  713. height = 80;
  714. } else if (section == 2) {//actions
  715. height = 80;
  716. } else if (section == 3) {//push-message
  717. // height = UITableViewAutomaticDimension;
  718. height = 122 + textCellChangeHeight;
  719. } else if (section == 4) {//conditions
  720. if (!_rcv) {
  721. _rcv = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesConditionViewController" storyboardName:@"Rules"];
  722. [self addChildViewController:_rcv];
  723. [_rcv didMoveToParentViewController:self];
  724. }
  725. height = _rcv.tableHeight;
  726. }
  727. return height;
  728. }
  729. + (BOOL)hasDeleteNode:(ItemModel *)deviceItem {
  730. NSArray *matchedArray = [deviceItem.subItems filteredArrayUsingPredicateFormat:@"deleteYn == %@", ksYES];
  731. return matchedArray.count;
  732. }
  733. - (void)configureTriggerCell:(RulesAddTableViewCell *)cell item:(ItemModel *)item {
  734. ItemSubModel *subItem = nil;
  735. NSString *deviceImageFileName = nil;
  736. NSLog(@"configureTriggerCell");
  737. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//장치일 경우
  738. subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  739. cell.lblItem.text = subItem.sourceName;
  740. deviceImageFileName = subItem.imageFileName;
  741. if ([subItem.deleteYn boolValue]) {
  742. [cell.lblItem setStrikethrough:cell.lblItem.text];
  743. }
  744. cell.lblSubItem.text = subItem.sourceSubName;
  745. //노드의 액션 값을 세팅함.
  746. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  747. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  748. cell.lblCondition.text = [DeviceModel contentValueMsgByCmdClsCode:subItem.cmdclsCode cmdclsTypeId:(NSString *)subItem.cmdclsTypeId contentValue:cmdclsValue.cmdclsValue];
  749. }
  750. else {
  751. if (subItem.cmdclsValueMsg) {
  752. cell.lblCondition.text = subItem.cmdclsValueMsg;
  753. } else {
  754. NSString *condition = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
  755. cell.lblCondition.text = [NSString stringWithFormat:@"%@%@ %@", subItem.cmdclsValue, subItem.unit, condition];
  756. }
  757. }
  758. //[cell.lblCondition setUnderLine:cell.lblCondition.text];
  759. cell.lblCondition.hidden = NO;
  760. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
  761. subItem = item.timers && item.timers.count ? item.timers[0] : nil;
  762. if (subItem) {
  763. NSInteger phour = [subItem.hour integerValue];
  764. NSString *period = phour < 12 ? @"오전" : @"오후";
  765. NSString *title = [NSString stringWithFormat:@"%@ %zd:%@", period, phour, subItem.minute];
  766. // NSDictionary *titleColor = @{NSForegroundColorAttributeName : kUITextColor02};
  767. cell.lblItem.text = title;
  768. cell.lblSubItem.text = subItem.dayofweek;//[self daysOfWeekAtSubItem];
  769. cell.lblCondition.hidden = YES;
  770. }
  771. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
  772. subItem = item.daylights && item.daylights.count ? item.daylights[0] : nil;
  773. if (subItem) {
  774. NSString *title = [subItem.sourceSubId isEqualToString:@"sunriseUtcTime"] ? @"해뜰때" : @"해질때";
  775. cell.lblItem.text = title;
  776. cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@", subItem.sourceName, subItem.dayofweek];//[self daysOfWeekAtSubItem]];
  777. cell.lblCondition.hidden = YES;
  778. }
  779. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
  780. subItem = item.heats && item.heats.count ? item.heats[0] : nil;
  781. if (subItem) {
  782. NSString *title = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"더울때" : @"추울때";
  783. cell.lblItem.text = title;
  784. cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@℃ / %@", subItem.sourceName, subItem.cmdclsValue, subItem.dayofweek];//[self daysOfWeekAtSubItem]];
  785. cell.lblCondition.hidden = YES;
  786. }
  787. }
  788. // if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  789. // if (item.imageFileName) {
  790. // [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  791. // } else {
  792. // [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:deviceImageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  793. // }
  794. // } else {
  795. cell.imgvIcon.image = [self imageForTrigger:item];
  796. // }
  797. }
  798. - (NSString *)daysOfWeekAtSubItem {
  799. NSString *daysOfWeek = ksEmptyString;
  800. ItemModel *daysCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDaysOfWeek];
  801. if (daysCondition) {
  802. ItemSubModel *subCondition = daysCondition.subItems[0];
  803. daysOfWeek = subCondition.dayofweek;
  804. }
  805. return daysOfWeek;
  806. }
  807. - (UIImage *)imageForTrigger:(ItemModel *)item {
  808. UIImage *image = nil;
  809. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  810. image = [UIImage imageNamed:@"img_rule_icon_time"];
  811. }
  812. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
  813. image = [UIImage imageNamed:@"img_rule_icon_sunrises"];
  814. }
  815. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
  816. image = [UIImage imageNamed:@"img_rule_icon_Temperature"];
  817. }
  818. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  819. ItemSubModel *subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  820. image = [UIImage imageNamed:[[JDFacade facade] deviceIconFromSourceSubId:subItem.sourceSubId]];
  821. }
  822. return image;
  823. }
  824. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  825. NSInteger section = indexPath.section;
  826. UITableViewCell *cell = nil;
  827. if (section == 0) {//title
  828. RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  829. tcell.txtRuleTitle.text = [tcell.txtRuleTitle.text isEmptyString] && _refRuleDetail ? _refRuleDetail.ruleName : tcell.txtRuleTitle.text;
  830. if (!_txtRuleTitle) {
  831. _txtRuleTitle = tcell.txtRuleTitle;
  832. }
  833. [self insertActionAddButton:tcell.btnAdd];
  834. cell = tcell;
  835. } else if (section == 1) {//triggers
  836. ItemModel *item = _triggers[indexPath.row];
  837. RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
  838. [self configureTriggerCell:tcell item:item];
  839. tcell.lblItem.textColor = kUITextColor04;
  840. tcell.btnDelete.value = item;
  841. [tcell.btnDelete removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
  842. [tcell.btnDelete addTarget:self action:@selector(btnDeleteTriggerTouched:) forControlEvents:UIControlEventTouchUpInside];
  843. cell = tcell;
  844. } else if (section == 2) {//actions
  845. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  846. ItemSubModel *subItem = deviceItem.subItems[indexPath.row];
  847. RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
  848. //title
  849. BOOL multiHub = [JDFacade facade].loginUser.isMultiHomeHub;
  850. NSArray *ids = [subItem.sourceId componentsSeparatedByString:@"-"];
  851. DeviceModel *hub = nil;
  852. if (ids.count > 1) hub = [[JDFacade facade].loginUser getHomeHub:[ids objectAtIndex:0]];
  853. NSString *name = hub == nil ? @"전체" : (hub.prdUserName != nil ? hub.prdUserName : hub.prdName);
  854. NSString *hubName = multiHub ? [self convMaxLengthName:name len:4] : @"";
  855. NSString *deviceName = [self convMaxLengthName:subItem.sourceName len:6];
  856. tcell.lblItem.text = multiHub ? [NSString stringWithFormat:@"[%@]%@", hubName,deviceName] : deviceName ;
  857. if ([subItem.deleteYn boolValue]) {
  858. [tcell.lblItem setStrikethrough:tcell.lblItem.text];
  859. }
  860. tcell.lblSubItem.text = subItem.sourceSubName;
  861. // tcell.lblItem.textColor = kUITextColor01;
  862. tcell.imgvIcon.image = [UIImage imageNamed:[[JDFacade facade] deviceIconFromSourceSubId:subItem.sourceSubId]];
  863. // [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  864. //노드의 액션 값을 세팅함.
  865. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  866. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  867. tcell.lblCondition.text = cmdclsValue.cmdclsValueMsg;
  868. }
  869. else {
  870. if (subItem.cmdclsValueMsg) {
  871. tcell.lblCondition.text = subItem.cmdclsValueMsg;
  872. }
  873. else {
  874. NSString *condition = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
  875. tcell.lblCondition.text = [NSString stringWithFormat:@"%@%@ %@", subItem.cmdclsValue, subItem.unit, condition];
  876. }
  877. }
  878. tcell.btnDelete.value = subItem;
  879. [tcell.btnDelete removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
  880. [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceActionTouched:) forControlEvents:UIControlEventTouchUpInside];
  881. cell = tcell;
  882. } else if (section == 3) {//push-message
  883. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  884. ItemSubModel *subItem = pushItem.pushes[indexPath.row];
  885. RulesAddTextTableViewCell *tcell = (RulesAddTextTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TextCellIdentifier"];
  886. //높이변경
  887. [tcell registTvStatusChangeBlock:^(NSInteger tag, CGFloat height) {
  888. if (tag == 0) {
  889. [tableView scrollToRowAtIndexPath:indexPath
  890. atScrollPosition:UITableViewScrollPositionTop
  891. animated:NO];
  892. }
  893. else {
  894. textCellChangeHeight += height;
  895. [self tableView:_tableView reloadRowWhileShowingKeyboard:indexPath];
  896. }
  897. }];
  898. if (!_txvMessage) {
  899. _txvMessage = tcell.txtView;
  900. }
  901. _txvMessage.text = [_txvMessage.text isEqualToString:_txvMessage.placeHolder] && _refRuleDetail ? subItem.cmdclsValue : _txvMessage.text;
  902. [self insertActionAddButton:tcell.btnAdd];
  903. cell = tcell;
  904. } else if (section == 4) {//conditions
  905. RulesAddConditionTableViewCell *tcell = (RulesAddConditionTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
  906. // if (_chkConditions.checked) {
  907. if (!tcell.container.subviews.count) {
  908. if ([self isViewLoaded]) {
  909. [_rcv beginAppearanceTransition:YES animated:NO];
  910. [tcell.container addSubview:_rcv.view];
  911. [_rcv endAppearanceTransition];
  912. }
  913. }
  914. // NSLog(@"section == 4");
  915. // NSLog(@"_conditionDevices : %@", _conditionDevices);
  916. _rcv.conditions = _conditions;
  917. _rcv.refDevices = _conditionDevices;
  918. tcell.constraintContainerHeight.constant = _rcv.tableHeight;
  919. // }
  920. cell = tcell;
  921. }
  922. return cell;
  923. }
  924. //키보드 유지 셀 높이 변경
  925. - (void)tableView:(UITableView *)tableView reloadRowWhileShowingKeyboard:(NSIndexPath *)indexPath {
  926. [tableView beginUpdates];
  927. [tableView endUpdates];
  928. [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
  929. }
  930. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  931. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  932. if (indexPath.section == 1) {//triggers
  933. ItemModel *item = _triggers[0];
  934. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  935. ItemSubModel *subItem = item.subItems[indexPath.row];
  936. [RulesAddViewController modifyTrigger:item subItem:subItem refDevices:_triggerDevices tableView:_tableView];
  937. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  938. [self modifyTriggerOfTimer:item];
  939. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
  940. [self modifyTriggerOfDaylight:item];
  941. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
  942. [self modifyTriggerOfHeat:item];
  943. }
  944. } else if (indexPath.section == 2) {//actions
  945. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  946. if (deviceItem) {
  947. ItemSubModel *subItem = deviceItem.subItems[indexPath.row];
  948. [RulesAddViewController modifyAction:subItem refDevices:_actionDevices tableView:_tableView];
  949. }
  950. }
  951. }
  952. #pragma mark
  953. - (void)modifyTriggerOfTimer:(ItemModel *)item {
  954. TimePickerPopupView *tpopup = [[TimePickerPopupView alloc] initFromNib];
  955. tpopup.timeTrigger = item;
  956. tpopup.refConditions = _conditions;
  957. [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  958. if (buttonIndex == 0) {//OK
  959. [_tableView reloadData];
  960. }
  961. }];
  962. }
  963. - (void)modifyTriggerOfDaylight:(ItemModel *)item {
  964. DaylightPopupView *dpopup = [[DaylightPopupView alloc] initFromNib];
  965. dpopup.daylightTrigger = item;
  966. dpopup.refConditions = _conditions;
  967. [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  968. if (buttonIndex == 0) {//OK
  969. [_tableView reloadData];
  970. }
  971. }];
  972. }
  973. - (void)modifyTriggerOfHeat:(ItemModel *)item {
  974. ExternHeatPopupView *epopup = [[ExternHeatPopupView alloc] initFromNib];
  975. epopup.externHeatTrigger = item;
  976. epopup.refConditions = _conditions;
  977. [epopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  978. if (buttonIndex == 0) {//OK
  979. [_tableView reloadData];
  980. }
  981. }];
  982. }
  983. + (void)modifyTrigger:(ItemModel *)trigger subItem:(ItemSubModel *)subItem refDevices:(NSMutableArray<ItemModel> *)refDevices tableView:(CustomTableView *)tableView {
  984. if ([subItem.deleteYn boolValue]) {
  985. [[JDFacade facade] toast:@"삭제된 장치입니다\r목록에서 제거하세요"];
  986. return;
  987. }
  988. ItemModel *deviceItem = [refDevices objectKey:@"sourceId" eqaulToString:subItem.sourceId];
  989. NSLog(@"deviceItem : %@", deviceItem);
  990. //노드 선택 팝럽.
  991. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  992. npopup.refDevice = deviceItem;
  993. npopup.typeCode = ksItemTypeCodeTrigger;
  994. npopup.isModifyMode = YES;
  995. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  996. if (buttonIndex == 0) {//OK
  997. ItemSubModel *fsubItem = [deviceItem.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  998. if (fsubItem != nil) {
  999. [trigger.subItems removeAllObjects];
  1000. [trigger.subItems addObject:fsubItem];
  1001. }
  1002. // subItem.cmdclsCode = matchedSubItem.cmdclsCode;
  1003. // subItem.cmdclsTypeId = matchedSubItem.cmdclsTypeId;
  1004. //
  1005. // if (matchedSubItem.cmdclsValueList) {
  1006. // subItem.cmdclsValueList = matchedSubItem.cmdclsValueList;
  1007. //
  1008. // CmdClsValueModel *pCmdClsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  1009. // subItem.cmdclsValue = pCmdClsValue.cmdclsValue; //CommandClassControlNodeView - 에서 선택됨.
  1010. // }
  1011. if (tableView) {
  1012. [tableView reloadData];
  1013. }
  1014. }
  1015. }];
  1016. }
  1017. + (void)modifyAction:(ItemSubModel *)subItem refDevices:(NSMutableArray<ItemModel> *)refDevices tableView:(CustomTableView *)tableView {
  1018. if ([subItem.deleteYn boolValue]) {
  1019. [[JDFacade facade] toast:@"삭제된 장치입니다\r목록에서 제거하세요"];
  1020. return;
  1021. }
  1022. ItemModel *item = [refDevices objectKey:@"sourceId" eqaulToString:subItem.sourceId];
  1023. ItemSubModel *matchedSubItem = [item.subItems objectKey:@"sourceSubId" eqaulToString:subItem.sourceSubId];
  1024. //노드 선택 팝럽.
  1025. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  1026. npopup.refDevice = item;
  1027. npopup.typeCode = ksItemTypeCodeTrigger;
  1028. npopup.isModifyMode = YES;
  1029. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1030. if (buttonIndex == 0) {//OK
  1031. subItem.cmdclsCode = matchedSubItem.cmdclsCode;
  1032. subItem.cmdclsTypeId = matchedSubItem.cmdclsTypeId;
  1033. subItem.cmdclsValueList = matchedSubItem.cmdclsValueList;
  1034. CmdClsValueModel *pCmdClsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  1035. subItem.cmdclsValue = pCmdClsValue.cmdclsValue; //CommandClassControlNodeView - 에서 선택됨.
  1036. if (tableView) {
  1037. [tableView reloadData];
  1038. }
  1039. }
  1040. }];
  1041. // NSString *cmdclsCode = matchedSubItem != nil ? matchedSubItem.cmdclsCode : nil;
  1042. //
  1043. // ActionDeviceCommandPopupView *popup = [[ActionDeviceCommandPopupView alloc] initFromNib];
  1044. // popup.typeCode = ksItemTypeCodeTrigger;
  1045. // popup.cmdclsCode = cmdclsCode;
  1046. // popup.refDevice = item;
  1047. // [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1048. //
  1049. //
  1050. // if (buttonIndex == 0) {//OK
  1051. //
  1052. // subItem.cmdclsCode = matchedSubItem.cmdclsCode;
  1053. // subItem.cmdclsTypeId = matchedSubItem.cmdclsTypeId;
  1054. // subItem.cmdclsValueList = matchedSubItem.cmdclsValueList;
  1055. // CmdClsValueModel *pCmdClsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  1056. // subItem.cmdclsValue = pCmdClsValue.cmdclsValue; //CommandClassControlNodeView - 에서 선택됨.
  1057. //
  1058. // if (tableView) {
  1059. //
  1060. // [tableView reloadData];
  1061. // }
  1062. // }
  1063. // }];
  1064. }
  1065. #pragma mark - TableView UI Events
  1066. - (void)btnAddTriggerTouched:(id)sender {
  1067. TriggerSelectPopupView *tpopup = [[TriggerSelectPopupView alloc] initFromNib];
  1068. tpopup.refTriggers = _triggers;
  1069. tpopup.refDevices = _triggerDevices;
  1070. tpopup.refConditions = _conditions;
  1071. [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1072. if (buttonIndex == 0) {
  1073. // NSLog(@"_triggers : %@", _triggers);
  1074. [_tableView reloadData];
  1075. }
  1076. }];
  1077. }
  1078. //동작할 장치 추가
  1079. - (void)btnAddActionTouched:(id)sender {
  1080. DeviceSelectPopupView *dpopup = [[DeviceSelectPopupView alloc] initFromNib];
  1081. dpopup.refDevices = _actionDevices;
  1082. dpopup.typeCode = ksItemTypeCodeTrigger;
  1083. [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1084. if (buttonIndex == 0) {//ok
  1085. // ItemModel *device = dpopup.selectedDevices[0];
  1086. //
  1087. // NSLog(@"device : %@", device);
  1088. //
  1089. // ItemSubModel *subModel = device.subItems.count > 0 ? [device.subItems objectAtIndex:0] : nil ;
  1090. // NSString *cmdclsCode = subModel != nil ? subModel.cmdclsCode : nil;
  1091. //
  1092. //
  1093. // if ( !(EQUALS(cmdclsCode, ksCmdClassTypeSwitchBinary) ||
  1094. // EQUALS(cmdclsCode, ksCmdClassTypeValve)) ) {
  1095. //
  1096. // return ;
  1097. // }
  1098. //
  1099. // ActionDeviceCommandPopupView *popup = [[ActionDeviceCommandPopupView alloc] initFromNib];
  1100. // popup.typeCode = ksItemTypeCodeTrigger;
  1101. // popup.cmdclsCode = cmdclsCode;
  1102. // popup.refDevice = device;
  1103. // [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1104. //
  1105. //
  1106. // if (buttonIndex == 0) {//OK
  1107. //
  1108. // ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1109. // if (!deviceItem) {//디바이스 아이템이 없을 경우,
  1110. // deviceItem = [[ItemModel alloc] init];
  1111. // deviceItem.itemName = @"이 장치를 실행";
  1112. // deviceItem.itemSubTypeCode = ksItemSubTypeCodeDevice;
  1113. //
  1114. // deviceItem.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  1115. //
  1116. // [_actions addObject:deviceItem];
  1117. // }
  1118. //
  1119. //
  1120. // ItemSubModel *snode = popup.selectedNode;
  1121. // //존재 여부 확인
  1122. // if (![deviceItem.subItems objectByUsingPredicateFormat:@"sourceId == %@ && sourceSubId == %@", snode.sourceId, snode.sourceSubId]) {
  1123. // [deviceItem.subItems addObject: snode];
  1124. // }
  1125. //
  1126. // [_tableView reloadData];
  1127. // }
  1128. // }];
  1129. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  1130. npopup.refDevice = dpopup.selectedDevices[0];
  1131. npopup.typeCode = ksItemTypeCodeTrigger;
  1132. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1133. //action add
  1134. if (buttonIndex == 0) {//OK
  1135. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1136. if (!deviceItem) {//디바이스 아이템이 없을 경우,
  1137. deviceItem = [[ItemModel alloc] init];
  1138. deviceItem.itemName = @"이 장치를 실행";
  1139. deviceItem.itemSubTypeCode = ksItemSubTypeCodeDevice;
  1140. deviceItem.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  1141. [_actions addObject:deviceItem];
  1142. }
  1143. ItemSubModel *snode = npopup.selectedNode;
  1144. //존재 여부 확인
  1145. if (![deviceItem.subItems objectByUsingPredicateFormat:@"sourceId == %@ && sourceSubId == %@", snode.sourceId, snode.sourceSubId]) {
  1146. [deviceItem.subItems addObject: snode];
  1147. }
  1148. [_tableView reloadData];
  1149. }
  1150. }];
  1151. }
  1152. }];
  1153. }
  1154. - (void)btnAddPushMessageTouched:(id)sender {
  1155. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  1156. if (!pushItem) {//디바이스 아이템이 없을 경우,
  1157. pushItem = [[ItemModel alloc] init];
  1158. pushItem.itemName = [JDFacade facade].loginUser.memberId;
  1159. pushItem.itemSubTypeCode = ksItemSubTypeCodeAppPush;
  1160. pushItem.pushes = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  1161. ItemSubModel *push = [[ItemSubModel alloc] init];
  1162. push.conditionTypeCode = @"02";
  1163. [pushItem.pushes addObject:push];
  1164. [_actions addObject:pushItem];
  1165. }
  1166. [_tableView reloadData];
  1167. }
  1168. - (void)btnDeleteTriggerTouched:(id)sender {
  1169. NSLog(@"btnDeleteTriggerTouched");
  1170. CustomButton *btnDelete = (CustomButton *)sender;
  1171. ItemModel *item = btnDelete.value;
  1172. NSLog(@"item : %@", item);
  1173. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//장치일 경우
  1174. ItemSubModel *subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  1175. NSLog(@"subItem : %@", subItem);
  1176. if (subItem) {
  1177. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  1178. NSLog(@"subItem.cmdclsValueList : %@", subItem.cmdclsValueList);
  1179. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  1180. [[JDFacade facade] setRadioButtonStatus:@NO object:cmdclsValue];
  1181. cmdclsValue.isSelected = NO;
  1182. }
  1183. }
  1184. }
  1185. if (_triggers && _triggers.count) {
  1186. [_triggers removeAllObjects];
  1187. }
  1188. [_tableView reloadData];
  1189. }
  1190. - (void)btnDeleteDeviceActionTouched:(id)sender {
  1191. NSLog(@"btnDeleteDeviceActionTouched");
  1192. CustomButton *btnDelete = (CustomButton *)sender;
  1193. ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
  1194. //선택 설정을 초기화
  1195. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  1196. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  1197. [[JDFacade facade] setRadioButtonStatus:@NO object:cmdclsValue];
  1198. cmdclsValue.isSelected = NO;
  1199. }
  1200. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1201. [deviceItem.subItems removeObject:subItem];
  1202. [_tableView reloadData];
  1203. }
  1204. - (void)btnDeletePushActionTouched:(id)sender {
  1205. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  1206. [_actions removeObject:pushItem];
  1207. [_tableView reloadData];
  1208. }
  1209. - (void)btnAddTouched:(id)sender {
  1210. UIButton *btn = sender;
  1211. //1001:규칙add, 2001:알림add
  1212. UIControl *control;
  1213. if ([btn tag] > 2000) {
  1214. control = [self.view viewWithTag:2000];
  1215. }
  1216. else {
  1217. control = [self.view viewWithTag:1000];
  1218. }
  1219. [control becomeFirstResponder];
  1220. }
  1221. //- (void)btnInputConfirmTouched:(id)sender {
  1222. //
  1223. // UIButton *btn = sender;
  1224. //
  1225. // //1002:규칙add, 2002:알림add
  1226. // if ([btn tag] > 2000) {
  1227. //
  1228. //
  1229. // }
  1230. // else {
  1231. //
  1232. // }
  1233. //
  1234. //}
  1235. - (void)insertActionAddButton:(UIButton*)button {
  1236. if (![button actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  1237. [button addTarget:self action:@selector(btnAddTouched:) forControlEvents:UIControlEventTouchUpInside];
  1238. }
  1239. }
  1240. //- (void)insertActionInputConfirmButton:(UIButton*)button {
  1241. //
  1242. // if (![button actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  1243. // [button addTarget:self action:@selector(btnInputConfirmTouched:) forControlEvents:UIControlEventTouchUpInside];
  1244. // }
  1245. //}
  1246. - (IBAction)btnCancelTouched:(id)sender {
  1247. [self dismissViewControllerAnimated:YES completion:nil];
  1248. }
  1249. #pragma mark - UI Events
  1250. - (IBAction)btnConfirmTouched:(id)sender {
  1251. [self.view endEditing:YES];
  1252. //1.Validate Title
  1253. RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; //타이틀 섹션
  1254. if (tcell && ![ValidateUtil validateTextfiled:tcell.txtRuleTitle type:ValidateTypeNull title:NSLocalizedString(@"규칙 이름", @"규칙 이름")]) {
  1255. return;
  1256. }
  1257. if (tcell.txtRuleTitle.text.length > 40) {
  1258. [[JDFacade facade] alert:@"규칙이름은 최대 40자까지 입력할 수 있습니다"];
  1259. return;
  1260. }
  1261. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  1262. if (pushItem) {
  1263. if (_txvMessage && [_txvMessage.text isEmptyString]) {//푸시메시지 여부 확인
  1264. [_actions removeObject:pushItem];
  1265. }
  1266. else if (_txvMessage) {
  1267. ItemSubModel *subItem = pushItem.pushes[0];
  1268. subItem.cmdclsValue = _txvMessage.text;
  1269. if (_txvMessage.text.length > 120) {
  1270. [[JDFacade facade] alert:@"알림메시지는 최대 120자까지 입력할 수 있습니다"];
  1271. return;
  1272. }
  1273. }
  1274. }
  1275. if (!_refRuleDetail) {//생성
  1276. [self requestRegisterRule];
  1277. }
  1278. else {//수정
  1279. BOOL hasDeleteNode = NO;
  1280. ItemModel *triggerDevice = [_triggers objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1281. ItemModel *actionDevice = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1282. ItemModel *conditionDevice = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1283. hasDeleteNode = [RulesAddViewController hasDeleteNode:triggerDevice];
  1284. hasDeleteNode = [RulesAddViewController hasDeleteNode:actionDevice] ? YES : hasDeleteNode;
  1285. hasDeleteNode = [RulesAddViewController hasDeleteNode:conditionDevice] ? YES : hasDeleteNode;
  1286. if (hasDeleteNode) {
  1287. [[JDFacade facade] confirm:@"삭제된 장치를 제외 후 저장합니다" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1288. if (buttonIndex == 0) {//OK
  1289. [self getRidOfDeleteDevices:triggerDevice];
  1290. [self getRidOfDeleteDevices:actionDevice];
  1291. [self getRidOfDeleteDevices:conditionDevice];
  1292. [_tableView reloadData];
  1293. [self requestModifyRule];
  1294. }
  1295. }];
  1296. }
  1297. else {
  1298. [self requestModifyRule];
  1299. }
  1300. }
  1301. }
  1302. //삭제된 장치를 제거함.
  1303. - (void)getRidOfDeleteDevices:(ItemModel *)deviceItem {
  1304. [deviceItem.subItems enumerateObjectsUsingBlock:^(ItemSubModel *subItem, NSUInteger idx, BOOL * _Nonnull stop) {
  1305. if (subItem.deleteYn) {
  1306. if ([_triggers containsObject:deviceItem]) {
  1307. [_triggers removeObject:deviceItem];
  1308. return;
  1309. }
  1310. [deviceItem.subItems removeObject:subItem];
  1311. }
  1312. }];
  1313. }
  1314. #pragma mark - MemoryWarning
  1315. - (void)didReceiveMemoryWarning
  1316. {
  1317. [super didReceiveMemoryWarning];
  1318. // Dispose of any resources that can be recreated.
  1319. }
  1320. @end