// // backupsFileRecordCell.m // 隐私保护 // // Created by xd h on 2024/1/4. // #import "backupsFileRecordCell.h" #import "cachesFileManager.h" #import @interface backupsFileRecordCell () @property(nonatomic,strong) UIImageView *fileImageView; @property(nonatomic,strong) UIButton *upLoadStateButton; @property (nonatomic, strong) UIButton *selectButton; @property(nonatomic,strong) UIView *progressBgView; @property(nonatomic,strong) UIView *progressSelectView; @property(nonatomic,strong) CAGradientLayer *glayer; @property(nonatomic,strong) UILabel *fileNamelabel; @property(nonatomic,strong) UILabel *fileTimelabel; @property(nonatomic,strong) UILabel *fileProgresslabel; @property(nonatomic,strong) UILabel *fileSpeedlabel; @property(nonatomic,strong) UILabel *rightTiplabel; @end @implementation backupsFileRecordCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self drawView]; } return self; } - (void)drawView { _fileImageView = [[UIImageView alloc] init]; //_fileImageView.backgroundColor = [UIColor greenColor]; [self.contentView addSubview:_fileImageView]; [_fileImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.width.mas_equalTo(34); make.height.mas_equalTo(34); make.bottom.mas_equalTo(-18); }]; _upLoadStateButton = [[UIButton alloc] init]; //_upLoadStateButton.backgroundColor = [UIColor redColor]; [self.contentView addSubview:_upLoadStateButton]; [_upLoadStateButton addTarget:self action:@selector(tapClick:) forControlEvents:UIControlEventTouchUpInside]; [_upLoadStateButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-5); make.width.mas_equalTo(50); make.height.mas_equalTo(50); make.bottom.mas_equalTo(-18); }]; if (self.selectButton == nil) { UIButton *but = [[UIButton alloc] init]; [but setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal]; [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected]; [self.contentView addSubview:but]; [but addTarget:self action:@selector(didClickSelectButFun:) forControlEvents:UIControlEventTouchUpInside]; but.hidden = YES; //but.backgroundColor = [UIColor greenColor]; //but.frame = CGRectMake([UIScreen mainScreen].bounds.size.width -10 - 25, 0, 25, 25); [but mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(30); make.height.mas_equalTo(30); make.centerX.mas_equalTo(_upLoadStateButton.mas_centerX); make.centerY.mas_equalTo(_upLoadStateButton.mas_centerY); }]; self.selectButton = but; } _progressBgView = [[UIView alloc] init]; _progressBgView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0]; _progressBgView.layer.cornerRadius = 1.5; _progressBgView.layer.masksToBounds = YES; [self.contentView addSubview:_progressBgView]; [_progressBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_fileImageView.mas_right).offset(10); make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10); make.height.mas_equalTo(2); make.centerY.mas_equalTo(0); }]; _progressSelectView = [[UIView alloc] init]; //_progressSelectView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0]; _progressSelectView.layer.cornerRadius = 1.5; _progressSelectView.layer.masksToBounds = YES; [_progressBgView addSubview:_progressSelectView]; // [_progressSelectView mas_makeConstraints:^(MASConstraintMaker *make) { // make.left.mas_equalTo(0); // make.right.mas_equalTo(-100); // make.height.mas_equalTo(3); // make.centerY.mas_equalTo(0); // }]; // gradient _glayer = [CAGradientLayer layer]; _glayer.startPoint = CGPointMake(0, 0.5); _glayer.endPoint = CGPointMake(1, 0.5); _glayer.colors = @[(__bridge id)[UIColor hwColor:@"#0BDDFD" alpha:1.0].CGColor, (__bridge id)[UIColor hwColor:@"#048CFB" alpha:1.0].CGColor]; _glayer.locations = @[@(0), @(1.0f)]; [_progressSelectView.layer addSublayer:_glayer]; CGRect frame = CGRectMake(0, 0, SCREEN_W, 2); //_progressSelectView.frame = frame; _glayer.frame = frame; _fileNamelabel = [[UILabel alloc] init]; _fileNamelabel.font = [UIFont boldSystemFontOfSize:16.0]; _fileNamelabel.textColor = [UIColor blackColor]; //_fileNamelabel.backgroundColor = [UIColor orangeColor]; [self.contentView addSubview:_fileNamelabel]; [_fileNamelabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_fileImageView.mas_right).offset(10); make.right.mas_equalTo(self.mas_centerX).offset(-20); make.height.mas_equalTo(19); make.bottom.mas_equalTo(_progressBgView.mas_top).offset(-3); }]; _fileTimelabel = [[UILabel alloc] init]; //_fileTimelabel.font = [UIFont systemFontOfSize:13.0]; _fileTimelabel.adjustsFontSizeToFitWidth = YES; _fileTimelabel.textColor = [UIColor blackColor]; _fileTimelabel.textAlignment = NSTextAlignmentRight; //_fileTimelabel.backgroundColor = [UIColor greenColor]; [self.contentView addSubview:_fileTimelabel]; //_fileTimelabel.text = @"当前文件预计需20分"; [_fileTimelabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.mas_centerX).offset(-20); make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10); make.height.mas_equalTo(15); make.bottom.mas_equalTo(_progressBgView.mas_top).offset(-3); }]; _fileProgresslabel = [[UILabel alloc] init]; _fileProgresslabel.font = [UIFont systemFontOfSize:12.0]; _fileProgresslabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0]; //_fileProgresslabel.backgroundColor = [UIColor greenColor]; [self.contentView addSubview:_fileProgresslabel]; [_fileProgresslabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_fileImageView.mas_right).offset(10); make.right.mas_equalTo(_progressBgView.mas_centerX).offset(100); make.height.mas_equalTo(15); make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3); }]; _fileSpeedlabel = [[UILabel alloc] init]; _fileSpeedlabel.font = [UIFont systemFontOfSize:12.0]; _fileSpeedlabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0]; _fileSpeedlabel.textAlignment = NSTextAlignmentRight; //_fileSpeedlabel.backgroundColor = [UIColor blackColor]; [self.contentView addSubview:_fileSpeedlabel]; [_fileSpeedlabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10); make.left.mas_equalTo(_progressBgView.mas_centerX); make.height.mas_equalTo(15); make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3); }]; _rightTiplabel = [[UILabel alloc] init]; _rightTiplabel.font = [UIFont systemFontOfSize:12.0]; _rightTiplabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0]; _rightTiplabel.textAlignment = NSTextAlignmentRight; _rightTiplabel.numberOfLines = 0; //_fileSpeedlabel.backgroundColor = [UIColor blackColor]; [self.contentView addSubview:_rightTiplabel]; [_rightTiplabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-15.0); make.left.mas_equalTo(_progressBgView.mas_centerX).offset(-40); make.height.mas_equalTo(30); make.centerY.mas_equalTo(0); }]; //长按手势 UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)]; //用几个手指触屏,默认1 longPressGesture.numberOfTouchesRequired = 1; //设置最短长按时间,单位为秒(默认0.5) longPressGesture.minimumPressDuration = 1; //设置手势识别期间所允许的手势可移动范围 longPressGesture.allowableMovement = 10; [self.contentView addGestureRecognizer:longPressGesture]; } - (void)setIsEditType:(BOOL)isEditType { _isEditType = isEditType; if(isEditType){ self.selectButton.hidden = NO; // if(_curUploadFileDataModel.curUploadStateType == uploadStateWait // ||_curUploadFileDataModel.curUploadStateType == uploadStateUploading // ||_curUploadFileDataModel.curUploadStateType == uploadStateSuspend){ _upLoadStateButton.hidden = YES; //} } else{ self.selectButton.hidden = YES; // if(_curUploadFileDataModel.curUploadStateType == uploadStateWait // ||_curUploadFileDataModel.curUploadStateType == uploadStateUploading // ||_curUploadFileDataModel.curUploadStateType == uploadStateSuspend){ _upLoadStateButton.hidden = NO; //} } } - (void)setIsSelectType:(BOOL)isSelectType { _isSelectType = isSelectType; if(isSelectType){ self.selectButton.selected = YES; } else{ self.selectButton.selected = NO; } } - (void)setCurPhotosBackupsTaskModel:(photosBackupsTaskModel *)curPhotosBackupsTaskModel { _curPhotosBackupsTaskModel = curPhotosBackupsTaskModel; //test code //_curPhotosBackupsTaskModel.curBackupsState = backupsStateUploading; _fileImageView.image = [UIImage imageNamed:@"uploadFile_image"]; _fileSpeedlabel.hidden = YES; _rightTiplabel.hidden = YES; _progressBgView.hidden = NO; if(_curPhotosBackupsTaskModel.curBackupsState == backupsStateUploading){ _upLoadStateButton.hidden = NO; [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal]; _fileSpeedlabel.hidden = NO; // //_fileSpeedlabel.text = @""; if(_curPhotosBackupsTaskModel.preTimeInterval <= 0 ||_curPhotosBackupsTaskModel.preDidUploadBytes <=0){ _fileSpeedlabel.text = @"--"; [self handlFileTimelabelFunBy:0]; HLog(@"111: %f",_curPhotosBackupsTaskModel.preTimeInterval); } else{ NSTimeInterval timers = _curPhotosBackupsTaskModel.curTimeInterval - _curPhotosBackupsTaskModel.preTimeInterval; long curUploadSize = _curPhotosBackupsTaskModel.didUploadBytes - _curPhotosBackupsTaskModel.preDidUploadBytes; if(timers > 0 && curUploadSize > 0){ //long speed= EachPieceSzie / timers; // if(curUploadSize <0 || curUploadSize> EachPieceSzie){ // curUploadSize = EachPieceSzie; // } if(curUploadSize < 0 || curUploadSize> MaxNasUploadPieceSzie){ curUploadSize = MaxNasUploadPieceSzie; } long speed = curUploadSize/timers; NSString * speedStr = nil; NSInteger speed_k = speed / (1024); if(speed == 0){ speedStr = @"--"; } else if(speed_k < 1024){ speedStr = [[NSString alloc] initWithFormat:@"%ldKB/s",speed_k]; } else { speedStr = [[NSString alloc] initWithFormat:@"%.2fMB/s",speed_k/1024.0]; } _fileSpeedlabel.text = speedStr; [self handlFileTimelabelFunBy:speed]; } } } else if(_curPhotosBackupsTaskModel.curBackupsState == backupsStateSuspend){ [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal]; _fileSpeedlabel.text = _curPhotosBackupsTaskModel.backupsTipMsg; _fileSpeedlabel.hidden = NO; _upLoadStateButton.hidden = NO; [self handlFileTimelabelFunBy:-1]; } else if(_curPhotosBackupsTaskModel.curBackupsState == backupsStateFail){ _upLoadStateButton.hidden = YES; _progressBgView.hidden = YES; [self handlFileTimelabelFunBy:-1]; } else{ _upLoadStateButton.hidden = YES; _progressBgView.hidden = YES; [self handlFileTimelabelFunBy:-1]; } _fileNamelabel.text = NSLocalizedString(@"set_Privacy_title_image",nil); _curPhotosBackupsTaskModel.preTimeInterval = _curPhotosBackupsTaskModel.curTimeInterval; _curPhotosBackupsTaskModel.preDidUploadBytes = _curPhotosBackupsTaskModel.didUploadBytes; if(_curPhotosBackupsTaskModel.curBackupsState == backupsStateDone){ NSString * curTimerStr = _curPhotosBackupsTaskModel.bg_updateTime; if(!curTimerStr){ curTimerStr = _curPhotosBackupsTaskModel.bg_createTime; } if(curTimerStr){ curTimerStr = [curTimerStr substringToIndex:(curTimerStr.length -4)]; } _fileProgresslabel.text = curTimerStr; _rightTiplabel.hidden = NO; _rightTiplabel.text = [[NSString alloc] initWithFormat:@"%@:%ld",NSLocalizedString(@"backups_cur_task_done_numbers",nil),_curPhotosBackupsTaskModel.didBackupsCount]; } else if(_curPhotosBackupsTaskModel.curBackupsState == backupsStateFail){ _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%ld/%ld",_curPhotosBackupsTaskModel.didBackupsCount,_curPhotosBackupsTaskModel.count]; //_curPhotosBackupsTaskModel.backupsTipMsg = @"备份失败,备份路径不存在"; _rightTiplabel.hidden = NO; _rightTiplabel.textColor = [UIColor hwColor:@"#DD4E4E" alpha:1.0]; _rightTiplabel.text = _curPhotosBackupsTaskModel.backupsTipMsg; } else{ //_fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%ld/%ld",_curPhotosBackupsTaskModel.didBackupsCount,_curPhotosBackupsTaskModel.count - _curPhotosBackupsTaskModel.failCount]; [self handlFileProgresslabelFun]; //进度设置 CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2); CGFloat curProgress = _curPhotosBackupsTaskModel.didBackupsCount/((_curPhotosBackupsTaskModel.count - _curPhotosBackupsTaskModel.failCount)*1.0); //只有一个任务的时候 if(_curPhotosBackupsTaskModel.LocalIdentifierArr.count == 1 && _curPhotosBackupsTaskModel.didBackupsCount == 0){ curProgress = _curPhotosBackupsTaskModel.didUploadBytes / (_curPhotosBackupsTaskModel.totalBytes*1.0); } //curProgress = _curPhotosBackupsTaskModel.didUploadBytes / (_curPhotosBackupsTaskModel.totalBytes*1.0); if(curProgress > 1){ curProgress = 1; } frame.size.width = frame.size.width * curProgress; if (isnan(frame.size.width)) { return; } _progressSelectView.frame = frame; //_glayer.frame = frame; } HLog(@"111: end"); } - (void)handlFileTimelabelFunBy:(NSInteger)speed { NSString * timeStr = @"--"; CGFloat estimateTime = 0.0; if(speed < 0){ _fileTimelabel.text = @""; return; } else if (speed == 0){ //不处理 //estimateTime = 0.0; } else{ if(_curPhotosBackupsTaskModel.totalBytes >0 && _curPhotosBackupsTaskModel.didUploadBytes>0 && _curPhotosBackupsTaskModel.totalBytes > _curPhotosBackupsTaskModel.didUploadBytes){ estimateTime = (_curPhotosBackupsTaskModel.totalBytes - _curPhotosBackupsTaskModel.didUploadBytes)/speed; } } //HLog(@"1111111111 %ld %ld %ld %ld %f",_curPhotosBackupsTaskModel.totalBytes,_curPhotosBackupsTaskModel.didUploadBytes,_curPhotosBackupsTaskModel.totalBytes-_curPhotosBackupsTaskModel.didUploadBytes,speed,estimateTime) if(estimateTime>0){ NSInteger seconds = (NSInteger)estimateTime; if(seconds >= 60){ timeStr = [[NSString alloc] initWithFormat:@"%ld%@",seconds/60,NSLocalizedString(@"common_minute",nil)]; } else{ timeStr = [[NSString alloc] initWithFormat:@"%ld%@",seconds,NSLocalizedString(@"common_second",nil)]; } } _fileTimelabel.text = [[NSString alloc] initWithFormat:@"%@%@",NSLocalizedString(@"Estimated_time",nil),timeStr]; HLog(@"%@ 111: %@ 222: %@ 333: %ld 444:%f total:%ld didUpload:%ld error:%d",_curPhotosBackupsTaskModel.filename,_fileTimelabel.text,timeStr,speed,estimateTime,_curPhotosBackupsTaskModel.totalBytes,_curPhotosBackupsTaskModel.didUploadBytes,_curPhotosBackupsTaskModel.totalBytes>=_curPhotosBackupsTaskModel.didUploadBytes?0:1); } - (void)handlFileProgresslabelFun { if(_curPhotosBackupsTaskModel.totalBytes == 0){ _fileProgresslabel.text = @""; return; } NSString * totalSizeStr = nil; NSInteger totalSize_k = _curPhotosBackupsTaskModel.totalBytes / 1024; if(totalSize_k < 1024){ totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k]; } else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){ totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fMB",totalSize_k/1024.0]; } else{ totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0]; } NSString * didUploadStr = nil; NSInteger didUploadSize_k = _curPhotosBackupsTaskModel.didUploadBytes / 1024; if(didUploadSize_k < 1024){ didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k]; } else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){ didUploadStr = [[NSString alloc] initWithFormat:@"%.2fMB",didUploadSize_k/1024.0]; } else{ didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0]; } _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr]; } -(void)longPressClick:(UILongPressGestureRecognizer *)press{ //state属性是所有手势父类提供的方法,用于记录手势的状态 if(press.state == UIGestureRecognizerStateBegan){ //NSLog(@"长按手势开始响应!"); if(_didLongPressClick){ _didLongPressClick(); } }else if (press.state == UIGestureRecognizerStateChanged){ //NSLog(@"长按手势状态发生改变!"); }else{ //NSLog(@"长按手势结束!"); } } - (void)didClickSelectButFun:(UIButton*)but { but.selected = !but.selected; if(_didClckSelectBut){ _didClckSelectBut(but.selected); } } - (void)tapClick:(UIButton*)but{ if(_didTapPressClick){ _didTapPressClick(); } if(_curPhotosBackupsTaskModel.curBackupsState == backupsStateUploading){ [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal]; } else if(_curPhotosBackupsTaskModel.curBackupsState == backupsStateSuspend){ [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal]; } but.userInteractionEnabled = NO; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ but.userInteractionEnabled = YES; }); } @end