// // CommandClassControlNodeView.m // kneet2 // // Created by Jason Lee on 11/5/15. // Copyright © 2015 ntels. All rights reserved. // #import "JDObject.h" #import "RequestHandler.h" #import "JDJSONModel.h" #import "DeviceModel.h" #import "CommandClassControlNodeView.h" #import "MultiLevelPopupView.h" #import "CustomRadioGroup.h" #import "CommandClassControlView.h" #define kfSwitchLabelPadding 20.0f //#define kfCommandClassSwitchLeftPadding 20.0f //#define kfCommandClassSwitchRightPadding 20.0f #define kfCommandClassLockRightPadding 20.0f //#define kfControlDefaultWidth 215.0f; //#define kfControlMaxWidth IPHONE_WIDTH - 105.0f #define kfControlDefaultHeight 35.0f @implementation CommandClassControlNodeView + (CommandClassControlNodeView *)viewForCommandClass:(CmdClsType)cmdClsType { CommandClassControlNodeView *controlView = nil; for (UIView *view in [CommonUtil nibViews:@"CommandClassControlNodeView"]) { if (view.tag == cmdClsType) { controlView = (CommandClassControlNodeView *)view; controlView.width = kfControlDefaultWidth; controlView.height = kfControlDefaultHeight; break; } } return controlView; } - (CGSize)sizeForIntrinsic { return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight); } - (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count { return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight); } - (void)controlNodeAction { if ([self.delegate respondsToSelector:@selector(didControlNodeAction:)]) { [self.delegate didControlNodeAction:self]; } } @end @implementation CommandClassControlNodeSensorBinaryView - (void)willMoveToSuperview:(UIView *)newSuperview { if (!newSuperview) return; [self initControl]; } - (void)initControl { [self setLayoutUI]; } - (void)setLayoutUI { } - (IBAction)btnOpenTouched:(id)sender { } - (IBAction)btnCloseTouched:(id)sender { } @end @implementation CommandClassControlNodeSensorMultiLevelView @end @implementation CommandClassControlNodeValveView - (void)willMoveToSuperview:(UIView *)newSuperview { if (!newSuperview) return; [self initControl]; } - (void)initControl { [self setLayoutUI]; } - (void)setLayoutUI { NSString *stmp = _subItem.cmdclsValueMsgLongest; CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOpen.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding; CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding; _constraintBtnOpenWidth.constant = eWidth; // CGRect sr = self.frame; // sr.size.width = eWidth; // self.frame = sr; CmdClsValueModel *cmdclsValueOn = nil, *cmdclsValueOff = nil; if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) { cmdclsValueOn = _subItem.cmdclsValueList[0]; cmdclsValueOff = _subItem.cmdclsValueList[1]; //default setting _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOn.cmdclsValue; //layout setting [_btnOpen setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal]; [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal]; _btnOpen.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0); _btnClose.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0); } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close cmdclsValueOff = _subItem.cmdclsValueList[0]; //default setting _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOff.cmdclsValue; //layout setting [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal]; _btnClose.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0); [_btnOpen removeFromSuperview]; [self removeConstraint:_constraintBtnSameWidth]; _constraintBtnOpenWidth.constant = 0; _constraintBtnSpace.constant = 0; _constraintBtnCloseWidth.constant = 50; } if ([_subItem.cmdclsValue isEqualToString:cmdclsValueOn.cmdclsValue]) { [self btnOpenTouched:nil]; } else { [self btnCloseTouched:nil]; } } - (IBAction)btnOpenTouched:(id)sender { if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) { [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]]; [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]]; _btnOpen.selected = YES; _btnClose.selected = NO; } if (sender) {//트리거 설정 [self controlNodeAction]; } } - (IBAction)btnCloseTouched:(id)sender { if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) { [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]]; [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]]; _btnOpen.selected = NO; _btnClose.selected = YES; } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]]; _btnClose.selected = YES; } if (sender) {//트리거 설정 [self controlNodeAction]; } } @end @implementation CommandClassControlNodeSwitchBinaryView - (void)willMoveToSuperview:(UIView *)newSuperview { if (!newSuperview) return; [self initControl]; } - (void)initControl { [self setLayoutUI]; } - (void)setLayoutUI { NSString *stmp = _subItem.cmdclsValueMsgLongest; CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOff.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding; CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding; _constraintBtnOnWidth.constant = eWidth; CmdClsValueModel *cmdclsValueOn = _subItem.cmdclsValueList[0]; CmdClsValueModel *cmdclsValueOff = _subItem.cmdclsValueList[1]; //default setting _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOn.cmdclsValue; [_btnOn setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal]; [_btnOff setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal]; _btnOn.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0); _btnOff.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0); if ([_subItem.cmdclsValue isEqualToString:cmdclsValueOn.cmdclsValue]) { [self btnOnTouched:nil]; } else { [self btnOffTouched:nil]; } } - (IBAction)btnOnTouched:(id)sender { [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]]; [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]]; _btnOn.selected = YES; _btnOff.selected = NO; if (sender) {//트리거 설정 [self controlNodeAction]; } } - (IBAction)btnOffTouched:(id)sender { [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]]; [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]]; _btnOn.selected = NO; _btnOff.selected = YES; if (sender) {//트리거 설정 [self controlNodeAction]; } } @end @interface CommandClassControlNodeSwitchMultiLevelView () { MultiLevelPopupView *_mpopup; } @end @implementation CommandClassControlNodeSwitchMultiLevelView - (void)willMoveToSuperview:(UIView *)newSuperview { if (!newSuperview) return; [self initControl]; } - (void)initControl { [self setLayoutUI]; } - (void)setLayoutUI { CGFloat half = ([_subItem.controlMaxValue floatValue] - [_subItem.controlMinValue floatValue]) / 2; NSString *cmdClsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : [CommandClassControlView cmdclsValueForType:_subItem.dataTypeCode value:half]; _subItem.conditionTypeCode = _subItem.conditionTypeCode && ![_subItem.conditionTypeCode isEmptyString] ? _subItem.conditionTypeCode : ksConditionTypeCodeGreatOrEqual; NSString *condition = _subItem.conditionTypeCode && ![_subItem.conditionTypeCode isEmptyString] ? [_subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하" : @"이상"; NSString *contentValue = [NSString stringWithFormat:@"%@%@ %@", cmdClsValue, _subItem.unit, condition]; _subItem.cmdclsValueMsg = contentValue; [_btnMultiLevel setTitle:contentValue forState:UIControlStateNormal]; } - (IBAction)btnMutilLevelTouched:(id)sender { //call popup if (!_mpopup) { _mpopup = [[MultiLevelPopupView alloc] initFromNib]; } _mpopup.refSubItem = _subItem; [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 0) {//OK NSString *condition = [_subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하"; NSString *contentValue = [NSString stringWithFormat:@"%@%@ %@", _subItem.cmdclsValue, _subItem.unit, condition]; _subItem.cmdclsValueMsg = contentValue; [_btnMultiLevel setTitle:contentValue forState:UIControlStateNormal]; if (sender) {//트리거 설정 [self controlNodeAction]; } } }]; } @end