// // TimingSwitchVC.m // tongxin // // Created by Apple on 2020/4/10. // Copyright © 2020 xTT. All rights reserved. // #import "TimingSwitchVC.h" #import "TableCell.h" #import "TimingSwitchModel.h" @interface TimingSwitchVC () @property (strong, nonatomic) UITableView *myTableView; @property (strong, nonatomic) TimingSwitchModel *model; @end @implementation TimingSwitchVC @synthesize myDataSource = _myDataSource; -(NSMutableArray *)myDataSource{ if(!_myDataSource){ _myDataSource = [NSMutableArray arrayWithArray:@[@[@"定时开关机"],@[@"开机时间",@"关机时间"]]]; } return _myDataSource; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self loadData]; } - (void)viewDidLoad { [super viewDidLoad]; [self zx_setRightBtnWithText:@"保存" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) { [self saveAction]; }]; [self initUI]; } - (void)loadData{ WEAKSELF [TimingSwitchModel getTimingSwitchDataSuccess:^(TimingSwitchModel * _Nonnull model) { weakSelf.model = model; [weakSelf.myTableView reloadData]; } failure:^(NSError * _Nonnull error) { }]; } - (void)initUI{ self.zx_navTitle = @"定时开关机"; _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(16, iPhoneX_NavHeight, ScreenWidth-32, ScreenHeight-iPhoneX_NavHeight) style:UITableViewStyleGrouped]; _myTableView.backgroundColor = tabViewBG; _myTableView.delegate = self; _myTableView.dataSource = self; _myTableView.estimatedSectionHeaderHeight = 0; _myTableView.estimatedSectionFooterHeight = 0; [self.view addSubview:_myTableView]; _myTableView.bounces = NO; //禁止弹跳 _myTableView.showsHorizontalScrollIndicator = NO; _myTableView.showsVerticalScrollIndicator = NO; //去线 _myTableView.separatorStyle = UITableViewCellSelectionStyleNone; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.myDataSource.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.myDataSource[section] count]; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) return 16; else return 12; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return section == 1 ? 10 + 70 + 10 : 0.1; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if (section == 1) { UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; UILabel *lable = [UICommon ui_label:CGRectMake(0, 0, ScreenWidth-30, 100) lines:0 align:NSTextAlignmentLeft font:Font_(12) textColor:KKGrey163 text:@"" Radius:0]; lable.text = @"温馨提示:\n为了增加设备的电池续航,平台已经为你开启定时开关机功能,您可以选择关闭"; [view addSubview:lable]; return view; } return nil; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 56; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *title = self.myDataSource[indexPath.section][indexPath.row]; WEAKSELF if([title isEqualToString:@"定时开关机"]) { baseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"baseCell"]; if (!cell) { cell = [[baseCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"baseCell"]; cell.textLabel.font = DefineFontSize; cell.textLabel.text = title; UISwitch *uiSwitch = [[UISwitch alloc] init]; cell.accessoryView = uiSwitch; [uiSwitch addTarget:cell action:@selector(cellClick:) forControlEvents:UIControlEventValueChanged]; } [UICommon ui_uiViewFillet:CGRectMake(0, 0, ScreenWidth-30, Adapted(50)) Viewdirection:PYUIdirectionNo toView:cell sizeMake:10]; UISwitch *uiSwitch = (UISwitch *)cell.accessoryView; uiSwitch.onTintColor = mainColor; uiSwitch.on = [self.model.switchStatus boolValue]; cell.textLabel.text = title; cell.block = ^(UISwitch *uiSwitch, TextFieldCell *blockCell){ //切换开关 xLog(@"切换开关uiSwitch.on : %d", uiSwitch.on); weakSelf.model.switchStatus = @(uiSwitch.on); }; // [UICommon tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath cellCoutn:1]; return cell; } else { TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell"]; cell.textField.font = FontBold_(14); cell.textField.textColor = KKBlack20; if (!cell) { cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TextFieldCell"]; if (indexPath.row == 1) { UIImageView *line = [UIImageView new]; line.frame = CGRectMake(15, 0, ScreenWidth-60, 0.5); line.backgroundColor = RGB(240, 240, 240); [cell addSubview:line]; } } [UICommon tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath cellCoutn:2 sizeH:0]; cell.textLabel.text = title; NSDateFormatter * df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"HH:mm"]; cell.textField.tintColor = [UIColor clearColor]; //使光标不可见 cell.accessoryType = UITableViewCellAccessoryNone; [cell.textField mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(cell.mas_right).inset(15); make.centerY.equalTo(cell); make.size.mas_equalTo(CGSizeMake(150, 30)); }]; NSString *timeString = @"--:--"; if ([title isEqualToString:@"开机时间"]) { if (self.model) { timeString = self.model.startTime; } }else if([title isEqualToString:@"关机时间"]){ if (self.model) { timeString = self.model.endTime; } } if(self.model){ [cell timeInputViewWithModel:UIDatePickerModeTime WithData:[df dateFromString:timeString]]; } cell.textField.text = timeString; cell.block = ^(NSString *text, TextFieldCell *blockCell){ if ([title isEqualToString:@"开机时间"]) { weakSelf.model.startTime = text; }else if([title isEqualToString:@"关机时间"]){ weakSelf.model.endTime = text; } }; return cell; } return nil; } -(BOOL)checkTimeRuleStartTime:(NSString*)aStartTime endTime:(NSString*)aEndTime { //比较2个时间段 开始时间需小于结束时间 NSDateFormatter * df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"HH:mm"]; double startTime =[df dateFromString:aStartTime].timeIntervalSince1970; double endTime =[df dateFromString:aEndTime].timeIntervalSince1970; // if(startTime >= endTime ){ // LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"提示" message:@"开机时间需小于关机时间" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:nil destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { // // } cancelHandler:nil destructiveHandler:nil]; // [alertView showAnimated:YES completionHandler:nil]; // return NO; // } return YES; } -(void)saveAction { WEAKSELF if([self checkTimeRuleStartTime:_model.startTime endTime:_model.endTime]){ [self.model postTimingSwitchDataSuccess:^(id _Nonnull responseObject) { xLog(@"保存定时开关机成功"); [UICommon MessageSuccessText:@"定时开关机设置成功" isImg:YES]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UICommon HidenLoading]; [weakSelf.navigationController popViewControllerAnimated:YES]; }); } failure:^(NSError * _Nonnull error) { }]; } } @end