| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- //
- // RulesConditionViewController.m
- // kneet2
- //
- // Created by Jason Lee on 11/25/15.
- // Copyright © 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "ItemModel.h"
- #import "CustomTableView.h"
- #import "CustomLabel.h"
- #import "CustomButton.h"
- #import "CustomImageView.h"
- #import "RulesConditionHomeModePopupView.h"
- #import "RulesAddViewController.h"
- #import "DeviceSelectPopupView.h"
- #import "DeviceNodePopupView.h"
- #import "UIImageView+WebCache.h"
- #import "RulesConditionViewController.h"
- @implementation RulesConditionHeaderTableViewCell
- @end
- @implementation RulesConditionDeviceTableViewCell
- @end
- @implementation RulesConditionTableViewCell
- @end
- @implementation RulesConditionFooterTableViewCell
- @end
- @interface RulesConditionViewController () <UITableViewDataSource, UITableViewDelegate> {
- CustomButton *_btnModeAdd, *_btnDurationAdd;
- NSMutableArray *_arrayForHeader, *_arrayForFooter;
-
- RulesConditionHomeModePopupView *_mpopup;
- }
- @property (weak, nonatomic, readonly) NSMutableArray<ItemSubModel> *modes;
- @property (weak, nonatomic, readonly) NSMutableArray<ItemSubModel> *durations;
- @property (weak, nonatomic, readonly) NSMutableArray<ItemSubModel> *pdevices;
- @end
- #pragma mark - Class Definition
- @implementation RulesConditionViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self initUI];
- [self prepareViewDidLoad];
- }
- - (void)initUI {
- [self initTableViewAsDefaultStyle:_tableView];
- _tableView.scrollEnabled = NO;
- }
- - (void)prepareViewDidLoad {
- }
- - (CGFloat)tableHeight {
- CGFloat height = _isShowingMode ? 0 : 255;
-
- if (_isShowingMode) {
- height = self.modes.count > 0 ? height + 95 + 60 : height; //add row height + header height
- height = self.durations.count > 0 ? height + 95 + 60: height;
- height = self.pdevices.count > 0 ? height + (95 * self.pdevices.count) + 60 : height;
-
- } else {
- height = self.modes.count > 0 ? height + 95 : height;
- height = self.durations.count > 0 ? height + 95 : height;
- height = self.pdevices.count > 0 ? height + (95 * self.pdevices.count) : height;
- }
-
- return height;
- }
- #pragma mark - Main Logic
- - (NSMutableArray<ItemSubModel> *)modes {
- ItemModel *mode = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
- return mode.subItems;
- }
- - (NSMutableArray<ItemSubModel> *)durations {
- ItemModel *duration = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDueDate];
- return duration.subItems;
- }
- - (NSMutableArray<ItemSubModel> *)pdevices {
- ItemModel *pdevice = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- return pdevice.subItems;
- }
- #pragma mark - UITableView DataSource & Delegate
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 3;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- NSInteger count = 0;
-
- if (section == 0) {//mode
- count = self.modes.count;
- } else if (section == 1) {//duration
- count = self.durations.count;
- } else if (section == 2) {//device
- count = self.pdevices.count;
- }
- return count;
- }
- - (NSString *)headerTitleForSection:(NSInteger)section {
- NSString *title = nil;
-
- if (section == 0) {//mode
- title = @"이 홈모드에서만";
-
- } else if (section == 1) {//duration
- title = @"이 기간동안만";
-
- } else if (section == 2) {//devices
- title = @"이 장치가 특정 상태일 때만";
-
- }
- return title;
- }
- - (void)addTargetToHeaderAddButton:(CustomButton *)btnAdd section:(NSInteger)section {
- if (section == 0) {
- _btnModeAdd = btnAdd;
- [btnAdd addTarget:self action:@selector(btnAddModeTouched:) forControlEvents:UIControlEventTouchUpInside];
-
- } else if (section == 1) {
- [btnAdd addTarget:self action:@selector(btnAddDurationTouched:) forControlEvents:UIControlEventTouchUpInside];
-
- } else if (section == 2) {
- _btnDurationAdd = btnAdd;
- [btnAdd addTarget:self action:@selector(btnAddDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- BOOL hasHeader = !_isShowingMode;
- if (section == 0) {
- hasHeader = self.modes.count || !_isShowingMode;
- } else if (section == 1) {
- hasHeader = self.durations.count || !_isShowingMode;
- } else if (section == 2) {
- hasHeader = self.pdevices.count || !_isShowingMode;
- }
-
- UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
- if (!view) {
- if (hasHeader) {
- RulesConditionHeaderTableViewCell *hcell = (RulesConditionHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
- view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
- [view addSubview:hcell];
-
- hcell.lblTitle.text = [self headerTitleForSection:section];
-
- hcell.btnAdd.hidden = _isShowingMode;
- if (!hcell.btnAdd.hidden && ![hcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [self addTargetToHeaderAddButton:hcell.btnAdd section:section];
- }
- } else {
- view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, CGFLOAT_MIN)];
- }
-
- if (_arrayForHeader.count == section) {
- [_arrayForHeader insertObject:view atIndex:section];
- }
-
- } else {
- if (hasHeader) {
- RulesConditionHeaderTableViewCell *hcell = (RulesConditionHeaderTableViewCell *)view.subviews[0];
- hcell.lblTitle.text = hcell.lblTitle.text = [self headerTitleForSection:section];
- }
- }
-
- return view;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- BOOL hasHeader = !_isShowingMode;
- if (section == 0) {
- hasHeader = self.modes.count || !_isShowingMode;
- } else if (section == 1) {
- hasHeader = self.durations.count || !_isShowingMode;
- } else if (section == 2) {
- hasHeader = self.pdevices.count || !_isShowingMode;
- }
- return hasHeader ? 60.0f : CGFLOAT_MIN;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
- BOOL hasFooter = !_isShowingMode;
- if (section == 0) {
- hasFooter = self.modes.count || !_isShowingMode;
- } else if (section == 1) {
- hasFooter = self.durations.count || !_isShowingMode;
- } else if (section == 2) {
- hasFooter = self.pdevices.count || !_isShowingMode;
- }
-
- UIView *view = _arrayForFooter.count > section ? _arrayForFooter[section] : nil;
- if (!view) {
- if (hasFooter) {
- RulesConditionFooterTableViewCell *hcell = (RulesConditionFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FooterCellIdentifier"];
- view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
- [view addSubview:hcell];
- } else {
- view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, CGFLOAT_MIN)];
- }
- if (_arrayForHeader.count == section) {
- [_arrayForHeader insertObject:view atIndex:section];
- }
- }
-
- return view;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
- BOOL hasFooter = !_isShowingMode;
- if (section == 0) {
- hasFooter = self.modes.count || !_isShowingMode;
- } else if (section == 1) {
- hasFooter = self.durations.count || !_isShowingMode;
- } else if (section == 2) {
- hasFooter = self.pdevices.count || !_isShowingMode;
- }
- return hasFooter ? 15.0f : CGFLOAT_MIN;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat height = 95.0f;
- return height;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- UITableViewCell *cell = nil;
- NSInteger section = indexPath.section;
-
- if (section == 0) {//mode
-
- ItemSubModel *subCondition = self.modes[0];
-
- RulesConditionTableViewCell *tcell = (RulesConditionTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
- tcell.lblItem.text = subCondition.cmdclsValueMsg;
-
- tcell.btnDelete.hidden = _isShowingMode;
- if (!tcell.btnDelete.hidden) {
- tcell.btnDelete.value = subCondition;
- if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [tcell.btnDelete addTarget:self action:@selector(btnDeleteModeTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
- }
-
- cell = tcell;
- } else if (section == 1) {//duration
- RulesConditionTableViewCell *tcell = (RulesConditionTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
-
-
- cell = tcell;
- } else if (section == 2) {//action
- RulesConditionDeviceTableViewCell *tcell = (RulesConditionDeviceTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"DeviceCellIdentifier"];
-
- ItemSubModel *subItem = self.pdevices[indexPath.row];
-
- tcell.lblItem.text = subItem.sourceName;
- if ([subItem.deleteYn boolValue]) {//삭제된 노드가 있을 경우,
- [tcell.lblItem setStrikethrough:tcell.lblItem.text];
- }
- tcell.lblSubItem.text = subItem.sourceSubName;
-
- [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 {
- tcell.lblCondition.text = subItem.cmdclsValueMsg;
- }
-
- tcell.btnDelete.hidden = _isShowingMode;
- if (!tcell.btnDelete.hidden) {
- tcell.btnDelete.value = subItem;
- if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
- } else {
- tcell.constraintConditionLabelRight.constant = 15.0f;
- }
- cell = tcell;
- }
-
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [super tableView:tableView didSelectRowAtIndexPath:indexPath];
-
- if (indexPath.section == 2) {//actions
- ItemSubModel *subItem = self.pdevices[indexPath.row];
- [RulesAddViewController modifyAction:subItem refDevices:_refDevices tableView:_tableView];
- }
- }
- #pragma mark - Table Events
- - (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];
- }
-
- _btnModeAdd.hidden = YES;
-
- [self updateTableView];
- }
- }];
- }
- - (void)btnAddDurationTouched:(id)sender {
-
- _btnDurationAdd.hidden = YES;
- }
- - (void)btnAddDeviceTouched:(id)sender {
-
- ItemModel *deviceCondition = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
- BOOL isNewTrigger = deviceCondition == nil;
- deviceCondition = isNewTrigger ? [[ItemModel alloc] init] : deviceCondition;
-
- DeviceSelectPopupView *dpopup = [[DeviceSelectPopupView alloc] initFromNib];
- dpopup.refDevices = _refDevices;
- 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 (isNewTrigger) {
- deviceCondition.itemName = @"이 장치가 특정 상태일 때만";
- deviceCondition.itemSubTypeCode = ksItemSubTypeCodeDevice; //트리거 타입 설정
-
- deviceCondition.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
-
- [_conditions addObject:deviceCondition];
- }
-
- ItemSubModel *snode = npopup.selectedNode;
- //존재 여부 확인
- if (![deviceCondition.subItems objectByUsingPredicateFormat:@"sourceId == %@ && sourceSubId == %@", snode.sourceId, snode.sourceSubId]) {
- [deviceCondition.subItems addObject: snode];
- }
-
- [self updateTableView];
- }
- }];
- }
- }];
- }
- - (void)btnDeleteModeTouched:(id)sender {
- CustomButton *btnDelete = (CustomButton *)sender;
- ItemSubModel *mode = btnDelete.value;
-
- [self.modes removeObject:mode];
-
-
- _btnModeAdd.hidden = NO;
-
- [self updateTableView];
- }
- - (void)btnDeleteDeviceTouched:(id)sender {
-
- CustomButton *btnDelete = (CustomButton *)sender;
- ItemSubModel *pdevice = btnDelete.value;
- //선택 설정을 초기화
- [[JDFacade facade] setRadioButtonStatus:@NO object:pdevice];
-
- for (CmdClsValueModel *cmdclsValue in pdevice.cmdclsValueList) {
- cmdclsValue.isSelected = NO;
- }
- [self.pdevices removeObject:pdevice];
-
- [self updateTableView];
- }
- - (void)updateTableView {
-
- CGFloat height = 255;
-
- height = self.modes.count > 0 ? height + 95 : height;
- height = self.durations.count > 0 ? height + 95 : height;
- height = self.pdevices.count > 0 ? height + (95 * self.pdevices.count) : height;
-
- self.view.height = height;
-
- [_tableView reloadData];
-
- if ([self.parentViewController isKindOfClass:[RulesAddViewController class]]) {
- RulesAddViewController *rvc = (RulesAddViewController *)self.parentViewController;
- [rvc.tableView reloadData];
- }
- }
- #pragma mark - UI Events
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|