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.

202 lines
7.0 KiB

//
// WHomeHeartAndTempCell.m
// tongxin
//
// Created by Apple on 2020/4/8.
// Copyright © 2020 xTT. All rights reserved.
//
#import "WHomeHeartAndTempCell.h"
#import "HeartAndTempModel.h"
#import "User.h"
#import "Device.h"
//倒计时时间
#define DefineCountdownTime 60
@interface WHomeHeartAndTempCell()
/// 类型 0心率 , 1体温
@property (assign, nonatomic) int type;
///定时器
@property (nonatomic, strong) NSTimer *timer;
/// 数据
@property (nonatomic, strong) NSString *data;
@end
@implementation WHomeHeartAndTempCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.contentView.backgroundColor = [UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
_backView.backgroundColor = [UIColor clearColor];
_titleLabel.textColor = RGB(51, 51, 51 );
_lastTimeLabel.textColor = RGB(153, 153, 153);
_lastTimeLabel.text = @"--:--";
_unitLabel.textColor = RGB(102, 102, 102);
_unitLabel.text = @"-/-";
_dataLabel.textColor = RGB(51, 51, 51 );
_dataLabel.text = @"--";
[_activeBtn setCornerRadius:_activeBtn.height/2.0 borderWidth:1 borderColor:RGB(245, 81, 70)];
[_activeBtn setTitleColor:RGB(245, 81, 70) forState:0];
[_activeBtn addTarget:self action:@selector(activeBtnFun:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setType:(int)type WithData:(NSString*)data LastTime:(NSNumber*)time{
_type = type;
_data = data;
NSString *tempKey;
if (type == 0) {
//心率
_titleLabel.text = @"心率";
_backgroundImageView.image = [UIImage imageNamed:@"img_heart_rate_bg"];
[_activeBtn setCornerRadius:_activeBtn.height/2.0 borderWidth:1 borderColor:RGB(245, 81, 70)];
[_activeBtn setTitleColor:RGB(245, 81, 70) forState:0];
tempKey = [NSString stringWithFormat:@"%@&%@",HeartMeasurementTimeKey,cUser.cDevice.imei];
_unitLabel.text = @"次/分";
}else{
//体温
_titleLabel.text = @"体温";
_backgroundImageView.image = [UIImage imageNamed:@"img_body_temperature_bg"];
[_activeBtn setCornerRadius:_activeBtn.height/2.0 borderWidth:1 borderColor:RGB(83, 169, 248)];
[_activeBtn setTitleColor:RGB(83, 169, 248) forState:0];
tempKey = [NSString stringWithFormat:@"%@&%@",TempMeasurementTimeKey,cUser.cDevice.imei];
_unitLabel.text = @"℃";
}
NSDate *tempDate = [UserDefaults valueForKey:tempKey];
if(tempDate){
//和最近的数据时间比对一下,如果时间间隔小于 DefineCountdownTime 证明已经更新了数据,但是没收到推送
NSTimeInterval tempTime = tempDate.timeIntervalSince1970;
if( (tempTime - time.doubleValue) <= DefineCountdownTime){
[self stopTimer];
[_activeBtn setTitle:[NSString stringWithFormat:@"开始测量"] forState:0];
}else if ((tempTime - [NSDate date].timeIntervalSince1970) < 1) {
[self stopTimer];
[_activeBtn setTitle:[NSString stringWithFormat:@"开始测量"] forState:0];
}else{
[_activeBtn setTitle:[NSString stringWithFormat:@"测量中(%ds)",(int)(tempTime - [NSDate date].timeIntervalSince1970)] forState:0];
[self startTimer];
_lastTimeLabel.hidden = YES;
_dataLabel.text = @"--";
return;
}
}else{
[self stopTimer];
[_activeBtn setTitle:[NSString stringWithFormat:@"开始测量"] forState:0];
}
if(data && ![data isEqualToString:@"0"]){
_dataLabel.text = data;
}else{
_dataLabel.text = @"--";
}
if (time && time.doubleValue !=0) {
//时间
NSString *dateStr = [myHelper distanceTimeWithBeforeTime:[time doubleValue]];
_lastTimeLabel.text = dateStr;
_lastTimeLabel.hidden = NO;
}else{
// _lastTimeLabel.text = @"--:--";
_lastTimeLabel.hidden = YES;
}
}
- (void)activeBtnFun:(UIButton*)sender{
if([sender.titleLabel.text isEqualToString:@"开始测量"]){
//调用接口开始测量一次, 收到推送刷新页面就行
WEAKSELF
[HeartAndTempModel postHeartAndTempDataWithType:_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.lastTimeLabel.hidden = YES;
weakSelf.dataLabel.text = @"--";
} failure:^(NSError * _Nonnull error) {
}];
}
}
#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.activeBtn setTitle:@"开始测量" forState:0];
self.lastTimeLabel.hidden = NO;
self.dataLabel.text = self.data;
}else{
[UserDefaults setValue:[NSDate dateWithTimeIntervalSince1970:tempTime] forKey:tempKey];
[self.activeBtn setTitle:[NSString stringWithFormat:@"测量中(%ds)",(int)(tempTime - [NSDate date].timeIntervalSince1970)] forState:0];
}
}else{
[self stopTimer];
[self.activeBtn setTitle:@"开始测量" forState:0];
self.lastTimeLabel.hidden = NO;
self.dataLabel.text = self.data;
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end