// // HomeTableViewCell.m // LekangGuard // // Created by ecell on 2022/10/14. // #import "HomeTableViewCell.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 HomeTableViewCell () /// 背景图片 @property (nonatomic ,weak) UIImageView *bgimg; /// 标题 @property (nonatomic ,weak) UILabel *titleLabel; /// 时间 @property (nonatomic ,weak) UILabel *timeLabel; /// 右边 fed0df @property (nonatomic ,weak) UIButton *rightBtn; /// 温度或者心率 @property (nonatomic ,weak) UILabel *commLabel; @property (nonatomic ,weak) UILabel *tiLabel; ///定时器 @property (nonatomic, strong) NSTimer *timer; @end @implementation HomeTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = KKClearColor; [self subCellView]; } return self; } - (void)setTitleStr:(NSString *)titleStr { _titleStr = titleStr; self.titleLabel.text = titleStr; if ([titleStr isEqualToString:GJText(@"心率")]) { self.bgimg.image = ImageName_(@"img_heart_rate_bg"); self.rightBtn.layer.borderColor = RGB(254, 74, 102).CGColor; [self.rightBtn setTitleColor:RGB(254, 74, 102) forState:0]; } if ([titleStr isEqualToString:GJText(@"体温")]) { self.bgimg.image = ImageName_(@"img_body_temperature_bg"); self.rightBtn.layer.borderColor = KKMainColor.CGColor; [self.rightBtn setTitleColor:KKMainColor forState:0]; } } - (void)setLaLoModel:(LatestLocationModel *)laLoModel { _laLoModel = laLoModel; if ([self.titleLabel.text isEqualToString:GJText(@"心率")]) { self.commLabel.text = STRINGHASVALUE(laLoModel.rate) ? laLoModel.rate : @"--"; self.timeLabel.text = laLoModel.rateTime; self.tiLabel.text = GJText(@"次/分"); } if ([self.titleLabel.text isEqualToString:GJText(@"体温")]) { self.commLabel.text = STRINGHASVALUE(laLoModel.temperature) ? laLoModel.temperature : @"--"; self.timeLabel.text = laLoModel.temperatureTime; self.tiLabel.text = @"℃"; } } - (void)subCellView { UIImageView *bgimg = [UICommon ui_imageView:CGRectZero fileName:@""]; self.bgimg = bgimg; [self.contentView addSubview:bgimg]; [bgimg mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.contentView).inset(10); make.top.equalTo(self.contentView); make.bottom.equalTo(self.contentView.mas_bottom).inset(10); }]; /// 标题 UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(11) textColor:KKGrey121 text:@"--" Radius:0]; self.titleLabel = titleLabel; [self.contentView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bgimg).offset(30); make.top.equalTo(bgimg).offset(Adapted(20)); }]; /// 时间 UILabel *timeLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(10) textColor:KKGrey143 text:@"--" Radius:0]; self.timeLabel = timeLabel; [self.contentView addSubview:timeLabel]; [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(titleLabel.mas_right).offset(10); make.centerY.equalTo(titleLabel); }]; /// 右边 fed0df UIButton *rightBtn = [UICommon ui_buttonSimple:CGRectZero font:FontADA_(12) normalColor:KKMainColor normalText:GJText(@"开始测量") click:^(UIButton *btn){ kWeakSelf(self) NSLog(@"%@",self.titleLabel.text); @try { [HeartAndTempModel postHeartAndTempDataWithType:[self.titleLabel.text isEqualToString:GJText(@"心率")] ? 1 : 2 cmd:2 obj:@"-1" success:^(id responseObject) { NSString * tempKey; if([self.titleLabel.text isEqualToString:GJText(@"心率")]) { //心率 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]; } failure:^(NSError * _Nonnull error) { }]; } @catch (NSException *exception) { } @finally { } }]; rightBtn.layer.borderWidth = 1; rightBtn.layer.borderColor = KKMainColor.CGColor; rightBtn.layer.cornerRadius = Adapted(15); self.rightBtn = rightBtn; [self.contentView addSubview:rightBtn]; CGRect titlesize = [UICommon GetTextWidth:rightBtn.currentTitle ViewHeight:Adapted(30) fontSize:FontADA_(12) type:@"w"]; [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgimg.mas_right).inset(20); make.centerY.equalTo(bgimg); make.size.mas_equalTo(CGSizeMake(Adapted(titlesize.size.width+20), Adapted(30))); }]; /// 温度或心率 UILabel *commLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontBoldADA_(18) textColor:KKGrey121 text:@"--" Radius:0]; self.commLabel = commLabel; [self.contentView addSubview:commLabel]; [commLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(titleLabel.mas_left); make.bottom.equalTo(bgimg.mas_bottom).inset(Adapted(20)); }]; UILabel *tiLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentLeft font:FontADA_(11) textColor:KKGrey121 text:@"" Radius:0]; self.tiLabel = tiLabel; [self.contentView addSubview:tiLabel]; [tiLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(commLabel.mas_right).offset(5); make.bottom.equalTo(commLabel.mas_bottom); }]; } #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.titleLabel.text isEqualToString:GJText(@"心率")]) { //心率 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.rightBtn setTitle:GJText(@"开始测量") forState:0]; //MARK: 这里倒计时结束时需要回调刷新页面的数据 // if(self.measurementEndBlock) // { // self.measurementEndBlock(); // } } else { [UserDefaults setValue:[NSDate dateWithTimeIntervalSince1970:tempTime] forKey:tempKey]; [self.rightBtn setTitle:[NSString stringWithFormat:GJText(@"测量中(%ds)"),(int)(tempTime - [NSDate date].timeIntervalSince1970)] forState:0]; } } else { [self stopTimer]; [self.rightBtn setTitle:GJText(@"开始测量") forState:0]; //MARK: 这里倒计时结束时需要回调刷新页面的数据 // if(self.measurementEndBlock) // { // self.measurementEndBlock(); // } } CGRect titlesize = [UICommon GetTextWidth:self.rightBtn.currentTitle ViewHeight:Adapted(30) fontSize:FontADA_(12) type:@"w"]; [self.rightBtn mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.bgimg.mas_right).inset(20); make.centerY.equalTo(self.bgimg); make.size.mas_equalTo(CGSizeMake(Adapted(titlesize.size.width+20), Adapted(30))); }]; } - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end