| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757 |
- //
- // RulesAddViewController.m
- // kneet2
- //
- // Created by Jason Lee on 11/20/15.
- // Copyright © 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "RequestHandler.h"
- #import "ItemModel.h"
- #import "CustomLabel.h"
- #import "CustomButton.h"
- #import "CustomImageView.h"
- #import "CustomTableView.h"
- #import "UIImageView+WebCache.h"
- #import "RulesAddViewController.h"
- #import "CustomCheckBox.h"
- #import "DeviceSelectPopupView.h"
- #import "DeviceNodePopupView.h"
- #import "TriggerSelectPopupView.h"
- #import "DeviceModel.h"
- #import "CustomTextView.h"
- #import "ValidateUtil.h"
- #import "CustomTextField.h"
- #import "RuleModel.h"
- @implementation RulesAddTitleTableViewCell
- @end
- @implementation RulesAddHeaderTableViewCell
- @end
- @implementation RulesAddTableViewCell
- @end
- @implementation RulesAddPushTableViewCell
- @end
- @implementation RulesAddConditionHeaderTableViewCell
- @end
- @implementation RulesAddConditionTableViewCell
- @end
- @implementation RulesAddFooterTableViewCell
- @end
- @interface RulesAddViewController () <CustomTextViewDelegate> {
- NSMutableArray<ItemModel> *_triggers, *_actions, *_conditions, *_pushes;
- NSMutableArray<ItemModel> *_triggerDevices, *_actionDevices;
-
- BOOL _isNotFirstLoading, _isCustomCreation;
- NSMutableArray *_arrayForHeader, *_arrayForFooter;
-
- CustomTextField *_txtRuleTitle;
- CustomButton *_btnTriggerAdd, *_btnPushAdd;
-
- CustomCheckBox *_chkConditions;
- }
- @end
- #pragma mark - Class Definition
- @implementation RulesAddViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- _arrayForHeader = [[NSMutableArray alloc] init];
- _arrayForFooter = [[NSMutableArray alloc] init];
-
- [self initUI];
- [self prepareViewDidLoad];
- }
- - (void)initUI {
- [self initTableViewAsDefaultStyle:_tableView];
- }
- - (void)prepareViewDidLoad {
- [self requestDeviceListForHome];
- }
- #pragma mark - Main Logic
- - (void)requestDeviceListForHome {
- //parameters
- NSDictionary *parameter = @{@"item_type_code": ksItemTypeCodeAction};
-
- NSString *path = [NSString stringWithFormat:API_GET_ITEM_DEVICES, ksItemTypeCodeAction];
-
- [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[ItemListModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- ItemListModel *fetchedItemList = (ItemListModel *)responseObject;
-
- if (fetchedItemList && fetchedItemList.list && fetchedItemList.list.count) {//API 성공 ,
- _triggerDevices = fetchedItemList.list;
- _actionDevices = [(NSMutableArray<ItemModel> *)[NSMutableArray alloc] initWithArray:_triggerDevices copyItems:YES];
- }
-
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)requestRegisterRule {
-
- NSArray *triggers = [self items:_triggers];
- NSArray *actions = [self items:_actions];
- NSArray *conditions = [self conditions:_triggers];
-
- //validate
- if (!triggers || !triggers.count) {
- [[JDFacade facade] alert:NSLocalizedString(@"선택된 트리거가 없습니다", @"선택된 트리거가 없습니다")];
- return;
- }
-
- if (!actions || !actions.count) {
- [[JDFacade facade] alert:NSLocalizedString(@"액션이 없습니다", @"액션이 없습니다")];
- return;
- }
-
- //parameters
- NSDictionary *parameter = @{@"rule_name" : _txtRuleTitle.text,
- @"use_yn" : ksYES,
- @"triggers": triggers ? triggers : [NSNull null],
- @"actions": actions ? actions : [NSNull null],
- @"conditions": conditions ? conditions : [NSNull null]};
-
- NSString *path = [NSString stringWithFormat:API_POST_RULE];
-
- [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[RuleModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- RuleModel *result = (RuleModel *) responseObject;
-
- if (result) {//API 성공 ,
- [[JDFacade facade] dismissModalStack:YES completion:^{
- [[JDFacade facade] toast:NSLocalizedString(@"등록되었습니다", @"등록되었습니다")];
- }];
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- //아이템 배열을 리턴함.
- - (NSArray *)items:(NSArray<ItemModel> *)items {
-
- //triggers, actions, conditions
-
- NSMutableArray *rItems = [[NSMutableArray alloc] init];
-
- for (ItemModel *item in items) {
-
- NSDictionary *dic = nil;
- NSMutableArray *subItems = [[NSMutableArray alloc] init];
-
- NSArray *pdevices = [self subItemsForType:item.subItems itemSubType:ksItemSubTypeCodeDevice];
- NSArray *pushes = [self subItemsForType:item.pushes itemSubType:ksItemSubTypeCodeAppPush];
- NSArray *timers = [self subItemsForType:item.timers itemSubType:ksItemSubTypeCodeTimer];
- NSArray *daylights = [self subItemsForType:item.timers itemSubType:ksItemSubTypeCodeDaylight];
-
- if (pdevices && pdevices.count) {//device
- [subItems addObjectsFromArray:pdevices];
- }
-
- if (pushes && pushes.count) {//push
- [subItems addObjectsFromArray:pushes];
- }
-
- if (timers && timers.count) {//timer
- [subItems addObjectsFromArray:timers];
- }
-
- if (daylights && daylights.count) {//해뜰때/질때
- [subItems addObject:daylights];
- }
-
- if (subItems.count) {
- dic = @{@"item_name": item.itemName,
- @"item_sub_type_code": item.itemSubTypeCode,
- @"item_sub": subItems};
- [rItems addObject:dic];
- }
- }
-
- return rItems;
- }
- //서브아이템 배열을 리턴함.
- - (NSArray *)subItemsForType:(NSArray *)subItems itemSubType:(NSString *)itemSubTypeCode {
-
- NSMutableArray *rSubItems = [[NSMutableArray alloc] init];
-
- if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
- for (ItemSubModel *pdevice in subItems) {
- NSDictionary *rSubItem = @{@"source_id": pdevice.sourceId,
- @"source_sub_id": pdevice.sourceSubId,
- @"cmdcls_value": pdevice.cmdclsValue,
- @"data_type_code": pdevice.dataTypeCode,
- };
-
- [rSubItems addObject:rSubItem];
- }
-
- } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
- //at once.
-
- for (ItemSubModel *subItem in subItems) {
- NSDictionary *rSubItem = @{@"cmdcls_value": subItem.cmdclsValue};
-
- [rSubItems addObject:rSubItem];
- }
-
- } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
- for (ItemSubModel *subItem in subItems) {
- NSDictionary *rSubItem = @{@"hour": subItem.hour,
- @"minute": subItem.minute,
- @"dayofweek": subItem.cmdclsValue,
- @"repeatable": ksYES};
-
- [rSubItems addObject:rSubItem];
- }
- } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 일 경우,
- for (ItemSubModel *subItem in subItems) {
- NSDictionary *rSubItem = @{@"sourceId": subItem.sourceId,
- @"sourceSubId": subItem.sourceSubId};
-
- [rSubItems addObject:rSubItem];
- }
- }
-
- return rSubItems;
- }
- - (NSArray *)conditions:(NSArray<ItemModel> *)items {//trigger만 해당됨.
-
- //conditions
- NSMutableArray *rConditions = [[NSMutableArray alloc] init];
-
- for (ItemModel *item in items) {
- ItemSubModel *subItem = nil;
-
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
- subItem = item.predDevices && item.predDevices.count ? item.predDevices[0] : nil;
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeMode]) {//모드일 경우,
- subItem = item.modes[0];
-
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
- subItem = item.timers[0];
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeGeoFencing]) {//지오펜싱일 경우,
- subItem = item.mobileDevices && item.mobileDevices.count ? item.mobileDevices[0] : nil;
- }
-
- for (ItemModel *condition in subItem.conditions) {//컨디션 여부
- ItemModel *copyCondition = [condition copy];
-
-
- for (ItemSubModel *subCondition in copyCondition.subItems) {
- subCondition.cmdclsValueMsg = nil;
- subCondition.dueDate = nil;
- subCondition.daysOfWeek = nil;
- subCondition.dueTime = nil;
- subCondition.homeModes = nil;
- subCondition.externHeat = nil;
- }
-
- [rConditions addObject:[copyCondition toDictionary]];
- }
- }
-
- return rConditions;
- }
- #pragma mark - UITableView DataSource & Delegate
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 5;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-
- NSInteger count = 0;
- if (section == 0) {//title
- count = 1;
-
- } else if (section == 1) {//triggers
- count = _triggers && _triggers.count ? _triggers.count : 0;
-
- } else if (section == 2) {//actions
-
- NSArray *matchedSubItems = [_actions matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
- count = matchedSubItems && matchedSubItems.count ? matchedSubItems.count : 0;
-
- } else if (section == 3) {//push-message
- count = _pushes && _pushes.count ? _pushes.count : 0;
-
- } else if (section == 4) {//conditions
- count = _chkConditions.checked ? 1 : 0;
- }
-
- return count;
- }
- - (NSString *)headerTitleForSection:(NSInteger)section {
- NSString *title = nil;
-
- if (section == 1) {//trigger
- title = @"이 때가 되면";
- if (_triggers && _triggers.count) {
- ItemModel *item = _triggers[0];
-
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
- title = @"장치 상태가 바뀔 때";
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
- title = @"이 시간마다";
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
- title = @"더울때 / 추울때";
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
- title = @"해뜰때 / 질때";
- }
- }
-
- } else if (section == 2) {//action
- title = @"이 장치를 실행";
- // title = _actions && _actions.count ? @"선택됨" : title;
- } else if (section == 3) {//pushes
- title = @"알림 메시지";
- // title = _pushes && _pushes.count ? @"선택됨" : title;
- }
-
- // else if (section == 3) {//conditions
- // title = @"추가 조건";
- // title = _triggers && triggers.count ? @"선택됨" : title;
- // }
- return title;
- }
- - (void)addTargetToHeaderAddButton:(CustomButton *)btnAdd section:(NSInteger)section {
- if (section == 1) {
- _btnTriggerAdd = btnAdd;
- [btnAdd addTarget:self action:@selector(btnAddTriggerTouched:) forControlEvents:UIControlEventTouchUpInside];
-
- } else if (section == 2) {
- [btnAdd addTarget:self action:@selector(btnAddActionTouched:) forControlEvents:UIControlEventTouchUpInside];
-
- } else if (section == 3) {
- _btnPushAdd = btnAdd;
- [btnAdd addTarget:self action:@selector(btnAddPushMessageTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
-
- UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
- if (!view) {
- if (section == 0) {
- view = [[UIView alloc] init];
- } else if (section == 1 || section == 2 || section == 3) {//TODO : hide add button or not;
- RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
- view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
- [view addSubview:hcell];
- hcell.lblTitle.text = [self headerTitleForSection:section];
-
- if (![hcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [self addTargetToHeaderAddButton:hcell.btnAdd section:section];
- }
-
- } else if (section == 4) {
- RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ConditionHeaderCellIdentifier"];
-
- view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
- [view addSubview:hcell];
- }
-
- if (_arrayForHeader.count == section) {
- [_arrayForHeader insertObject:view atIndex:section];
- }
- } else {
-
- if (section == 1 || section == 2 || section == 3) {
- RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)view.subviews[0];
- hcell.lblTitle.text = hcell.lblTitle.text = [self headerTitleForSection:section];
-
- } else if (section == 4) {
- RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)view.subviews[0];
- }
- }
-
- return view;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- if (section == 0) {
- return 0.01f;
- }
- return 90.0f;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
-
- UIView *view = _arrayForFooter.count > section ? _arrayForFooter[section] : nil;
- if (!view) {
-
- RulesAddFooterTableViewCell *hcell = (RulesAddFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FooterCellIdentifier"];
- view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
- [view addSubview:hcell];
-
- if (_arrayForHeader.count == section) {
- [_arrayForHeader insertObject:view atIndex:section];
- }
- }
-
- return view;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
- if (section == 0) {
- return 0.01f;
- }
- return 15.0f;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat height = 0.0f;
- NSInteger section = indexPath.section;
-
- if (section == 0) {//title
- height = 50;
-
- } else if (section == 1) {//triggers
- height = 95;
-
- } else if (section == 2) {//actions
- height = 95;
-
- } else if (section == 3) {//push-message
- height = 115;
-
- } else if (section == 4) {//conditions
- height = 255;
- }
-
- return height;
- }
- - (void)configureTriggerCell:(RulesAddTableViewCell *)cell item:(ItemModel *)item {
-
- ItemSubModel *subItem = nil;
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
- subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
-
- cell.lblItem.text = item.sourceName;
- cell.lblSubItem.text = subItem.sourceSubName;
-
- //노드의 액션 값을 세팅함.
- if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
- CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
- cell.lblCondition.text = [DeviceModel contentValueMsgByCmdClsCode:subItem.cmdclsCode cmdclsTypeId:(NSString *)subItem.cmdclsTypeId contentValue:cmdclsValue.cmdclsValue];
- } else {
- cell.lblCondition.text = subItem.cmdclsValueMsg;
- }
- cell.lblCondition.hidden = NO;
-
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
-
- subItem = item.timers && item.timers.count ? item.timers[0] : nil;
- if (subItem) {
- NSInteger phour = [subItem.hour integerValue];
- NSString *period = phour < 12 ? @"AM" : @"PM";//NSLocalizedString(@"오전", @"오전") : NSLocalizedString(@"오후", @"오후");
-
- if ([period isEqualToString:NSLocalizedString(@"오후", @"오후")]) {
- if (phour > 12) {
- phour -= 12;
- }
- }
-
- NSString *title = [NSString stringWithFormat:@"%@ %zd:%@", period, phour, subItem.minute];
-
- // NSDictionary *titleColor = @{NSForegroundColorAttributeName : kUITextColor02};
- cell.lblItem.text = title;
- cell.lblSubItem.text = subItem.daysOfWeek;
-
- cell.lblCondition.hidden = YES;
- }
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
-
- subItem = item.daylights && item.daylights.count ? item.daylights[0] : nil;
- if (subItem) {
- NSString *title = [subItem.sourceId isEqualToString:@"sunriseUtcTime"] ? @"해뜰때" : @"해질때";
- cell.lblItem.text = title;
- cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@", subItem.sourceName, subItem.daysOfWeek];
-
- cell.lblCondition.hidden = YES;
- }
- }
-
-
- [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- NSInteger section = indexPath.section;
-
- UITableViewCell *cell = nil;
-
- if (section == 0) {//title
- RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
-
- if (!_txtRuleTitle) {
- _txtRuleTitle = tcell.txtRuleTitle;
- }
-
- cell = tcell;
-
- } else if (section == 1) {//triggers
- ItemModel *item = _triggers[indexPath.row];
-
- RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
-
- [self configureTriggerCell:tcell item:item];
-
- tcell.btnDelete.value = item;
- if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [tcell.btnDelete addTarget:self action:@selector(btnDeleteTriggerTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
-
- cell = tcell;
- } else if (section == 2) {//actions
-
- ItemModel *item = _actions[indexPath.row];
- //노드 액션이 선택된 노드만 출력함.
- NSInteger fcount = indexPath.row; //선택된 액션만 찾기 위한 인덱스
- NSArray *matchedSubItems = [_actions matchedArrayInSubArrays:@"subItems" objectName:ksCustomRadioButtonStatus condition:YES];
-
- ItemSubModel *subItem = nil;
- for (subItem in matchedSubItems) {
- if (fcount == 0) {
- break;
- } else {
- fcount--;
- }
- }
-
- RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
- tcell.lblItem.text = item.sourceName;
- [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
-
- //노드의 액션 값을 세팅함.
- if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
- CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
- tcell.lblCondition.text = cmdclsValue.cmdclsValueMsg;
- } else {
- tcell.lblCondition.text = subItem.cmdclsValueMsg;
- }
-
- tcell.btnDelete.value = subItem;
-
- if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [tcell.btnDelete addTarget:self action:@selector(btnDeleteActionTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
-
- cell = tcell;
-
- } else if (section == 3) {//push-message
- RulesAddPushTableViewCell *tcell = (RulesAddPushTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"PushCellIdentifier"];
- tcell.txvMessage.delegate = self;
-
- cell = tcell;
- } else if (section == 4) {//conditions
- RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
-
- cell = tcell;
- }
-
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- #pragma mark - TableView UI Events
- - (void)btnAddTriggerTouched:(id)sender {
- if (!_triggers) {
- _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
- }
-
- TriggerSelectPopupView *tpopup = [[TriggerSelectPopupView alloc] initFromNib];
- tpopup.refTriggers = _triggers;
- tpopup.refDevices = _triggerDevices;
- [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {
- [_tableView reloadData];
- }
- }];
- }
- - (void)btnAddActionTouched:(id)sender {
- DeviceSelectPopupView *dpopup = [[DeviceSelectPopupView alloc] initFromNib];
- dpopup.refDevices = _actionDevices;
- dpopup.typeCode = ksItemTypeCodeTrigger;
-
- [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//ok
- DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
- npopup.refDevice = dpopup.selectedDevices[0];
- npopup.typeCode = ksItemTypeCodeTrigger;
-
- [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- //action add
- if (buttonIndex == 0) {//OK
- if (!_actions) {
- _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
- }
-
- npopup.refDevice.itemName = npopup.refDevice.sourceName;
- npopup.refDevice.itemSubTypeCode = ksItemSubTypeCodeDevice;
-
- [_actions addObject:npopup.refDevice];
- [_tableView reloadData];
- }
- }];
- }
- }];
- }
- - (void)btnAddPushMessageTouched:(id)sender {
- if (!_pushes) {
- _pushes = [(NSMutableArray<ItemModel> *)[NSMutableArray alloc] init];
- }
-
- if (!_pushes.count) {
- ItemModel *push = [[ItemModel alloc] init];
- push.itemSubTypeCode = ksItemSubTypeCodeAppPush;
-
- [_pushes addObject:push];
- }
-
- [_tableView reloadData];
- }
- - (void)btnDeleteTriggerTouched:(id)sender {
- if (_triggers && _triggers.count) {
- [_triggers removeAllObjects];
- }
-
- [_tableView reloadData];
- }
- - (void)btnDeleteActionTouched:(id)sender {
- CustomButton *btnDelete = (CustomButton *)sender;
-
- ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
-
- //선택 설정을 초기화
- [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
-
- for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
- cmdclsValue.isSelected = NO;
- }
-
- //액션과 일치하는 서브아이템을 삭제해줌.
- [_actions enumerateObjectsUsingBlock:^(ItemModel *action, NSUInteger idx, BOOL * _Nonnull stop) {
- NSArray *matchedArray = [action.subItems matchedArrayByObjectName:ksCustomRadioButtonStatus condition:YES];
-
- if (!matchedArray.count) {
- [_actions removeObject:action];
- }
- }];
-
- [_tableView reloadData];
- }
- #pragma mark - CustomTextView Delegate
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- _btnPushAdd.hidden = textView.text.length > 0;
- NSLog(@"%s\n %zd", __PRETTY_FUNCTION__, textView.text.length);
-
- return textView.text.length < 120;
- }
- #pragma mark - UI Events
- - (IBAction)btnConfirmTouched:(id)sender {
- [self.view endEditing:YES];
-
- //1.Validate Title
- RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; //첫번째 섹션
-
- if (tcell && ![ValidateUtil validateTextfiled:tcell.txtRuleTitle type:ValidateTypeNull title:NSLocalizedString(@"규칙 이름", @"규칙 이름")]) {
- return;
- }
-
- if (!_tmpRuleDetail) {//생성
- [self requestRegisterRule];
-
- } else {//수정
- // [self requestModifyRule];
- }
- }
- - (IBAction)btnCancelTouched:(id)sender {
- [[JDFacade facade] dismissModalStack:YES completion:nil];
- }
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|