You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

205 lines
7.3 KiB

//
// 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 ()<UITableViewDelegate,UITableViewDataSource>
@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 initUI];
}
- (void)loadData{
WEAKSELF
[TimingSwitchModel getTimingSwitchDataSuccess:^(TimingSwitchModel * _Nonnull model) {
weakSelf.model = model;
[weakSelf.myTableView reloadData];
} failure:^(NSError * _Nonnull error) {
}];
}
- (void)initUI{
self.title = @"定时开关机";
[self zx_setRightBtnWithText:@"保存" clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
[self saveAction];
}];
_myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, iPhoneX_NavHeight, ScreenWidth, ScreenHeight-getRectNavAndStatusHight) style:UITableViewStylePlain];
_myTableView.backgroundColor = tabViewBG;
_myTableView.delegate = self;
_myTableView.dataSource = self;
[self.view addSubview:_myTableView];
_myTableView.bounces = NO; //禁止弹跳
_myTableView.showsHorizontalScrollIndicator = NO;
_myTableView.showsVerticalScrollIndicator = NO;
//去线
// _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.myDataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 0.01;
}
return 15 * FIX_SCREEN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 10 + 70 + 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake((SWIDTH - 310 * FIX_SCREEN) / 2, 10,
310 * FIX_SCREEN, 100)];
lable.text = @"温馨提示:\n为了增加设备的电池续航,平台已经为你开启定时开关机功能,您可以选择关闭";
lable.numberOfLines = 0;
lable.font = [UIFont systemFontOfSize:14];
lable.textColor = [UIColor lightGrayColor];
[view addSubview:lable];
return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 49;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *title = self.myDataSource[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];
}
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);
};
return cell;
}else{
TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell"];
if (!cell) {
cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TextFieldCell"];
}
cell.textLabel.text = title;
NSDateFormatter * df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"HH:mm"];
cell.textField.tintColor = [UIColor clearColor]; //使光标不可见
cell.textField.font = DefineFontSize;
cell.accessoryType = UITableViewCellAccessoryNone;
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(@"保存定时开关机成功");
[SVProgressHUD showSuccessWithStatus:@"定时开关机设置成功"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
[weakSelf.navigationController popViewControllerAnimated:YES];
});
} failure:^(NSError * _Nonnull error) {
}];
}
}
@end