// // HAndTChartCell.m // tongxin // // Created by Apple on 2020/4/10. // Copyright © 2020 xTT. All rights reserved. // #import "HAndTChartCell.h" #import "User.h" #import "Device.h" #import "UILabel+BezierAnimation.h" #import "FBYLineGraphView.h" //倒计时时间 #define DefineCountdownTime 60 @interface HAndTChartCell() /// 0:心率 1:体温 @property (assign, nonatomic)int type; @property (strong, nonatomic)NSString *qDayStr; ///定时器 @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, strong) HeartAndTempModel *model; ///图表 @property (nonatomic, strong) FBYLineGraphView *LineGraphView; @end @implementation HAndTChartCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code self.selectionStyle = UITableViewCellSelectionStyleNone; [_qActiveBtn setCornerRadius:_qActiveBtn.height/2.0 borderWidth:1 borderColor:mainColor]; [_qActiveBtn setTitleColor:mainColor forState:0]; [_qActiveBtn addTarget:self action:@selector(nowQueryOneAction:) forControlEvents:UIControlEventTouchUpInside]; [_lastDayBtn setCornerRadius:2]; _lastDayBtn.backgroundColor = mainColor; [_lastDayBtn setTitleColor:[UIColor whiteColor] forState:0]; [_lastDayBtn addTarget:self action:@selector(daychangeAction:) forControlEvents:UIControlEventTouchUpInside]; [_nextDayBtn setCornerRadius:2]; _nextDayBtn.backgroundColor = mainColor; [_nextDayBtn setTitleColor:[UIColor whiteColor] forState:0]; [_nextDayBtn addTarget:self action:@selector(daychangeAction:) forControlEvents:UIControlEventTouchUpInside]; [self setqDayLabelText:nil]; } -(void)setDataModel:(HeartAndTempModel*)model WithType:(int)type WithqDayStr:(NSString*)qDayStr{ [self setqDayLabelText:qDayStr]; _type = type; _model = model; //设置图表 [self setLineGraphViewWithData:model]; _unintLabel.text = @"次/分"; NSString *tempKey = [NSString stringWithFormat:@"%@&%@",HeartMeasurementTimeKey,cUser.cDevice.imei]; _backImageView.image = [UIImage imageNamed:@"icon_details_heart_rate_box_1"]; NSString *precision = @"%d"; if(type == 1){ _unintLabel.text = @"℃"; tempKey = [NSString stringWithFormat:@"%@&%@",TempMeasurementTimeKey,cUser.cDevice.imei]; _backImageView.image = [UIImage imageNamed:@"icon_details_temperature_box_1"]; precision = @"%.01f"; } NSDate *tempDate = [UserDefaults valueForKey:tempKey]; if(tempDate){ //和最近的数据时间比对一下,如果时间间隔小于 DefineCountdownTime 证明已经更新了数据,但是没收到推送 NSTimeInterval tempTime = tempDate.timeIntervalSince1970; if( (tempTime - model.lastDataTime.doubleValue) < DefineCountdownTime){ [self stopTimer]; [_qActiveBtn setTitle:[NSString stringWithFormat:@"开始测量"] forState:0]; }else if ((tempTime - [NSDate date].timeIntervalSince1970) < 1) { [self stopTimer]; [_qActiveBtn setTitle:[NSString stringWithFormat:@"开始测量"] forState:0]; }else{ [_qActiveBtn setTitle:[NSString stringWithFormat:@"测量中(%ds)",(int)(tempTime - [NSDate date].timeIntervalSince1970)] forState:0]; [self startTimer]; _lastTimeLabel.hidden = YES; _dataLabel.text = @"--"; return; } }else{ [self stopTimer]; [_qActiveBtn setTitle:[NSString stringWithFormat:@"开始测量"] forState:0]; } if(model.lastData && ![model.lastData isEqualToString:@"0"]){ if(![_dataLabel.text isEqualToString:model.lastData]){ _dataLabel.text = model.lastData; [_dataLabel animationFromnum:0 toNum:model.lastData.floatValue duration:0.5 precision:precision]; } }else{ _dataLabel.text = @"--"; } if (model.lastDataTime && model.lastDataTime.doubleValue !=0) { //时间 NSString *lastDay = [myHelper getDateFormatWithStr:@"yyyy-MM-dd HH:mm" timeInterval:model.lastDataTime.doubleValue]; _lastTimeLabel.text = [NSString stringWithFormat:@"上一次: %@",lastDay]; _lastTimeLabel.hidden = NO; }else{ // _lastTimeLabel.text = @"--:--"; _lastTimeLabel.hidden = YES; } } - (void)setLineGraphViewWithData:(HeartAndTempModel*)model { if (_LineGraphView) { [_LineGraphView removeFromSuperview]; } // 初始化折线图 _LineGraphView = [[FBYLineGraphView alloc] initWithFrame:CGRectMake(0 , 0, _lineChartContentView.size.width, _lineChartContentView.size.height)]; [self.lineChartContentView addSubview:_LineGraphView]; _LineGraphView.Ycount = 6; // Y轴分割个数 if(_type == 0){ //心率 NSInteger MaxValue = model.upper.integerValue+10; NSInteger MinValue = model.lower.integerValue-10; int tempCount = (int)((MaxValue - MinValue)/(_LineGraphView.Ycount-1)); if (tempCount == 0){ tempCount = 1; } _LineGraphView.maxValue = MinValue+ tempCount*(_LineGraphView.Ycount-1); // Y轴最大值 _LineGraphView.minValue = MinValue; // Y轴最小值 _LineGraphView.upperValue = model.upper.integerValue; _LineGraphView.lowerValue = model.lower.integerValue; } else { //体温 NSInteger MaxValue = model.upper.integerValue+3; NSInteger MinValue = model.lower.integerValue-1; int tempCount = (int)((MaxValue - MinValue)/(_LineGraphView.Ycount-1)); if (tempCount == 0){ tempCount = 1; } _LineGraphView.maxValue = MinValue+ tempCount*(_LineGraphView.Ycount-1); // Y轴最大值 _LineGraphView.minValue = MinValue; // Y轴最小值 // _LineGraphView.maxValue = model.upper.integerValue+2; // Y轴最大值 // _LineGraphView.minValue = model.lower.integerValue-2; // Y轴最小值 _LineGraphView.upperValue = model.upper.floatValue; _LineGraphView.lowerValue = model.lower.floatValue; } _LineGraphView.MainColor = mainColor; _LineGraphView.XYAxisColor = [UIColor lightGrayColor]; NSMutableArray *tempDataList = [NSMutableArray array]; NSArray *dayDataArr = [model.dataListDict valueForKey:_qDayStr]; if(dayDataArr && dayDataArr.count > 0){ for (NSDictionary *item in dayDataArr) { [tempDataList addObject: @{@"timestamp":item[@"timestamp"],@"data":item[@"data"]}]; } } // NSArray *tempArr = @[ // @{@"timestamp":@(1586876882),@"data":@77}, // @{@"timestamp":@(1586873282),@"data":@63}, // @{@"timestamp":@(1586869682),@"data":@86}, // @{@"timestamp":@(1586866082),@"data":@77}, // @{@"timestamp":@(1586862482),@"data":@63}, // @{@"timestamp":@(1586858882),@"data":@86}, // @{@"timestamp":@(1586855282),@"data":@77}, // @{@"timestamp":@(1586851682),@"data":@63}, // @{@"timestamp":@(1586804882),@"data":@86}, // ]; tempDataList = (NSMutableArray*)[[tempDataList reverseObjectEnumerator] allObjects]; [_LineGraphView setXMarkWithDay:_qDayStr TitlesTimeAndValues:tempDataList titleKey:@"timestamp" valueKey:@"data"]; // X轴刻度标签及相应的值 //LineGraphView.xScaleMarkLEN = 60; //设置完数据等属性后绘图折线图 [_LineGraphView mapping]; } - (void)daychangeAction:(UIButton*)sender{ NSString *curDayStr = self.qDayLabel.text; if([self.qDayLabel.text isEqualToString:@"今天"]){ curDayStr = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]; } NSString *swithDayStr ; if([sender isEqual:_lastDayBtn]){ //前一天 //当前位置的天数转换成的时间戳 NSTimeInterval tempTime = [myHelper getDateWithStr:curDayStr dateFormat:@"yyyy-MM-dd"].timeIntervalSince1970; swithDayStr = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:tempTime - 60*60*24]; }else{ //后一天 NSTimeInterval tempTime = [myHelper getDateWithStr:curDayStr dateFormat:@"yyyy-MM-dd"].timeIntervalSince1970; swithDayStr = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:tempTime + 60*60*24]; NSString *todayStr = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]; if ([swithDayStr isEqualToString:todayStr]){ swithDayStr = @"今天"; } } [self setqDayLabelText:swithDayStr]; if(self.switchQueryBlock){ self.switchQueryBlock(_qDayStr); } } -(void)setqDayLabelText:(NSString*)day{ if(day == nil || day.length == 0){ day = @"今天"; } if ([day isEqualToString:@"今天"] || [day isEqualToString:[myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]]) { [self setBtnBadRoGoodFlag:NO WithBtn:_nextDayBtn]; _qDayStr = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]; _qDayLabel.text = @"今天"; }else{ _qDayStr = day; _qDayLabel.text = day; [self setBtnBadRoGoodFlag:YES WithBtn:_nextDayBtn]; } } -(void)nowQueryOneAction:(UIButton*)sender{ if([sender.titleLabel.text isEqualToString:@"开始测量"]){ //调用接口开始测量一次, 收到推送刷新页面就行 @try { WEAKSELF [HeartAndTempModel postHeartAndTempDataWithType:self.type params:@[@{@"frequency":@(-1)}] success:^(id responseObject) { NSString * tempKey; if(weakSelf.type == 0){ //心率 tempKey = [NSString stringWithFormat:@"%@&%@",HeartMeasurementTimeKey,cUser.cDevice.imei]; }else{ //体温 tempKey = [NSString stringWithFormat:@"%@&%@",TempMeasurementTimeKey,cUser.cDevice.imei]; } //设置 倒计时时间后的 时间戳 [UserDefaults setValue:[NSDate dateWithTimeIntervalSince1970:([NSDate date].timeIntervalSince1970 + DefineCountdownTime)] forKey:tempKey]; //先调用一次 [weakSelf calculateFun]; [weakSelf startTimer]; weakSelf.dataLabel.text = @"--"; weakSelf.lastTimeLabel.hidden = YES; } failure:^(NSError * _Nonnull error) { }]; } @catch (NSException *exception) { } @finally { } } } #pragma mark- --------定时器相关方法-------- - (void)startTimer { //如果定时器已开启,先停止再重新开启 if (self.timer) [self stopTimer]; __weak typeof(self) weakSelf = self; self.timer = [NSTimer xr_timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) { [weakSelf calculateFun]; }]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } - (void)stopTimer { [self.timer invalidate]; self.timer = nil; } /// 倒计时计算方法 - (void)calculateFun{ NSString *tempKey; if(self.type == 0){ //心率 tempKey = [NSString stringWithFormat:@"%@&%@",HeartMeasurementTimeKey,cUser.cDevice.imei]; }else{ //体温 tempKey = [NSString stringWithFormat:@"%@&%@",TempMeasurementTimeKey,cUser.cDevice.imei]; } NSDate *tempDate = [UserDefaults valueForKey:tempKey]; if(tempDate){ NSTimeInterval tempTime = tempDate.timeIntervalSince1970; if ( (tempTime - [NSDate date].timeIntervalSince1970) < 1 ) { [UserDefaults removeObjectForKey:tempKey]; [self stopTimer]; [self.qActiveBtn setTitle:@"开始测量" forState:0]; //MARK: 这里倒计时结束时需要回调刷新页面的数据 if(self.measurementEndBlock){ self.measurementEndBlock(); } }else{ [UserDefaults setValue:[NSDate dateWithTimeIntervalSince1970:tempTime] forKey:tempKey]; [self.qActiveBtn setTitle:[NSString stringWithFormat:@"测量中(%ds)",(int)(tempTime - [NSDate date].timeIntervalSince1970)] forState:0]; } }else{ [self stopTimer]; [self.qActiveBtn setTitle:@"开始测量" forState:0]; //MARK: 这里倒计时结束时需要回调刷新页面的数据 if(self.measurementEndBlock){ self.measurementEndBlock(); } } } /** 设置按钮不可选且半透明 @param flag Bad is NO , Good is YES @param button button */ -(void)setBtnBadRoGoodFlag:(BOOL)flag WithBtn:(UIButton*) button{ if(flag){ [button setAlpha:1]; [button setEnabled:YES]; }else{ [button setAlpha:0.5]; [button setEnabled:NO]; } } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end