// // HAndTViewController.m // LekangGuard // // Created by ecell on 2022/11/25. // #import "HAndTViewController.h" #import "HAndTChartCell.h" #import "HeartAndTempModel.h" #import "HAndTTableViewCell.h" #import "HAndTSwitchTableViewCell.h" @interface HAndTViewController () /// 编辑上报频率的容器View @property (strong, nonatomic) UIView * editingFrequencyView; /// 编辑上报频率的TextField @property (strong, nonatomic) UITextField * editingFrequencyTextField; @property (strong, nonatomic) UITableView *myTableView; @property (strong, nonatomic) HeartAndTempModel* model; @property (nonatomic ,strong) NSString *qDayStr; @property (nonatomic ,strong)NSMutableArray *myDataSource; @end @implementation HAndTViewController - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self loadDataWithQdayStr:nil]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = KKBackgroundGrey; self.zx_navTitle = self.type == 1 ? GJText(@"心率") : GJText(@"体温"); [self initUI]; } - (void)loadDataWithQdayStr:(NSString*)qDay { kWeakSelf(self) if(!qDay || qDay.length == 0) { //今天 self.qDayStr = [UICommon getDateFormatWithStr:@"yyyy-MM-dd" date:[NSDate date]]; } else { self.qDayStr = qDay; } // add by lsz 21-12-28 @try{}@catch{} @try { [HeartAndTempModel getHeartAndTempDataWithType:self.type date:self.qDayStr success:^(HeartAndTempModel * _Nonnull model) { weakself.model = model; [weakself.myTableView reloadData]; } failure:^(NSError * _Nonnull error) { }]; } @catch (NSException *exception) { NSLog(@"HeartAndTemp err=%@",exception); } } - (void)initUI{ if(self.type == 1){ self.title = GJText(@"心率"); self.myDataSource[1] = @[GJText(@"心率开关")]; }else{ self.title = GJText(@"体温"); self.myDataSource[1] = @[GJText(@"体温开关")]; } _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, iPhoneX_NavHeight, SCREEN_WIDTH, SCREEN_HEIGHT-iPhoneX_NavHeight) style:UITableViewStyleGrouped]; _myTableView.backgroundColor = KKClearColor; _myTableView.delegate = self; _myTableView.dataSource = self; [self.view addSubview:_myTableView]; _myTableView.bounces = NO; //禁止弹跳 _myTableView.showsHorizontalScrollIndicator = NO; _myTableView.showsVerticalScrollIndicator = NO; //去线 _myTableView.separatorStyle = UITableViewCellSelectionStyleNone; // iOS 11 系统 tableView 会自动调整高度 在 Gounp 模式下 头部的高度会有问题 ,需要对 预设的高度设置为0 _myTableView.estimatedRowHeight = 0; _myTableView.estimatedSectionHeaderHeight = 0; _myTableView.estimatedSectionFooterHeight = 0; [_myTableView registerNib:[UINib nibWithNibName:@"HAndTChartCell" bundle:nil] forCellReuseIdentifier:@"HAndTChartCellID"]; [_myTableView registerClass:HAndTSwitchTableViewCell.class forCellReuseIdentifier:NSStringFromClass(HAndTSwitchTableViewCell.class)]; [_myTableView registerClass:HAndTTableViewCell.class forCellReuseIdentifier:NSStringFromClass(HAndTTableViewCell.class)]; } -(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 0.01; }else if(section == 1) { return AdaptedHeight(15); }else{ return 1; } } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *title = self.myDataSource[indexPath.section][indexPath.row]; if([title isEqualToString:@"图表"]){ return 491; }else{ return Adapted(50); } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *title = self.myDataSource[indexPath.section][indexPath.row]; kWeakSelf(self) if([title isEqualToString:@"图表"]) { HAndTChartCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HAndTChartCellID" forIndexPath:indexPath]; if(_model){ [cell setDataModel:_model WithType:_type WithqDayStr:self.qDayStr]; } cell.measurementEndBlock = ^{ [weakself loadDataWithQdayStr:nil]; }; cell.switchQueryBlock = ^(NSString * _Nonnull qDay) { [weakself loadDataWithQdayStr:qDay]; }; return cell; } else if ([title containsString:@"开关"]) { HAndTSwitchTableViewCell *cell = [HAndTSwitchTableViewCell cellWithTableView:tableView indexPath:indexPath]; if(_model) cell.isSwitchOpen = [_model.switchOn boolValue]; cell.titleStr = F(@"%@%@", GJText(self.type == 1 ? GJText(@"心率") : GJText(@"体温")),GJText(@"开关")); cell.isSwitch = ^(BOOL isOpen){ NSLog(@"切换开关"); if(weakself.model) { [HeartAndTempModel postHeartAndTempDataWithType:weakself.type cmd:1 obj:isOpen ? @"1" : @"0" success:^(id _Nonnull responseObject){ [UICommon MessageSuccessText:@"修改成功"]; weakself.model.switchOn = @(isOpen); [weakself.myTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationNone]; } failure:^(NSError * _Nonnull error) { [weakself.myTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationNone]; }]; } }; return cell; } else { HAndTTableViewCell *cell = [HAndTTableViewCell cellWithTableView:tableView indexPath:indexPath]; cell.textStr = [NSString stringWithFormat:GJText(@"%@分钟/次"),self.model.frequency]; return cell; } } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; if([title containsString:GJText(@"频率")]){ //显示 [self showChangeFrequency]; } } -(void)showChangeFrequency { if(!_editingFrequencyView) { _editingFrequencyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)]; _editingFrequencyView.backgroundColor = [UIColor whiteColor]; CGFloat alertViewWidth = (320.f - 20*2 - 20*2); _editingFrequencyTextField = [[UITextField alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - alertViewWidth)/2, 1, alertViewWidth, 48)]; _editingFrequencyTextField.borderStyle = UITextBorderStyleRoundedRect; _editingFrequencyTextField.layer.borderColor = RGB(229, 229, 229).CGColor; _editingFrequencyTextField.backgroundColor = RGB(250, 250, 250); _editingFrequencyTextField.placeholder = GJText(@"请输入上报频率(分钟/次)"); _editingFrequencyTextField.keyboardType = UIKeyboardTypeNumberPad; [_editingFrequencyView addSubview:_editingFrequencyTextField]; } _editingFrequencyTextField.text = self.model.frequency; kWeakSelf(self) LGAlertView *alertView = [[LGAlertView alloc] initWithViewAndTitle:GJText(@"上报频率") message:@"" style:LGAlertViewStyleAlert view:_editingFrequencyView buttonTitles:@[GJText(@"确定")] cancelButtonTitle:GJText(@"取消") destructiveButtonTitle:nil actionHandler:^(LGAlertView * _Nonnull alertView, NSUInteger index, NSString * _Nullable title) { [weakself.editingFrequencyTextField resignFirstResponder]; NSLog(@"修改 上报频率 %@",weakself.editingFrequencyTextField.text); NSNumber *tempFrequency = @([weakself.model.frequency intValue]); @try { tempFrequency = @([weakself.editingFrequencyTextField.text intValue]); } @catch (NSException *exception) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UICommon MessageErrorText:@"请输入正确的数值"]; }); return ; } @finally { } NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setValue:tempFrequency forKey:@"frequency"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [HeartAndTempModel postHeartAndTempDataWithType:weakself.type cmd:2 obj:F(@"%@", tempFrequency) success:^(id _Nonnull responseObject) { [UICommon MessageSuccessText:@"修改成功"]; weakself.model.frequency = weakself.editingFrequencyTextField.text; [weakself.myTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationNone]; } failure:^(NSError * _Nonnull error) { [weakself.myTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationNone]; }]; }); } cancelHandler:^(LGAlertView * _Nonnull alertView) { [weakself.editingFrequencyTextField resignFirstResponder]; } destructiveHandler:^(LGAlertView * _Nonnull alertView) { }]; [alertView showAnimated:YES completionHandler:nil]; } - (NSMutableArray *)myDataSource { if(!_myDataSource) { _myDataSource = [NSMutableArray arrayWithArray:@[@[@"图表"],@[@"开关"],@[@"上报频率"]]]; } return _myDataSource; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end