// // Xuanduo2SettingController.m // Ifish // // Created by Alex on 2019/4/28. // Copyright © 2019 lianlian. All rights reserved. // #import "Xuanduo2SettingController.h" #import "UINavigationBar+Background.h" #import "TemperatureSetCell.h" #import "ChangeWaterCell.h" #import "Xuanduo2Model.h" #import "TemperatureSetModel.h" #import "setWaringTpModel.h" #import "remindCyclePic.h" #import "MyPickerView.h" #import "PlusTableViewCell.h" #import "SetTimerCell.h" #import "XuanduoSectionHeaderView.h" #import "XuanduoSelectHeader.h" #import "MydatePickerView.h" #import "SetTimerModel.h" #import "ReadTimerModel.h" #import "Xuanduo2DataUtility.h" #import "Xuanduo2TimerModel.h" #define kBtnTag 500 #define kSwitchBtnTag 600 static NSString *temperatureCellid = @"TemperatureSetCell"; static NSString *changeWaterCellid = @"ChangeWaterCell"; static NSString *addCellid = @"PlusTableViewCell"; static NSString *plusflag = @"plusCell"; static NSString *setTimerflag = @"SetTimerCell"; @interface Xuanduo2SettingController () @property(nonatomic,strong) setRemindWaterModel *waterInfmodel; @property(nonatomic,strong) Xuanduo2TimerModel *timerModel; @property (nonatomic,strong) NSMutableDictionary *timeDic; @property(nonatomic,copy) NSString *nextChangeDateLabel; @property(nonatomic,strong) MyHud *XuTohud; @end @implementation Xuanduo2SettingController { NSString *_wenduString; NSIndexPath *_indexPath; MyPickerView *_mypicView; NSArray *_titieArr; NSArray *_imgArr; remindCyclePic*_remindPic; BOOL _huanShuiSwitchIsOn; NSInteger _selectSection; MydatePickerView*_mydatepicView; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (_currentdevice) { [IFISHHTTPTOOL getRemindWaterInfWith:_currentdevice.deviceId huishuiInfo:^(setRemindWaterModel *remindModel) { self.waterInfmodel=remindModel; NSUserDefaults*defuat=[NSUserDefaults standardUserDefaults]; [defuat setInteger:self.waterInfmodel.remindcycle forKey:@"xuto2picResult"]; [defuat synchronize]; self.nextChangeDateLabel=self.waterInfmodel.remindDate; [self.tableView reloadData]; }]; } } - (void)viewDidLoad { [super viewDidLoad]; [self setup]; [self connect]; } - (void)viewWillDisappear:(BOOL)animated{ [_indicatorView stopAnimating]; } - (void)setup { [self.navigationController.navigationBar resetBackgroundImage]; [self addTitleViewWithTitle:@"水族箱设置"]; [self refreshWithData:self.dataModel]; [self setupTable]; [self creatIndicatorView]; [Socketsingleton sharedInstance].communiteDelegate = self; } - (void)refreshWithData:(Xuanduo2Model *)model { NSString*low = model.miniTemp; NSString*heigh = model.maxTemp; UInt64 lowten = [dataContorl hexToTen:low]; UInt64 heighten = [dataContorl hexToTen:heigh]; float formatlow = lowten/10; float formatheigh = heighten/10; _wenduString = [NSString stringWithFormat:@"%.0f~%.0f°C",formatlow,formatheigh]; } - (void)setupTable { _titieArr = @[@"循环泵",@"增氧泵",@"灯光1",@"灯光2",@"造浪泵",@"杀菌灯"]; _imgArr = @[@"xunhuan_big",@"gas_big",@"light_big",@"light_big",@"wave_big",@"shajun_big"]; _selectSection = -1; for (int i = 0; i<_titieArr.count; i++) { NSMutableArray *dataArr = [NSMutableArray arrayWithCapacity:0]; [dataArr addObject:plusflag]; [self.timeDic setObject:dataArr forKey:_titieArr[i]]; } self.tableView.backgroundColor=COLOR_MIAN; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; [self.tableView registerNib:[UINib nibWithNibName:temperatureCellid bundle:nil] forCellReuseIdentifier:temperatureCellid]; [self.tableView registerNib:[UINib nibWithNibName:changeWaterCellid bundle:nil] forCellReuseIdentifier:changeWaterCellid]; [self.tableView registerNib:[UINib nibWithNibName:addCellid bundle:nil] forCellReuseIdentifier:addCellid]; [self.tableView registerNib:[UINib nibWithNibName:setTimerflag bundle:nil] forCellReuseIdentifier:setTimerflag]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2 + self.timeDic.allKeys.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return 1; }else if (section == 1){ return 2; } if (section - 2 < _titieArr.count) { NSArray *dataArr = [self.timeDic objectForKey:_titieArr[section-2]]; return dataArr.count; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0){ TemperatureSetCell *cell = [tableView dequeueReusableCellWithIdentifier:temperatureCellid]; TemperatureSetModel *model=[[TemperatureSetModel alloc]init]; model.title = @"预警设置"; cell.titleLabel.text = model.title; NSLog(@"wendu str = %@",_wenduString); model.temperatureLabel = _wenduString; cell.temperaturelabel.text = model.temperatureLabel; [cell.temPSwitch addTarget:self action:@selector(xuTotempSwitchAction:) forControlEvents:UIControlEventTouchUpInside]; if (self.dataModel) { if ([self.dataModel.alarmSwitch isEqualToString:@"01"]) { [cell.temPSwitch setBackgroundImage:[UIImage imageNamed:@"greenbtn_big"] forState:UIControlStateNormal]; cell.temPSwitch.selected = NO; }else{ [cell.temPSwitch setBackgroundImage:[UIImage imageNamed:@"graybtn_big"] forState:UIControlStateNormal]; cell.temPSwitch.selected = YES; } } return cell; }else if(indexPath.section == 1 && indexPath.row == 0){ if (![self.currentdevice.waterRemind isEqual:[NSNull null]]) { ChangeWaterCell *cell = [tableView dequeueReusableCellWithIdentifier:changeWaterCellid]; [cell.ChangeWaterSwitch addTarget:self action:@selector(xuTochangeWaterSwitch:) forControlEvents:UIControlEventTouchUpInside]; if ([self.waterInfmodel.waterremind isEqualToString:@"1"]) { [cell.ChangeWaterSwitch setBackgroundImage:[UIImage imageNamed:@"greenbtn_big"] forState:UIControlStateNormal]; cell.ChangeWaterSwitch.selected = NO; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"firstSetCell"]]; _huanShuiSwitchIsOn=YES; }else{ [cell.ChangeWaterSwitch setBackgroundImage:[UIImage imageNamed:@"graybtn_big"] forState:UIControlStateNormal]; cell.ChangeWaterSwitch.selected = YES; _huanShuiSwitchIsOn=NO; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"set_cellBack"]]; } if (self.waterInfmodel.remindcycle == 0) { //在未获取换水信息时暂时显示 cell.huanshuiDaylabel.text=@"换水时间"; }else{ cell.huanshuiDaylabel.text=[NSString stringWithFormat:@"%ld天",(long)self.waterInfmodel.remindcycle]; NSLog(@"ce;;%@",cell.huanshuiDaylabel.text); NSLog(@"222"); } return cell; } }else if (indexPath.section==1 && indexPath.row == 1){ static NSString*nextHuanShuiLablecellId=@"labelcell"; UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:nextHuanShuiLablecellId]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nextHuanShuiLablecellId]; } if (_huanShuiSwitchIsOn) { cell.textLabel.text=[NSString stringWithFormat:@"下次换水时间:%@",self.waterInfmodel.remindDate]; if (!self.waterInfmodel.remindDate) { cell.textLabel.text=[NSString stringWithFormat:@"下次换水时间:%@",self.nextChangeDateLabel]; } cell.textLabel.font=[UIFont systemFontOfSize:15]; [cell.textLabel sizeToFit]; cell.textLabel.textColor =[UIColor colorWithRed:146.0/256.0 green:146.0/256.0 blue:146.0/256.0 alpha:1]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"secondSetCell"]]; }else{ cell.textLabel.text=@""; } cell.userInteractionEnabled = NO; return cell; }else { if (indexPath.section - 2 < _titieArr.count && _selectSection == indexPath.section) { NSArray *dataArr = [self.timeDic objectForKey:_titieArr[indexPath.section-2]]; BOOL havePlus = [dataArr containsObject:plusflag]; if (havePlus && (indexPath.row == dataArr.count-1)) { PlusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:addCellid forIndexPath:indexPath]; cell.addTimerBtn.tag = kBtnTag + indexPath.section; [cell.addTimerBtn addTarget:self action:@selector(addTimer:) forControlEvents:UIControlEventTouchUpInside]; return cell; }else { SetTimerCell *cell = [tableView dequeueReusableCellWithIdentifier:setTimerflag forIndexPath:indexPath]; cell.switchBtn.tag = kSwitchBtnTag + indexPath.row; [cell.switchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside]; XuanduoTimerGroup *group = [dataArr objectAtIndex:havePlus?indexPath.row+1:indexPath.row]; BOOL isOn = NO; if ([group.status isEqualToString:@"01"]) { isOn = YES; } [cell configTitleLabel:[NSString stringWithFormat:@"时段%@",group.groupNumber] timer:group.time switchStatus:isOn]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } } } return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { return 50; }else if (indexPath.section == 1 && indexPath.row == 0){ return 50; }else if (indexPath.section == 1 && indexPath.row == 1){ if (!_huanShuiSwitchIsOn) { }else { return 50; } }else if (indexPath.section - 2 <_titieArr.count) { NSArray *dataArr = [self.timeDic objectForKey:_titieArr[indexPath.section-2]]; BOOL havePlus = [dataArr containsObject:plusflag]; if (_selectSection == indexPath.section) { if (havePlus && indexPath.row == dataArr.count - 1) { return 80; }else{ return 50; } }else { return 0; } } return 0; } #pragma mark tableView 头视图和脚视图 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) { return 5; }else if (section == 2){ return 80; }else if (section == 3){ return 40; }else if (section == 4){ return 40; }else if (section == 5){ return 40; }else if (section == 6){ return 40; }else if (section == 7){ return 40; }else { return CGFLOAT_MIN; } } - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0) { UIView *oneHeader = [[UIView alloc] init]; oneHeader.backgroundColor = COLOR_MIAN; return oneHeader; }else if(section == 2){ XuanduoSectionHeaderView *section1HeaderView = [XuanduoSectionHeaderView shareHeaderView]; section1HeaderView.arrowBtn.tag = kBtnTag + section; if (_selectSection == section) { section1HeaderView.arrowBtn.selected = YES; } section1HeaderView.titleLabel.text = _titieArr[section-2]; [section1HeaderView.arrowBtn addTarget:self action:@selector(foldAction:) forControlEvents:UIControlEventTouchUpInside]; return section1HeaderView; }else if(section == 3){ XuanduoSelectHeader *headerView = [XuanduoSelectHeader shareHeaderView]; headerView.controlBtn.tag = kBtnTag + section; if (_selectSection == section) { headerView.controlBtn.selected = YES; } [headerView.controlBtn addTarget:self action:@selector(foldAction:) forControlEvents:UIControlEventTouchUpInside]; [headerView configWithImageName:_imgArr[section-2] labelName:_titieArr[section-2]]; return headerView; }else if(section == 4){ XuanduoSelectHeader *headerView = [XuanduoSelectHeader shareHeaderView]; headerView.controlBtn.tag = kBtnTag + section; if (_selectSection == section) { headerView.controlBtn.selected = YES; } [headerView.controlBtn addTarget:self action:@selector(foldAction:) forControlEvents:UIControlEventTouchUpInside]; [headerView configWithImageName:_imgArr[section-2] labelName:_titieArr[section-2]]; return headerView; }else if(section == 5){ XuanduoSelectHeader *headerView = [XuanduoSelectHeader shareHeaderView]; headerView.controlBtn.tag = kBtnTag + section; if (_selectSection == section) { headerView.controlBtn.selected = YES; } [headerView.controlBtn addTarget:self action:@selector(foldAction:) forControlEvents:UIControlEventTouchUpInside]; [headerView configWithImageName:_imgArr[section-2] labelName:_titieArr[section-2]]; return headerView; }else if(section == 6){ XuanduoSelectHeader *headerView = [XuanduoSelectHeader shareHeaderView]; headerView.controlBtn.tag = kBtnTag + section; if (_selectSection == section) { headerView.controlBtn.selected = YES; } [headerView.controlBtn addTarget:self action:@selector(foldAction:) forControlEvents:UIControlEventTouchUpInside]; [headerView configWithImageName:_imgArr[section-2] labelName:_titieArr[section-2]]; return headerView; }else if(section == 7){ XuanduoSelectHeader *headerView = [XuanduoSelectHeader shareHeaderView]; headerView.controlBtn.tag = kBtnTag + section; if (_selectSection == section) { headerView.controlBtn.selected = YES; } [headerView.controlBtn addTarget:self action:@selector(foldAction:) forControlEvents:UIControlEventTouchUpInside]; [headerView configWithImageName:_imgArr[section-2] labelName:_titieArr[section-2]]; return headerView; } return nil; } #pragma mark tableView 点击事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ _indexPath = indexPath; if (indexPath.section==0) { _mypicView=[[MyPickerView alloc]init]; _mypicView.frame=CGRectMake(0, 0, kScreenSize.width, kScreenSize.height); [self.view.window addSubview:_mypicView]; [_mypicView.sureButton addTarget:self action:@selector(mypicViewRemoveByDone) forControlEvents:UIControlEventTouchUpInside]; [self temperatureMainQueue]; }else if (indexPath.section==1){ //换水提醒日期选择 _remindPic=[[remindCyclePic alloc]init]; _remindPic.frame=CGRectMake(0, 0, kScreenSize.width, kScreenSize.height); [self.view.window addSubview:_remindPic]; [_remindPic.sureBtn addTarget:self action:@selector(xuToremindCyclePicDone) forControlEvents:UIControlEventTouchUpInside]; [self huanShuiMainQueue]; }else { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:_indexPath]; if ([cell isKindOfClass:[PlusTableViewCell class]]) { return; } [self addTimer:nil]; } } -(void)temperatureMainQueue{ NSArray*lowArr=[_mypicView.picArr objectAtIndex:0]; NSArray*heighArr=[_mypicView.picArr objectAtIndex:2]; for (NSInteger i=0; i=0 && [readTimer isEqualToString:@"0116"]) { if (!_timerModel) { _timerModel = [[Xuanduo2TimerModel alloc] init]; _timerModel.groupModelArr = [NSMutableArray array]; }else { [_timerModel.groupModelArr removeAllObjects]; } [Xuanduo2DataUtility readTimerSocketDataWithBackMsgModel:_timerModel addWithBackStr:string1]; NSMutableArray *dataArr = [self.timeDic objectForKey:_titieArr[_selectSection-2]]; [dataArr removeAllObjects]; [dataArr addObject:plusflag]; [dataArr addObjectsFromArray:_timerModel.groupModelArr]; BOOL havePlus = [dataArr containsObject:plusflag]; if (havePlus && dataArr.count > 10) { [dataArr removeObjectAtIndex:0]; }else if (!havePlus && dataArr.count < 10){ [dataArr insertObject:plusflag atIndex:0]; } NSLog(@"dataarr.count = %ld",dataArr.count); [self.XuTohud hidmyHud]; [self.indicatorView stopAnimating]; [self.tableView reloadData]; } if ([readTimer isEqualToString:@"0108"] || [readTimer isEqualToString:@"0105"]) { [Xuanduo2DataUtility readSocketDataWithBackMsgModel:self.dataModel addWithBackData:data]; [self refreshWithData:self.dataModel]; [self.XuTohud hidmyHud]; [self.indicatorView stopAnimating]; [self.tableView reloadData]; } } -(void)ifishDeviceLogInFail{ } -(void)ifishDeviceLogInSuccees{ } -(void)ifishSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{ } @end