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.

405 lines
14 KiB

2 years ago
//
// HAndTChartCell.m
// tongxin
//
// Created by Apple on 2020/4/10.
// Copyright © 2020 xTT. All rights reserved.
//
#import "HAndTChartCell.h"
#import "UILabel+BezierAnimation.h"
#import "FBYLineGraphView.h"
#import "ZZBadgeButton.h"
@interface NSTimer (XRTimer)
+ (NSTimer *)xr_timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *))block;
@end
@implementation NSTimer (XRTimer)
+ (NSTimer *)xr_timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *))block {
if ([self respondsToSelector:@selector(timerWithTimeInterval:repeats:block:)]) {
return [self timerWithTimeInterval:interval repeats:repeats block:block];
}
return [self timerWithTimeInterval:interval target:self selector:@selector(timerAction:) userInfo:block repeats:repeats];
}
+ (void)timerAction:(NSTimer *)timer {
void (^block)(NSTimer *timer) = timer.userInfo;
if (block) block(timer);
}
@end
//倒计时时间
#define DefineCountdownTime 60
@interface HAndTChartCell()
/// 1:心率 2:体温
@property (assign, nonatomic)NSInteger 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;
[_lastDayBtn setTitle:GJText(@"前一天") forState:0];
[_nextDayBtn setTitle:GJText(@"后一天") forState:0];
CGRect rect = [UICommon GetTextWidth:_lastDayBtn.currentTitle ViewHeight:24 fontSize:Font_(12) type:@"w"];
CGRect rect1 = [UICommon GetTextWidth:_nextDayBtn.currentTitle ViewHeight:24 fontSize:Font_(12) type:@"w"];
[_lastDayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(15);
make.top.equalTo(self).offset(15);
make.size.mas_equalTo(CGSizeMake(Adapted(rect.size.width+10), 24));
}];
[_nextDayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.mas_right).inset(15);
make.centerY.equalTo(_lastDayBtn);
make.size.mas_equalTo(CGSizeMake(Adapted(rect1.size.width+10), 24));
}];
[_qActiveBtn setCornerRadius:_qActiveBtn.height/2.0 borderWidth:1 borderColor:KKMainColor];
[_qActiveBtn setTitleColor:KKMainColor forState:0];
[_qActiveBtn addTarget:self action:@selector(nowQueryOneAction:) forControlEvents:UIControlEventTouchUpInside];
[_lastDayBtn setCornerRadius:2];
_lastDayBtn.backgroundColor = KKMainColor;
[_lastDayBtn setTitleColor:[UIColor whiteColor] forState:0];
[_lastDayBtn addTarget:self action:@selector(daychangeAction:) forControlEvents:UIControlEventTouchUpInside];
[_nextDayBtn setCornerRadius:2];
_nextDayBtn.backgroundColor = KKMainColor;
[_nextDayBtn setTitleColor:[UIColor whiteColor] forState:0];
[_nextDayBtn addTarget:self action:@selector(daychangeAction:) forControlEvents:UIControlEventTouchUpInside];
[self setqDayLabelText:nil];
}
-(void)setDataModel:(HeartAndTempModel*)model WithType:(NSInteger)type WithqDayStr:(NSString*)qDayStr
{
[self setqDayLabelText:qDayStr];
_type = type;
_model = model;
//设置图表
[self setLineGraphViewWithData:model];
_unintLabel.text = type == 1 ? GJText(@"次/分") : GJText(@"℃");
NSString *tempKey = [NSString stringWithFormat:@"%@&%@",type == 1 ? HeartMeasurementTimeKey : TempMeasurementTimeKey,APIManager.sharedManager.deviceModel.imei];
_backImageView.image = type == 1 ? ImageName_(@"icon_details_heart_rate_box_1") : ImageName_(@"icon_details_temperature_box_1");
NSString *precision = type == 1 ? @"%ld" : @"%.1f";
NSDate *tempDate = [UserDefaults valueForKey:tempKey];
if(tempDate)
{
//和最近的数据时间比对一下,如果时间间隔小于 DefineCountdownTime 证明已经更新了数据,但是没收到推送
NSTimeInterval tempTime = tempDate.timeIntervalSince1970;
if( (tempTime - model.lastDataTime.doubleValue) < DefineCountdownTime)
{
[self stopTimer];
[_qActiveBtn setTitle:[NSString stringWithFormat:GJText(@"开始测量")] forState:0];
}
else if ((tempTime - [NSDate date].timeIntervalSince1970) < 1)
{
[self stopTimer];
[_qActiveBtn setTitle:[NSString stringWithFormat:GJText(@"开始测量")] forState:0];
}
else
{
[_qActiveBtn setTitle:[NSString stringWithFormat:GJText(@"测量中(%ds)"),(int)(tempTime - [NSDate date].timeIntervalSince1970)] forState:0];
[self startTimer];
_lastTimeLabel.hidden = YES;
_dataLabel.text = @"--";
return;
}
}
else
{
[self stopTimer];
[_qActiveBtn setTitle:[NSString stringWithFormat:GJText(@"开始测量")] 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 = [UICommon getDateFormatWithStr:@"yyyy-MM-dd HH:mm" timeInterval:model.lastDataTime.doubleValue];
_lastTimeLabel.text = [NSString stringWithFormat:@"%@: %@",GJText(@"上一次"),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 = KKMainColor;
_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"]}];
}
}
tempDataList = (NSMutableArray*)[[tempDataList reverseObjectEnumerator] allObjects];
[_LineGraphView setXMarkWithDay:_qDayStr TitlesTimeAndValues:tempDataList titleKey:@"timestamp" valueKey:@"data"]; // X轴刻度标签及相应的值
//设置完数据等属性后绘图折线图
[_LineGraphView mapping];
}
- (void)daychangeAction:(UIButton*)sender
{
NSString *curDayStr = self.qDayLabel.text;
if([self.qDayLabel.text isEqualToString:GJText(@"今天")])
{
curDayStr = [UICommon getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970];
}
NSString *swithDayStr ;
if([sender isEqual:_lastDayBtn])
{
//前一天
//当前位置的天数转换成的时间戳
NSTimeInterval tempTime = [UICommon getDateWithStr:curDayStr dateFormat:@"yyyy-MM-dd"].timeIntervalSince1970;
swithDayStr = [UICommon getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:tempTime - 60*60*24];
}
else
{
//后一天
NSTimeInterval tempTime = [UICommon getDateWithStr:curDayStr dateFormat:@"yyyy-MM-dd"].timeIntervalSince1970;
swithDayStr = [UICommon getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:tempTime + 60*60*24];
NSString *todayStr = [UICommon getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970];
if ([swithDayStr isEqualToString:todayStr])
{
swithDayStr = GJText(@"今天");
}
}
[self setqDayLabelText:swithDayStr];
if(self.switchQueryBlock){
self.switchQueryBlock(_qDayStr);
}
}
-(void)setqDayLabelText:(NSString*)day
{
if(day == nil || day.length == 0)
{
day = GJText(@"今天");
}
if ([day isEqualToString:GJText(@"今天")] || [day isEqualToString:[UICommon getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]]) {
[self setBtnBadRoGoodFlag:NO WithBtn:_nextDayBtn];
_qDayStr = [UICommon getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970];
_qDayLabel.text = GJText(@"今天");
}
else
{
_qDayStr = day;
_qDayLabel.text = day;
[self setBtnBadRoGoodFlag:YES WithBtn:_nextDayBtn];
}
}
-(void)nowQueryOneAction:(UIButton*)sender
{
if([sender.titleLabel.text isEqualToString:GJText(@"开始测量")])
{
//调用接口开始测量一次, 收到推送刷新页面就行
kWeakSelf(self)
@try {
[HeartAndTempModel postHeartAndTempDataWithType:self.type cmd:2 obj:@"-1" success:^(id responseObject) {
NSString * tempKey;
if(weakself.type == 1){
//心率
tempKey = [NSString stringWithFormat:@"%@&%@",HeartMeasurementTimeKey,APIManager.sharedManager.deviceModel.imei];
}else{
//体温
tempKey = [NSString stringWithFormat:@"%@&%@",TempMeasurementTimeKey,APIManager.sharedManager.deviceModel.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 == 1)
{
//心率
tempKey = [NSString stringWithFormat:@"%@&%@",HeartMeasurementTimeKey,APIManager.sharedManager.deviceModel.imei];
}
else
{
//体温
tempKey = [NSString stringWithFormat:@"%@&%@",TempMeasurementTimeKey,APIManager.sharedManager.deviceModel.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:GJText(@"开始测量") forState:0];
//MARK: 这里倒计时结束时需要回调刷新页面的数据
if(self.measurementEndBlock){
self.measurementEndBlock();
}
}
else
{
[UserDefaults setValue:[NSDate dateWithTimeIntervalSince1970:tempTime] forKey:tempKey];
[self.qActiveBtn setTitle:[NSString stringWithFormat:GJText(@"测量中(%ds)"),(int)(tempTime - [NSDate date].timeIntervalSince1970)] forState:0];
}
}
else
{
[self stopTimer];
[self.qActiveBtn setTitle:GJText(@"开始测量") 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