| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598 |
- //
- // 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 "RuleModel.h"
- #import "DeviceModel.h"
- #import "CustomLabel.h"
- #import "CustomButton.h"
- #import "CustomImageView.h"
- #import "CustomTableView.h"
- #import "CustomCheckBox.h"
- #import "CustomTextView.h"
- #import "CustomTextField.h"
- #import "UIImageView+WebCache.h"
- #import "ValidateUtil.h"
- #import "DeviceSelectPopupView.h"
- #import "DeviceNodePopupView.h"
- #import "TriggerSelectPopupView.h"
- #import "ConditionSelectPopupView.h"
- #import "RulesConditionHomeModePopupView.h"
- #import "RulesConditionViewController.h"
- #import "RulesAddViewController.h"
- #import "RulesAddCompleteViewController.h"
- #import "RulesDetailViewController.h"
- #import "RulesViewController.h"
- #import "TimePickerPopupView.h"
- #import "DaylightPopupView.h"
- #import "ExternHeatPopupView.h"
- //규칙이름
- @implementation RulesAddTitleTableViewCell
- #pragma mark - textfield delegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
-
- return YES;
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
- BOOL input = text.length == 0;
-
- _btnConfirm.hidden = input;
- _btnAdd.hidden = !input;
-
- return YES;
- }
- @end
- //알림 메시지
- @interface RulesAddTextTableViewCell() <UITextViewDelegate>
- @property (nonatomic, strong) void (^heightChangeBlock)(CGFloat height) ;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *constTxtHeight;
- @property CGRect previousRect;
- @end
- @implementation RulesAddTextTableViewCell
- - (void)awakeFromNib {
- [super awakeFromNib];
-
- _previousRect = CGRectZero;
- _previousRect.origin.y = 11.f;
- }
- - (void)registHeightChangeBlock:(void (^)(CGFloat height)) heightChangeBlock {
-
- _heightChangeBlock = heightChangeBlock ;
- }
- - (void)invokeChangeHeight:(CGFloat)height {
-
- if( _heightChangeBlock != nil )
- _heightChangeBlock(height) ;
- }
- #pragma mark - textView delegate
- - (void)textViewDidChange:(UITextView *)textView {
-
- UITextPosition* pos = _txtView.endOfDocument;
- CGRect currentRect = [_txtView caretRectForPosition:pos];
- CGFloat previousY = _previousRect.origin.y;
-
-
- if (previousY != currentRect.origin.y) {
-
- CGFloat height = currentRect.origin.y - previousY;
- _constTxtHeight.constant = _constTxtHeight.constant + height < 48.f ? 48.f : _constTxtHeight.constant + height;
- [self invokeChangeHeight:height];
- }
-
- _previousRect = currentRect;
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
-
- NSString *string = [textView.text stringByReplacingCharactersInRange:range withString:text];
- BOOL input = string.length == 0;
-
- _btnConfirm.hidden = input;
- _btnAdd.hidden = !input;
-
- return YES;
- }
- - (void)textViewDidBeginEditing:(UITextView *)textView {
-
- _imgTxtBg.highlighted = YES;
- }
- - (void)textViewDidEndEditing:(UITextView *)textView {
-
- _imgTxtBg.highlighted = NO;
- }
- @end
- @implementation RulesAddHeaderTableViewCell
- @end
- @implementation RulesAddTableViewCell
- @end
- @implementation RulesAddPushTableViewCell
- @end
- @implementation RulesAddConditionHeaderTableViewCell
- @end
- @implementation RulesAddConditionTableViewCell
- @end
- @implementation RulesAddFooterTableViewCell
- @end
- @interface RulesAddViewController () <KeyboardFrameChangedDelegate> {
-
-
- NSMutableArray<ItemModel> *_triggerDevices, *_actionDevices, *_conditionDevices;
-
- BOOL _isNotFirstLoading, _hasCondition;
- NSMutableArray *_arrayForHeader, *_arrayForFooter;
- CGFloat textCellChangeHeight; //알림 메세지 셀 사이즈 변동
-
-
- CustomTextField *_txtRuleTitle;
- CustomTextView *_txvMessage;
- CustomButton *_btnTriggerAdd, *_btnPushAdd;
-
- CustomCheckBox *_chkConditions;
-
- RulesConditionHomeModePopupView *_mpopup;
- RulesConditionViewController *_rcv;
-
- __weak IBOutlet NSLayoutConstraint *constTableBottom;
-
- }
- @property (strong, nonatomic) NSMutableArray<ItemModel> *triggers;
- @property (strong, nonatomic) NSMutableArray<ItemModel> *actions;
- @property (strong, nonatomic) NSMutableArray<ItemModel> *conditions;
- @end
- @implementation RulesAddViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self initProperties];
- [self initUI];
- [self prepareViewDidLoad];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self addObserverKeyboardFrameChanged:self] ;
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- [self removeObserverKeyboard] ;
- }
- #pragma mark - Keyboard Frame Changed Delegate
- - (void)keyboardFrameChanged:(CGRect)frame duration:(CGFloat)duration display:(BOOL)display {
-
- NSLog(@"call keyboardFrameChanged!!");
-
- [UIView animateWithDuration:duration animations:^{
-
- if( display ) {
- constTableBottom.constant = frame.size.height;
- }
- else {
- constTableBottom.constant = 50.0f;
- }
- [self.view layoutIfNeeded];
-
- }] ;
- }
- #pragma mark - init
- - (void)initProperties {
- _arrayForHeader = [[NSMutableArray alloc] init];
- _arrayForFooter = [[NSMutableArray alloc] init];
-
- if (!_triggers) {
- _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
- }
-
- if (!_actions) {
- _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
- }
- if (!_conditions) {
- _conditions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
- }
- }
- - (void)initUI {
-
- [self initTableViewAsDefaultStyle:_tableView];
-
- if (_refRuleDetail) {
- _lblTitle.text = @"규칙 편집";
- }
- }
- - (void)prepareViewDidLoad {
-
- [self requestDeviceListForAction:ksItemTypeCodeTrigger];
- [self requestDeviceListForAction:ksItemTypeCodeAction];
- }
- #pragma mark - Main Logic
- - (void)requestDeviceListForAction:(NSString *)typeCode {
- //parameters
- // NSDictionary *parameter = @{@"item_type_code": typeCode};
- NSArray *arr = @[typeCode];
- // NSString *path = [NSString stringWithFormat:API_GET_ITEM_DEVICES, ksItemTypeCodeAction];
- NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_GET_ITEM_DEVICES arguments:arr];
-
- [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ItemListModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- ItemListModel *fetchedItemList = (ItemListModel *)responseObject;
-
- if (fetchedItemList && fetchedItemList.list && fetchedItemList.list.count) {//API 성공 ,
-
- if ([typeCode isEqualToString:ksItemTypeCodeAction]) {
- _actionDevices = fetchedItemList.list;
- if (_refRuleDetail) {//편집일 경우,
- ItemModel *actionDevice = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- [self matchValueOfDevices:actionDevice devices:_actionDevices];
- }
-
- } else {
-
- _triggerDevices = fetchedItemList.list;
- _conditionDevices = [(NSMutableArray<ItemModel> *)[NSMutableArray alloc] initWithArray:_triggerDevices copyItems:YES];
-
- if (_refRuleDetail) {//편집일 경우,
- ItemModel *triggerDevice = [_triggers objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- ItemModel *conditionDevice = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
-
- [self matchValueOfDevices:triggerDevice devices:_triggerDevices];
- [self matchValueOfDevices:conditionDevice devices:_conditionDevices];
- }
-
- [self requestDeviceListForAction:ksItemTypeCodeAction];
- }
-
- }
-
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)matchValueOfDevices:(ItemModel *)deviceItem devices:(NSMutableArray<ItemModel> *)devices {
-
- for (ItemSubModel *subItem in deviceItem.subItems) {
- NSArray *matchedArray = [devices matchedArrayInSubArrays:@"subItems" predicateFormat:@"sourceId == %@ && sourceSubId == %@", subItem.sourceId, subItem.sourceSubId];
- if (matchedArray.count) {
- ItemSubModel *matchedSubItem = matchedArray.firstObject;
- [[JDFacade facade] setRadioButtonStatus:@YES object:matchedSubItem];
-
- CmdClsValueModel *pCmdClsValue = [matchedSubItem.cmdclsValueList objectKey:@"cmdclsValue" eqaulToString:subItem.cmdclsValue];
- [[JDFacade facade] setRadioButtonStatus:@YES object:pCmdClsValue];
- }
- }
- }
- - (void)requestRegisterRule {
-
- NSArray *triggers = [self items:_triggers];
- NSArray *actions = [self items:_actions];
-
- BOOL hasConditionForRegister = [_conditions matchedArrayInSubArrays:@"subItems" predicateFormat:@"conditionTypeCode == %@", @"09"].count || _chkConditions.checked;
- NSArray *conditions = hasConditionForRegister ? [self conditions:_conditions] : nil;
-
- //validate
- if (!triggers || !triggers.count) {
- [[JDFacade facade] alert:NSLocalizedString(@"실행할 때를 선택하세요", @"실행할 때를 선택하세요")];
- return;
- }
-
- if (!actions || !actions.count) {
- [[JDFacade facade] alert:NSLocalizedString(@"실행할 장치를 선택하세요", @"실행할 장치를 선택하세요")];
- return;
- }
-
- //parameters
- NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"rule_name" : _txtRuleTitle.text,
- @"use_yn" : ksYES,
- @"triggers": triggers ? triggers : [NSNull null],
- @"actions": actions ? actions : [NSNull null]}];
- if (conditions) {
- [parameter setObject:conditions forKey:@"conditions"];
- }
-
-
- NSString *path = [NSString stringWithFormat:API_POST_RULE];
-
- [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[RuleModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- RuleModel *newRule = (RuleModel *) responseObject;
-
- if (newRule) {//API 성공 ,
-
- ItemModel *item = _triggers.firstObject;
- if (item) {
- newRule.ruleName = _txtRuleTitle.text;
- newRule.triggerItemSubTypeCode = item.itemSubTypeCode;
- }
-
- RulesAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesAddCompleteViewController" storyboardName:@"Rules"];
- vc.refRule = newRule;
- vc.providesPresentationContextTransitionStyle = YES;
- vc.definesPresentationContext = YES;
-
- [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
-
- [self presentViewController:vc animated:YES completion:nil];
-
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)requestModifyRule {
-
- NSArray *triggers = [self items:_triggers];
- NSArray *actions = [self items:_actions];
-
-
- BOOL hasConditionForRegister = [_conditions matchedArrayInSubArrays:@"subItems" predicateFormat:@"conditionTypeCode == %@", @"09"].count || _chkConditions.checked;
- NSArray *conditions = hasConditionForRegister ? [self conditions:_conditions] : nil;
-
- //validate
- if (!triggers || !triggers.count) {
- [[JDFacade facade] alert:NSLocalizedString(@"실행할 때를 선택하세요", @"실행할 때를 선택하세요")];
- return;
- }
-
- if (!actions || !actions.count) {
- [[JDFacade facade] alert:NSLocalizedString(@"실행할 장치를 선택하세요", @"실행할 장치를 선택하세요")];
- return;
- }
-
- //parameters
- NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"rule_name" : _txtRuleTitle.text,
- @"use_yn" : ksYES,
- @"triggers": triggers ? triggers : [NSNull null],
- @"actions": actions ? actions : [NSNull null]}];
- if (conditions) {
- [parameter setObject:conditions forKey:@"conditions"];
- }
-
- NSString *path = [NSString stringWithFormat:API_POST_RULE_MODIFY, _refRuleDetail.homegrpRuleId];
-
- [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[RuleModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- RuleModel *rule = (RuleModel *) responseObject;
-
- if (rule) {//API 성공
-
- RulesDetailViewController *vc = [[JDFacade facade] viewControllerOnPresentingViewController:self viewControllerClass:[RulesDetailViewController class]];
- if (vc) {
- [vc requestRuleDetail];
- } else {
- RulesViewController *rvc = [[JDFacade facade] viewControllerOnPresentingViewController:self viewControllerClass:[RulesViewController class]];
- [rvc prepareViewDidLoad];
- }
-
- [self dismissViewControllerAnimated:YES completion:^{
- [[JDFacade facade] toast:@"편집을 완료합니다"];
- }];
- }
-
- } 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.daylights itemSubType:ksItemSubTypeCodeDaylight];
- NSArray *heats = [self subItemsForType:item.heats itemSubType:ksItemSubTypeCodeHeat];
-
- 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 addObjectsFromArray:daylights];
- }
- if (heats && heats.count) {//더울떄 / 추울때
- [subItems addObjectsFromArray:heats];
- }
-
- 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]) {//디바이스일 경우,
-
- NSArray<ItemSubModel> *pdevices = (NSArray<ItemSubModel> *)[subItems matchedArrayByObjectName:ksCustomRadioButtonStatus condition:YES];
- for (ItemSubModel *pdevice in pdevices) {
- NSDictionary *rSubItem = @{@"source_id": pdevice.sourceId,
- @"source_sub_id": pdevice.sourceSubId,
- @"cmdcls_value": pdevice.cmdclsValue,
- @"condition_type_code": pdevice.conditionTypeCode,
- @"data_type_code": pdevice.dataTypeCode};
-
- [rSubItems addObject:rSubItem];
- }
-
- } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
- //at once.
-
- for (ItemSubModel *subItem in subItems) {
- NSDictionary *rSubItem = @{@"condition_type_code": subItem.conditionTypeCode,
- @"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};
-
- [rSubItems addObject:rSubItem];
- }
- } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 일 경우,
- for (ItemSubModel *subItem in subItems) {
- NSDictionary *rSubItem = @{@"source_id": subItem.sourceId,
- @"source_sub_id": subItem.sourceSubId};
-
- [rSubItems addObject:rSubItem];
- }
- } else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//해뜰때/질때 일 경우,
- for (ItemSubModel *subItem in subItems) {
- NSDictionary *rSubItem = @{@"source_id": subItem.sourceId,
- @"condition_type_code": subItem.conditionTypeCode,
- @"cmdcls_value": subItem.cmdclsValue};
-
- [rSubItems addObject:rSubItem];
- }
- }
-
- return rSubItems;
- }
- - (NSArray *)conditions:(NSArray<ItemModel> *)items {//trigger만 해당됨.
-
- //conditions
- NSMutableArray *rConditions = [[NSMutableArray alloc] init];
-
- for (ItemModel *condition in items) {//item = condition
- NSMutableArray *rSubItems = [[NSMutableArray alloc] init];
-
- if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeHomeMode]) {
- for (ItemSubModel *mode in condition.subItems) {
- NSDictionary *rSubItem = @{@"source_id": mode.sourceId,
- @"cmdcls_value": mode.cmdclsValue,
- @"condition_type_code": mode.conditionTypeCode};
-
- [rSubItems addObject:rSubItem];
- }
- } else if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDueDate]) {
- for (ItemSubModel *subItem in condition.subItems) {
- NSDictionary *rSubItem = @{@"cmdcls_value": subItem.cmdclsValue,
- @"condition_type_code": subItem.conditionTypeCode};
-
- [rSubItems addObject:rSubItem];
- }
- } else if ([condition.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
- for (ItemSubModel *pdevice in condition.subItems) {
- NSDictionary *rSubItem = @{@"source_id": pdevice.sourceId,
- @"source_sub_id": pdevice.sourceSubId,
- @"cmdcls_value": pdevice.cmdclsValue,
- @"data_type_code": pdevice.dataTypeCode,
- @"condition_type_code": pdevice.conditionTypeCode};
-
- [rSubItems addObject:rSubItem];
- }
- } else if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
- for (ItemSubModel *subItem in condition.subItems) {
- NSDictionary *rSubItem = @{@"cmdcls_value": subItem.cmdclsValue,
- @"condition_type_code": subItem.conditionTypeCode};
-
- [rSubItems addObject:rSubItem];
- }
- }
-
- if (rSubItems.count) {
- NSDictionary *dic = @{@"item_name": condition.itemName,
- @"item_sub_type_code": condition.itemSubTypeCode,
- @"item_sub": rSubItems};
- [rConditions addObject:dic];
- }
- }
-
- return rConditions;
- }
- - (void)cloneSubItemsForItemSubType:(NSArray<ItemModel> *)items {
-
- for (ItemModel *item in items) {
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
- //기존 아이템을 그대로 사용함.
- for (ItemSubModel *subItem in item.subItems) {
- [[JDFacade facade] setRadioButtonStatus:@YES object:subItem];
- [[JDFacade facade] setCheckBoxStatus:@YES object:subItem];
- }
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
- item.pushes = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
- item.subItems = nil;
-
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
- item.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
- item.subItems = nil;
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
- item.daylights = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
- item.subItems = nil;
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
- item.heats = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
- item.subItems = nil;
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeMode]) {//더울때/추울때 경우,
- //기존 아이템을 그대로 사용함.
- }
- }
- }
- - (void)setRefRuleDetail:(RuleDetailModel *)refRuleDetail {
-
-
- _refRuleDetail = refRuleDetail;
-
- _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.triggers copyItems:YES];
- _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.actions copyItems:YES];
- _conditions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.conditions copyItems:YES];
-
- [self cloneSubItemsForItemSubType:_triggers];
- [self cloneSubItemsForItemSubType:_actions];
- [self cloneSubItemsForItemSubType:_conditions];
-
- _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
- _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDueDate] ? YES : _hasCondition;
- _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice] ? YES : _hasCondition;
- }
- #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
- ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- count = deviceItem.subItems && deviceItem.subItems.count ? deviceItem.subItems.count : 0;
-
- } else if (section == 3) {//push-message
- count = 1;
-
- } else if (section == 4) {//conditions
- count = _chkConditions.checked || _hasCondition ? 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) {//conditions
- // title = @"추가 조건";
- // title = _triggers && triggers.count ? @"선택됨" : title;
- // }
- return title;
- }
- - (void)addTargetToHeaderAddButton:(CustomButton *)btnAdd section:(NSInteger)section {
-
- NSLog(@"section : %ld", 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];
-
- }
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
-
- UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
-
- if (!view) {
- if (section == 0 || section == 3) {//title
- view = [[UIView alloc] init];
- } else if (section == 1 || section == 2) {//hide add button or not;
- RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
- hcell.width = IPHONE_WIDTH;
-
- view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
- [view addSubview:hcell];
- hcell.lblTitle.text = [self headerTitleForSection:section];
- hcell.imgBg.hidden = section % 2 == 0;
-
- if (![hcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [self addTargetToHeaderAddButton:hcell.btnAdd section:section];
- }
-
- } else if (section == 4) {//conditions
- RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ConditionHeaderCellIdentifier"];
- hcell.width = IPHONE_WIDTH;
-
- view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
- [view addSubview:hcell];
-
- _chkConditions = hcell.chkConditions;
-
- if (!_isNotFirstLoading) {
- _chkConditions.checked = _hasCondition;
- _isNotFirstLoading = YES;
- }
-
- if (![_chkConditions actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [_chkConditions addTarget:self action:@selector(chkConditionsTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
-
- if (![hcell.btnConditions actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [hcell.btnConditions addTarget:self action:@selector(btnConditionsTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
- }
-
- if (_arrayForHeader.count == section) {
- [_arrayForHeader insertObject:view atIndex:section];
- }
- } else {
-
- if (section == 1 || section == 2) {
- 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];
- }
- }
-
- [self checkHeaderButton:section];
-
- return view;
- }
- - (void)checkHeaderButton:(NSInteger)section {
-
- if (section == 1) {
- _btnTriggerAdd.hidden = self.triggers.count > 0;
- }
- }
- - (void)chkConditionsTouched:(id)sender {
- [_tableView reloadData];
-
- CustomCheckBox *chkCondition = (CustomCheckBox *)sender;
-
- if (chkCondition.checked) {
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:4];
- [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
- }
- }
- - (void)btnConditionsTouched:(id)sender {
- ConditionSelectPopupView *popup = [[ConditionSelectPopupView alloc] initFromNib];
- [popup show];
-
- };
- - (void)btnAddModeTouched:(id)sender {
-
- ItemModel *mode = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
- BOOL isNewTrigger = mode == nil;
- mode = isNewTrigger ? [[ItemModel alloc] init] : mode;
-
- if (!_mpopup) {
- _mpopup = [[RulesConditionHomeModePopupView alloc] initFromNib];
- }
-
- _mpopup.condition = mode;
- [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//OK
- if (isNewTrigger) {
- [_conditions addObject:mode];
- }
-
- [self.tableView reloadData];
- }
- }];
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
-
- if (section == 0 || section == 3) {
- return 0.01f;
- }
- else if (section == 4) {
- return 56.0f;
- }
-
- return 56.0f;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
-
- UIView *view = _arrayForFooter.count > section ? _arrayForFooter[section] : nil;
- if (!view) {
-
- RulesAddFooterTableViewCell *hcell = (RulesAddFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FooterCellIdentifier"];
- hcell.width = IPHONE_WIDTH;
-
- 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 0.01f;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat height = 0.0f;
- NSInteger section = indexPath.section;
-
- if (section == 0) {//title
- height = 122;
-
- } else if (section == 1) {//triggers
- height = 80;
-
- } else if (section == 2) {//actions
- height = 80;
-
- } else if (section == 3) {//push-message
- // height = UITableViewAutomaticDimension;
- height = 122 + textCellChangeHeight;
-
- } else if (section == 4) {//conditions
- if (!_rcv) {
- _rcv = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesConditionViewController" storyboardName:@"Rules"];
-
- [self addChildViewController:_rcv];
- [_rcv didMoveToParentViewController:self];
- }
-
- height = _rcv.tableHeight;
- }
-
- return height;
- }
- + (BOOL)hasDeleteNode:(ItemModel *)deviceItem {
- NSArray *matchedArray = [deviceItem.subItems filteredArrayUsingPredicateFormat:@"deleteYn == %@", ksYES];
- return matchedArray.count;
- }
- - (void)configureTriggerCell:(RulesAddTableViewCell *)cell item:(ItemModel *)item {
-
- ItemSubModel *subItem = nil;
- NSString *deviceImageFileName = nil;
-
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//장치일 경우
- subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
-
- cell.lblItem.text = subItem.sourceName;
- deviceImageFileName = subItem.imageFileName;
- if ([subItem.deleteYn boolValue]) {
- [cell.lblItem setStrikethrough:cell.lblItem.text];
- }
-
- 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 {
- if (subItem.cmdclsValueMsg) {
- cell.lblCondition.text = subItem.cmdclsValueMsg;
- } else {
- NSString *condition = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
- cell.lblCondition.text = [NSString stringWithFormat:@"%@%@ %@", subItem.cmdclsValue, subItem.unit, condition];
- }
- }
- [cell.lblCondition setUnderLine:cell.lblCondition.text];
- 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 = [self daysOfWeekAtSubItem];
-
- 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.sourceSubId isEqualToString:@"sunriseUtcTime"] ? @"해뜰때" : @"해질때";
- cell.lblItem.text = title;
- cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@", subItem.sourceName, [self daysOfWeekAtSubItem]];
-
- cell.lblCondition.hidden = YES;
- }
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
-
- subItem = item.heats && item.heats.count ? item.heats[0] : nil;
- if (subItem) {
- NSString *title = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"더울때" : @"추울때";
-
- cell.lblItem.text = title;
- cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@℃ / %@", subItem.sourceName, subItem.cmdclsValue, [self daysOfWeekAtSubItem]];
-
- cell.lblCondition.hidden = YES;
- }
- }
-
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
- if (item.imageFileName) {
- [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
- } else {
- [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:deviceImageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
- }
- } else {
- cell.imgvIcon.image = [self imageForTrigger:item];
- }
- }
- - (NSString *)daysOfWeekAtSubItem {
-
- NSString *daysOfWeek = ksEmptyString;
- ItemModel *daysCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDaysOfWeek];
- if (daysCondition) {
- ItemSubModel *subCondition = daysCondition.subItems[0];
- daysOfWeek = subCondition.daysOfWeek;
- }
- return daysOfWeek;
- }
- - (UIImage *)imageForTrigger:(ItemModel *)item {
- UIImage *image = nil;
-
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
- image = [UIImage imageNamed:@"img_rule_trigger_icon_time"];
-
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
- image = [UIImage imageNamed:@"img_rule_trigger_icon_sunset"];
-
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
- image = [UIImage imageNamed:@"img_rule_trigger_icon_temperature"];
- }
- return image;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- NSInteger section = indexPath.section;
-
- UITableViewCell *cell = nil;
-
- if (section == 0) {//title
- RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
-
- tcell.txtRuleTitle.text = [tcell.txtRuleTitle.text isEmptyString] && _refRuleDetail ? _refRuleDetail.ruleName : tcell.txtRuleTitle.text;
- if (!_txtRuleTitle) {
- _txtRuleTitle = tcell.txtRuleTitle;
- }
-
- [self insertActionAddButton:tcell.btnAdd];
-
- cell = tcell;
-
- } else if (section == 1) {//triggers
-
- ItemModel *item = _triggers[indexPath.row];
-
- RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
- [self configureTriggerCell:tcell item:item];
-
- tcell.lblItem.textColor = kUITextColor04;
-
- 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 *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- ItemSubModel *subItem = deviceItem.subItems[indexPath.row];
-
- RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
- tcell.lblItem.text = subItem.sourceName;
- if ([subItem.deleteYn boolValue]) {
- [tcell.lblItem setStrikethrough:tcell.lblItem.text];
- }
- tcell.lblSubItem.text = subItem.sourceSubName;
- tcell.lblItem.textColor = kUITextColor01;
-
- [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:subItem.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 {
- if (subItem.cmdclsValueMsg) {
- tcell.lblCondition.text = subItem.cmdclsValueMsg;
- } else {
- NSString *condition = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
- tcell.lblCondition.text = [NSString stringWithFormat:@"%@%@ %@", subItem.cmdclsValue, subItem.unit, condition];
- }
- }
-
- [tcell.lblCondition setUnderLine:tcell.lblCondition.text];
-
- tcell.btnDelete.value = subItem;
- if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceActionTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
-
- cell = tcell;
-
- } else if (section == 3) {//push-message
-
- ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
- ItemSubModel *subItem = pushItem.pushes[indexPath.row];
-
- RulesAddTextTableViewCell *tcell = (RulesAddTextTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TextCellIdentifier"];
-
- //높이변경
- [tcell registHeightChangeBlock:^(CGFloat height) {
- textCellChangeHeight += height;
- [self tableView:_tableView reloadRowWhileShowingKeyboard:indexPath];
- }];
-
- if (!_txvMessage) {
- _txvMessage = tcell.txtView;
- }
- _txvMessage.text = [_txvMessage.text isEqualToString:_txvMessage.placeHolder] && _refRuleDetail ? subItem.cmdclsValue : _txvMessage.text;
- [self insertActionAddButton:tcell.btnAdd];
-
- cell = tcell;
- } else if (section == 4) {//conditions
-
- RulesAddConditionTableViewCell *tcell = (RulesAddConditionTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
-
- if (_chkConditions.checked) {
- if (!tcell.container.subviews.count) {
- if ([self isViewLoaded]) {
- [_rcv beginAppearanceTransition:YES animated:NO];
- [tcell.container addSubview:_rcv.view];
- [_rcv endAppearanceTransition];
- }
- }
- _rcv.conditions = _conditions;
- _rcv.refDevices = _conditionDevices;
-
- tcell.constraintContainerHeight.constant = _rcv.tableHeight;
- }
-
- cell = tcell;
- }
-
- return cell;
- }
- //키보드 유지 셀 높이 변경
- - (void)tableView:(UITableView *)tableView reloadRowWhileShowingKeyboard:(NSIndexPath *)indexPath {
-
- [tableView beginUpdates];
- [tableView endUpdates];
-
- [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- if (indexPath.section == 1) {//triggers
- ItemModel *item = _triggers[0];
-
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
- ItemSubModel *subItem = item.subItems[indexPath.row];
- [RulesAddViewController modifyTrigger:item subItem:subItem refDevices:_triggerDevices tableView:_tableView];
-
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
- [self modifyTriggerOfTimer:item];
-
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
- [self modifyTriggerOfDaylight:item];
-
- } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
- [self modifyTriggerOfHeat:item];
- }
-
- } else if (indexPath.section == 2) {//actions
- ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- if (deviceItem) {
- ItemSubModel *subItem = deviceItem.subItems[indexPath.row];
- [RulesAddViewController modifyAction:subItem refDevices:_actionDevices tableView:_tableView];
- }
- }
- }
- #pragma mark
- - (void)modifyTriggerOfTimer:(ItemModel *)item {
-
- TimePickerPopupView *tpopup = [[TimePickerPopupView alloc] initFromNib];
- tpopup.timeTrigger = item;
- tpopup.refConditions = _conditions;
-
- [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//OK
- [_tableView reloadData];
- }
- }];
- }
- - (void)modifyTriggerOfDaylight:(ItemModel *)item {
-
- DaylightPopupView *dpopup = [[DaylightPopupView alloc] initFromNib];
- dpopup.daylightTrigger = item;
- dpopup.refConditions = _conditions;
-
- [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//OK
- [_tableView reloadData];
- }
- }];
- }
- - (void)modifyTriggerOfHeat:(ItemModel *)item {
-
- ExternHeatPopupView *epopup = [[ExternHeatPopupView alloc] initFromNib];
- epopup.externHeatTrigger = item;
- epopup.refConditions = _conditions;
-
- [epopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//OK
- [_tableView reloadData];
- }
- }];
- }
- + (void)modifyTrigger:(ItemModel *)trigger subItem:(ItemSubModel *)subItem refDevices:(NSMutableArray<ItemModel> *)refDevices tableView:(CustomTableView *)tableView {
-
- if ([subItem.deleteYn boolValue]) {
- [[JDFacade facade] toast:@"삭제된 장치입니다\r목록에서 제거하세요"];
- return;
- }
-
- ItemModel *deviceItem = [refDevices objectKey:@"sourceId" eqaulToString:subItem.sourceId];
-
- //노드 선택 팝럽.
- DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
- npopup.refDevice = deviceItem;
- npopup.typeCode = ksItemTypeCodeTrigger;
- npopup.isModifyMode = YES;
-
- [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//OK
-
- [trigger.subItems removeAllObjects];
- ItemSubModel *fsubItem = [deviceItem.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
- [trigger.subItems addObject:fsubItem];
-
- // subItem.cmdclsCode = matchedSubItem.cmdclsCode;
- // subItem.cmdclsTypeId = matchedSubItem.cmdclsTypeId;
- //
- // if (matchedSubItem.cmdclsValueList) {
- // subItem.cmdclsValueList = matchedSubItem.cmdclsValueList;
- //
- // CmdClsValueModel *pCmdClsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
- // subItem.cmdclsValue = pCmdClsValue.cmdclsValue; //CommandClassControlNodeView - 에서 선택됨.
- // }
-
- if (tableView) {
- [tableView reloadData];
- }
- }
- }];
- }
- + (void)modifyAction:(ItemSubModel *)subItem refDevices:(NSMutableArray<ItemModel> *)refDevices tableView:(CustomTableView *)tableView {
-
- if ([subItem.deleteYn boolValue]) {
- [[JDFacade facade] toast:@"삭제된 장치입니다\r목록에서 제거하세요"];
- return;
- }
-
- ItemModel *item = [refDevices objectKey:@"sourceId" eqaulToString:subItem.sourceId];
- ItemSubModel *matchedSubItem = [item.subItems objectKey:@"sourceSubId" eqaulToString:subItem.sourceSubId];
-
- //노드 선택 팝럽.
- DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
- npopup.refDevice = item;
- npopup.typeCode = ksItemTypeCodeTrigger;
- npopup.isModifyMode = YES;
-
- [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//OK
- subItem.cmdclsCode = matchedSubItem.cmdclsCode;
- subItem.cmdclsTypeId = matchedSubItem.cmdclsTypeId;
- subItem.cmdclsValueList = matchedSubItem.cmdclsValueList;
- CmdClsValueModel *pCmdClsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
- subItem.cmdclsValue = pCmdClsValue.cmdclsValue; //CommandClassControlNodeView - 에서 선택됨.
-
- if (tableView) {
-
- [tableView reloadData];
- }
- }
- }];
- }
- #pragma mark - TableView UI Events
- - (void)btnAddTriggerTouched:(id)sender {
-
- TriggerSelectPopupView *tpopup = [[TriggerSelectPopupView alloc] initFromNib];
- tpopup.refTriggers = _triggers;
- tpopup.refDevices = _triggerDevices;
- tpopup.refConditions = _conditions;
-
- [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
-
- ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- if (!deviceItem) {//디바이스 아이템이 없을 경우,
- deviceItem = [[ItemModel alloc] init];
- deviceItem.itemName = @"이 장치를 실행";
- deviceItem.itemSubTypeCode = ksItemSubTypeCodeDevice;
-
- deviceItem.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
-
- [_actions addObject:deviceItem];
- }
-
-
- ItemSubModel *snode = npopup.selectedNode;
- //존재 여부 확인
- if (![deviceItem.subItems objectByUsingPredicateFormat:@"sourceId == %@ && sourceSubId == %@", snode.sourceId, snode.sourceSubId]) {
- [deviceItem.subItems addObject: snode];
- }
-
- [_tableView reloadData];
- }
- }];
- }
- }];
- }
- - (void)btnAddPushMessageTouched:(id)sender {
-
- ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
- if (!pushItem) {//디바이스 아이템이 없을 경우,
- pushItem = [[ItemModel alloc] init];
- pushItem.itemName = [JDFacade facade].loginUser.memberId;
- pushItem.itemSubTypeCode = ksItemSubTypeCodeAppPush;
-
- pushItem.pushes = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
-
- ItemSubModel *push = [[ItemSubModel alloc] init];
- push.conditionTypeCode = @"02";
-
- [pushItem.pushes addObject:push];
- [_actions addObject:pushItem];
- }
-
- [_tableView reloadData];
- }
- - (void)btnDeleteTriggerTouched:(id)sender {
-
- CustomButton *btnDelete = (CustomButton *)sender;
-
- ItemModel *item = btnDelete.value;
-
- if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//장치일 경우
- ItemSubModel *subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
- if (subItem) {
- [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
- for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
- [[JDFacade facade] setRadioButtonStatus:@NO object:cmdclsValue];
- cmdclsValue.isSelected = NO;
- }
- }
- }
-
- if (_triggers && _triggers.count) {
- [_triggers removeAllObjects];
- }
-
- [_tableView reloadData];
- }
- - (void)btnDeleteDeviceActionTouched:(id)sender {
-
- CustomButton *btnDelete = (CustomButton *)sender;
-
- ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
-
- //선택 설정을 초기화
- [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
-
- for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
- [[JDFacade facade] setRadioButtonStatus:@NO object:cmdclsValue];
- cmdclsValue.isSelected = NO;
- }
-
- ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- [deviceItem.subItems removeObject:subItem];
-
- [_tableView reloadData];
- }
- - (void)btnDeletePushActionTouched:(id)sender {
-
- ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
- [_actions removeObject:pushItem];
- [_tableView reloadData];
-
- }
- - (void)btnAddTouched:(id)sender {
- UIButton *btn = sender;
-
- //1001:규칙add, 2001:알림add
-
- UIControl *control;
-
- if ([btn tag] > 2000) {
-
- control = [self.view viewWithTag:2000];
- }
- else {
-
- control = [self.view viewWithTag:1000];
-
- }
-
- [control becomeFirstResponder];
-
- }
- //- (void)btnInputConfirmTouched:(id)sender {
- //
- // UIButton *btn = sender;
- //
- // //1002:규칙add, 2002:알림add
- // if ([btn tag] > 2000) {
- //
- //
- // }
- // else {
- //
- // }
- //
- //}
- - (void)insertActionAddButton:(UIButton*)button {
-
- if (![button actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [button addTarget:self action:@selector(btnAddTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
- }
- //- (void)insertActionInputConfirmButton:(UIButton*)button {
- //
- // if (![button actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- // [button addTarget:self action:@selector(btnInputConfirmTouched:) forControlEvents:UIControlEventTouchUpInside];
- // }
- //}
- - (IBAction)btnCancelTouched:(id)sender {
-
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- #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 (tcell.txtRuleTitle.text.length > 40) {
- [[JDFacade facade] alert:@"규칙이름은 최대 40자까지 입력할 수 있습니다"];
- return;
- }
-
- ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
- if (pushItem) {
- if (_txvMessage && [_txvMessage.text isEmptyString]) {//푸시메시지 여부 확인
- [_actions removeObject:pushItem];
- } else if (_txvMessage) {
- ItemSubModel *subItem = pushItem.pushes[0];
- subItem.cmdclsValue = _txvMessage.text;
-
- if (_txvMessage.text.length > 120) {
- [[JDFacade facade] alert:@"알림메시지는 최대 120자까지 입력할 수 있습니다"];
- return;
- }
- }
- }
-
- if (!_refRuleDetail) {//생성
- [self requestRegisterRule];
-
- } else {//수정
- BOOL hasDeleteNode = NO;
- ItemModel *triggerDevice = [_triggers objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- ItemModel *actionDevice = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- ItemModel *conditionDevice = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
-
- hasDeleteNode = [RulesAddViewController hasDeleteNode:triggerDevice];
- hasDeleteNode = [RulesAddViewController hasDeleteNode:actionDevice] ? YES : hasDeleteNode;
- hasDeleteNode = [RulesAddViewController hasDeleteNode:conditionDevice] ? YES : hasDeleteNode;
-
- if (hasDeleteNode) {
- [[JDFacade facade] confirm:@"삭제된 장치를 제외 후 저장합니다" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
- if (buttonIndex == 0) {//OK
- [self getRidOfDeleteDevices:triggerDevice];
- [self getRidOfDeleteDevices:actionDevice];
- [self getRidOfDeleteDevices:conditionDevice];
-
- [_tableView reloadData];
-
- [self requestModifyRule];
- }
- }];
- } else {
- [self requestModifyRule];
- }
- }
- }
- //삭제된 장치를 제거함.
- - (void)getRidOfDeleteDevices:(ItemModel *)deviceItem {
-
- [deviceItem.subItems enumerateObjectsUsingBlock:^(ItemSubModel *subItem, NSUInteger idx, BOOL * _Nonnull stop) {
- if (subItem.deleteYn) {
- if ([_triggers containsObject:deviceItem]) {
- [_triggers removeObject:deviceItem];
- return;
- }
- [deviceItem.subItems removeObject:subItem];
- }
- }];
- }
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|