| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- //
- // DeviceNodePopupView.m
- // kneet2
- //
- // Created by Jason Lee on 11/5/15.
- // Copyright (c) 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "RequestHandler.h"
- #import "JDJSONModel.h"
- #import "CustomLabel.h"
- #import "CustomTextField.h"
- #import "CustomTableView.h"
- #import "CustomImageView.h"
- #import "CustomButton.h"
- #import "CustomCheckBox.h"
- #import "ValidateUtil.h"
- #import "ImageUtil.h"
- #import "UIImageView+WebCache.h"
- #import "DeviceNodePopupView.h"
- #import "CustomRadioGroup.h"
- #import "CommandClassControlNodeView.h"
- @implementation DeviceNodePopupTableViewCell
- @end
- @interface DeviceNodePopupView () <UITableViewDataSource, UITableViewDelegate, CustomCheckBoxDelegate, CommandClassControlNodeViewDelegate> {
- CustomRadioReusableGroup *_rgroup;
-
- BOOL _isNotFirstLoading, _hasSelectedNode;
- UIImage *_bgCellImage1, *_bgCellImage2;
-
- NSMutableArray<ItemModel> *_checkedItems;
- NSMutableArray *_controlViewArray;
- }
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *constTableHeight;
- @end
- @implementation DeviceNodePopupView
- - (id)initFromNib {
-
- for (UIView *view in [CommonUtil nibViews:@"DeviceNodePopupView"]) {
- if ([view isKindOfClass:[DeviceNodePopupView class]]) {
- self = (DeviceNodePopupView *)view;
-
- //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
- self.frame = [UIScreen mainScreen].bounds;
-
- [self initTableViewAsDefaultStyle:_tableView];
-
- UINib *nib = [UINib nibWithNibName:@"DeviceNodePopupTableViewCell" bundle:nil];
- [_tableView registerNib:nib forCellReuseIdentifier:@"NodeCellIdentifier"];
-
- _controlViewArray = [[NSMutableArray alloc] init];
-
- //set radio group
- _rgroup = [[CustomRadioReusableGroup alloc] init];
- _rgroup.tableView = _tableView;
-
- [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
-
-
- [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
- [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
-
-
- [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
- [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
-
- //Localization
- [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
- [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
-
- UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 2);
- _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_01"]];
- _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_02"]];
-
- }
- }
-
- return self;
- }
- - (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
- tableView.dataSource = self;
- tableView.delegate = self;
-
- tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- tableView.backgroundColor = [UIColor clearColor];
-
- tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
- }
- - (void)didMoveToSuperview {
-
- if (_refDevice.subItems.count == 1) {
-
- _constTableHeight.constant = 130.f;
- }
- }
- - (void)setRefDevice:(ItemModel *)refDevice {
-
- _refDevice = refDevice;
-
- //NSLog(@"refDevice : %@", refDevice);
-
- self.lblTitle.text = _refDevice.sourceName;
- // [self.imgvDevice sd_setImageWithURL:[NSURL URLWithString:_refDevice.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
-
- _hasSelectedNode = [_refDevice.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES] != nil; //이미 선택된 노드가 있는 지 확인,
- [_tableView reloadData];
- }
- - (NSArray<ItemModel> *)selectedItems {
- NSArray<ItemModel> *items = nil;
-
- if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
- items = _checkedItems;
- } else {
- items = (NSArray<ItemModel> *)@[_rgroup.valueForChecked];
- }
-
- return items;
- }
- - (ItemSubModel *)selectedNode {
-
- return _rgroup.valueForChecked;
- }
- #pragma mark - Main Logic
- #pragma mark - UITableView DataSource & Delegate
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _refDevice.subItems ? _refDevice.subItems.count : 0;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat height = 130;
-
- return height;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- ItemSubModel *subItem = _refDevice.subItems[indexPath.row];
-
- DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"NodeCellIdentifier"];
-
- tcell.lblNodeName.text = subItem.sourceSubName;
-
- //뷰를 초기화함.
- [[tcell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- UIView *subview = (UIView *)obj;
- [subview removeFromSuperview];
- }];
-
- if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {//트리거 또는 컨디션
- tcell.chkSelect.hidden = YES;
- tcell.rdoSelect.value = subItem;
-
- if (indexPath.row == 0 && !_isNotFirstLoading && !_hasSelectedNode) {
-
- _isNotFirstLoading = YES;
- tcell.rdoSelect.checked = YES;
-
- } else {
-
- tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
- }
-
- //추가일 경우, 현재 노드 및 선택된 노드 제외
- //수정일 경우, 현재 노드와 선택되지 않은 노드를 선택 가능
- if (!_isModifyMode) {
-
- }
- // tcell.rdoSelect.enabled = _isModifyMode;
-
-
- [_rgroup addRadioButton:tcell.rdoSelect];
-
- } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//액션
-
- tcell.rdoSelect.hidden = YES;
- tcell.chkSelect.delegate = self;
- tcell.chkSelect.value = subItem;
- tcell.chkSelect.checked = [tcell.chkSelect getCheckStatusFromValue];
-
- }
-
- CommandClassControlNodeView *controlView = _controlViewArray.count > indexPath.row ? _controlViewArray[indexPath.row] : nil;
-
- //가스락인데 실행조건>장치상태 일때 온오프 할 수 있을 수 있다. 동작할 장치에서는 불가능
- NSInteger type = subItem.cmdclsType;
- if (type == 17 && subItem.cmdclsValueList.count == 2) {
- type = 1; //토글
- }
-
- if (!controlView) {
- controlView = [CommandClassControlNodeView viewForCommandClass:type];
- controlView.delegate = self;
-
- [_controlViewArray addObject:controlView];
- }
-
- controlView.nodeEnable = tcell.rdoSelect.checked;
- controlView.subItem = subItem;
- tcell.controlContainer.hidden = !controlView;
-
- if (!tcell.controlContainer.hidden) {
-
- UIView *superview = tcell.controlContainer;
- [superview addSubview:controlView];
- [controlView fillToSuperView];
-
- // [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
- //
- // make.size.mas_equalTo(controlView.frame.size);
- // make.left.equalTo(superview);
- // make.top.equalTo(superview);
- // }];
- }
-
-
- //set background image
- if (indexPath.row % 2 == 0) {
- tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
- } else {
- tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
- }
-
- return tcell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
-
- NSLog(@"didSelectRowAtIndexPath");
-
-
- if ([_typeCode isEqualToString:ksItemTypeCodeTrigger] || [_typeCode isEqualToString:ksItemTypeCodeCondition]) {
-
- [_rgroup someRadioButtonTouched:tcell.rdoSelect];
-
- } else if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {
-
- [tcell.chkSelect checkBoxClicked];
-
- }
-
- [_tableView reloadData];
- }
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
-
- // Remove seperator inset
- if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
- [cell setSeparatorInset:UIEdgeInsetsZero];
- }
-
- // Prevent the cell from inheriting the Table View's margin settings
- if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
- [cell setPreservesSuperviewLayoutMargins:NO];
- }
-
- // Explictly set your cell's layout margins
- if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
- [cell setLayoutMargins:UIEdgeInsetsZero];
- }
- }
- #pragma mark - CustomCheckBox Delegate
- - (void)didCheckBoxClicked:(id)sender {
- [_tableView reloadData];
- }
- #pragma mark - CommandClassControlNodeView Delegate
- - (void)didControlNodeAction:(id)sender {
-
- NSLog(@"didControlNodeAction");
-
- CommandClassControlNodeView *nodeView = (CommandClassControlNodeView *)sender;
-
- [_tableView enumarateTableViewCellsUsingBlock:^(UITableViewCell *cell, BOOL *stop) {
- DeviceNodePopupTableViewCell *tcell = (DeviceNodePopupTableViewCell *)cell;
-
- if ([tcell.controlContainer isEqual:nodeView.superview]) {
- [_rgroup someRadioButtonTouched:tcell.rdoSelect];
- *stop = YES;
- }
- }];
- }
- #pragma mark - UI Events
- - (IBAction)btnConfirmTouched:(id)sender {
- //validate
- if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티
-
- if ([_typeCode isEqualToString:ksItemTypeCodeAction]) {//디바이스 멀티 BOOL isSelected = NO;
-
- _checkedItems = (NSMutableArray<ItemModel> *)[_refDevice.subItems matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
- if (!_checkedItems || !_checkedItems.count) {
- [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
- return;
- }
- }
- }
- else {//동일한 노드의 액션이 이미 선택되었는지 체크,
-
- ItemSubModel *subItem = _rgroup.valueForChecked;
-
- // CmdClsValueModel *cmdclsValue = nil;
-
- for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
-
- if (!_passAlreadyAdded && !_isModifyMode && cmdclsValue.isSelected) {
- [[JDFacade facade] toast:@"이미 추가되어 있습니다"];
- return;
- }
-
- cmdclsValue.isSelected = [[[JDFacade facade] getRadioButtonStatus:cmdclsValue] boolValue];
- if (cmdclsValue.isSelected) {
-
- subItem.cmdclsValue = cmdclsValue.cmdclsValue;
- subItem.cmdclsValueMsg = cmdclsValue.cmdclsValueMsg;
- }
-
- }
-
- }
-
-
- [super btnConfirmTouched:sender];
-
- }
- @end
|