downloadFileRecordCell.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // downloadFileRecordCell.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/10.
  6. //
  7. #import "downloadFileRecordCell.h"
  8. #import "cachesFileManager.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. #import "downloadThumbnailManager.h"
  11. @interface downloadFileRecordCell ()
  12. @property(nonatomic,strong) UIImageView *fileImageView;
  13. @property(nonatomic,strong) UIButton *upLoadStateButton;
  14. @property (nonatomic, strong) UIButton *selectButton;
  15. @property(nonatomic,strong) UIView *progressBgView;
  16. @property(nonatomic,strong) UIView *progressSelectView;
  17. @property(nonatomic,strong) CAGradientLayer *glayer;
  18. @property(nonatomic,strong) UILabel *fileNamelabel;
  19. @property(nonatomic,strong) UILabel *fileProgresslabel;
  20. @property(nonatomic,strong) UILabel *fileSpeedlabel;
  21. @end
  22. @implementation downloadFileRecordCell
  23. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  24. {
  25. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  26. if (self)
  27. {
  28. [self drawView];
  29. }
  30. return self;
  31. }
  32. - (void)drawView
  33. {
  34. _fileImageView = [[UIImageView alloc] init];
  35. //_fileImageView.backgroundColor = [UIColor greenColor];
  36. [_fileImageView setContentMode:(UIViewContentModeScaleAspectFit)];
  37. [self.contentView addSubview:_fileImageView];
  38. [_fileImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.left.mas_equalTo(15);
  40. make.width.mas_equalTo(34);
  41. make.height.mas_equalTo(34);
  42. make.bottom.mas_equalTo(-18);
  43. }];
  44. _upLoadStateButton = [[UIButton alloc] init];
  45. //_upLoadStateButton.backgroundColor = [UIColor redColor];
  46. [self.contentView addSubview:_upLoadStateButton];
  47. [_upLoadStateButton addTarget:self action:@selector(tapClick:) forControlEvents:UIControlEventTouchUpInside];
  48. [_upLoadStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.right.mas_equalTo(-5);
  50. make.width.mas_equalTo(50);
  51. make.height.mas_equalTo(50);
  52. make.bottom.mas_equalTo(-18);
  53. }];
  54. if (self.selectButton == nil) {
  55. UIButton *but = [[UIButton alloc] init];
  56. [but setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal];
  57. [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
  58. [self.contentView addSubview:but];
  59. [but addTarget:self action:@selector(didClickSelectButFun:) forControlEvents:UIControlEventTouchUpInside];
  60. but.hidden = YES;
  61. //but.backgroundColor = [UIColor greenColor];
  62. //but.frame = CGRectMake([UIScreen mainScreen].bounds.size.width -10 - 25, 0, 25, 25);
  63. [but mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.width.mas_equalTo(30);
  65. make.height.mas_equalTo(30);
  66. make.centerX.mas_equalTo(_upLoadStateButton.mas_centerX);
  67. make.centerY.mas_equalTo(_upLoadStateButton.mas_centerY);
  68. }];
  69. self.selectButton = but;
  70. }
  71. _progressBgView = [[UIView alloc] init];
  72. _progressBgView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
  73. _progressBgView.layer.cornerRadius = 1.5;
  74. _progressBgView.layer.masksToBounds = YES;
  75. [self.contentView addSubview:_progressBgView];
  76. [_progressBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  78. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  79. make.height.mas_equalTo(2);
  80. make.centerY.mas_equalTo(0);
  81. }];
  82. _progressSelectView = [[UIView alloc] init];
  83. //_progressSelectView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
  84. _progressSelectView.layer.cornerRadius = 1.5;
  85. _progressSelectView.layer.masksToBounds = YES;
  86. [_progressBgView addSubview:_progressSelectView];
  87. // [_progressSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
  88. // make.left.mas_equalTo(0);
  89. // make.right.mas_equalTo(-100);
  90. // make.height.mas_equalTo(3);
  91. // make.centerY.mas_equalTo(0);
  92. // }];
  93. // gradient
  94. _glayer = [CAGradientLayer layer];
  95. _glayer.startPoint = CGPointMake(0, 0.5);
  96. _glayer.endPoint = CGPointMake(1, 0.5);
  97. _glayer.colors = @[(__bridge id)[UIColor hwColor:@"#0BDDFD" alpha:1.0].CGColor, (__bridge id)[UIColor hwColor:@"#048CFB" alpha:1.0].CGColor];
  98. _glayer.locations = @[@(0), @(1.0f)];
  99. [_progressSelectView.layer addSublayer:_glayer];
  100. CGRect frame = CGRectMake(0, 0, SCREEN_W, 2);
  101. //_progressSelectView.frame = frame;
  102. _glayer.frame = frame;
  103. _fileNamelabel = [[UILabel alloc] init];
  104. _fileNamelabel.font = [UIFont boldSystemFontOfSize:16.0];
  105. _fileNamelabel.textColor = [UIColor blackColor];
  106. //_fileNamelabel.backgroundColor = [UIColor greenColor];
  107. [self.contentView addSubview:_fileNamelabel];
  108. [_fileNamelabel mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  110. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  111. make.height.mas_equalTo(19);
  112. make.bottom.mas_equalTo(_progressBgView.mas_top).offset(-3);
  113. }];
  114. _fileProgresslabel = [[UILabel alloc] init];
  115. _fileProgresslabel.font = [UIFont systemFontOfSize:12.0];
  116. _fileProgresslabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  117. //_fileProgresslabel.backgroundColor = [UIColor greenColor];
  118. [self.contentView addSubview:_fileProgresslabel];
  119. [_fileProgresslabel mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  121. make.right.mas_equalTo(_progressBgView.mas_centerX).offset(100);
  122. make.height.mas_equalTo(15);
  123. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  124. }];
  125. _fileSpeedlabel = [[UILabel alloc] init];
  126. _fileSpeedlabel.font = [UIFont systemFontOfSize:12.0];
  127. _fileSpeedlabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  128. _fileSpeedlabel.textAlignment = NSTextAlignmentRight;
  129. //_fileSpeedlabel.backgroundColor = [UIColor blackColor];
  130. [self.contentView addSubview:_fileSpeedlabel];
  131. [_fileSpeedlabel mas_makeConstraints:^(MASConstraintMaker *make) {
  132. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  133. make.left.mas_equalTo(_progressBgView.mas_centerX);
  134. make.height.mas_equalTo(15);
  135. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  136. }];
  137. //长按手势
  138. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
  139. //用几个手指触屏,默认1
  140. longPressGesture.numberOfTouchesRequired = 1;
  141. //设置最短长按时间,单位为秒(默认0.5)
  142. longPressGesture.minimumPressDuration = 1;
  143. //设置手势识别期间所允许的手势可移动范围
  144. longPressGesture.allowableMovement = 10;
  145. [self.contentView addGestureRecognizer:longPressGesture];
  146. }
  147. - (void)setIsEditType:(BOOL)isEditType
  148. {
  149. _isEditType = isEditType;
  150. if(isEditType){
  151. self.selectButton.hidden = NO;
  152. // if(_curUploadFileDataModel.curUploadStateType == uploadStateWait
  153. // ||_curUploadFileDataModel.curUploadStateType == uploadStateUploading
  154. // ||_curUploadFileDataModel.curUploadStateType == uploadStateSuspend){
  155. _upLoadStateButton.hidden = YES;
  156. //}
  157. }
  158. else{
  159. self.selectButton.hidden = YES;
  160. if(_curCouldPhoneFileModel.curDownloadStateType == downloadStateWait
  161. ||_curCouldPhoneFileModel.curDownloadStateType == downloadStateUploading
  162. ||_curCouldPhoneFileModel.curDownloadStateType == downloadStateSuspend){
  163. _upLoadStateButton.hidden = NO;
  164. }
  165. }
  166. }
  167. - (void)setIsSelectType:(BOOL)isSelectType
  168. {
  169. _isSelectType = isSelectType;
  170. if(isSelectType){
  171. self.selectButton.selected = YES;
  172. }
  173. else{
  174. self.selectButton.selected = NO;
  175. }
  176. }
  177. - (void)setCurCouldPhoneFileModel:(couldPhoneFileModel *)curCouldPhoneFileModel
  178. {
  179. _fileSpeedlabel.hidden = YES;
  180. _progressBgView.hidden = NO;
  181. _upLoadStateButton.hidden = NO;
  182. _curCouldPhoneFileModel = curCouldPhoneFileModel;
  183. [[downloadThumbnailManager shareInstance] handlToDownloadOneThumbnailWith:_curCouldPhoneFileModel];
  184. if(_curCouldPhoneFileModel.curDownloadStateType != downloadStateDone){
  185. //HLog(@"hhhhhhh2 %@ %ld ",_curCouldPhoneFileModel.name,_curCouldPhoneFileModel.didDownloadBytes);
  186. }
  187. // NSString*pathStr = [cachesFileManager getFilePathWithName:curUploadFileDataModel.filename type:curUploadFileDataModel.curUploadFileType];
  188. // UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
  189. //
  190. // if(image){
  191. // _fileImageView.image = image;
  192. // }
  193. // else
  194. {
  195. _fileImageView.image = [UIImage imageNamed:@"uploadFile_image"];
  196. if([curCouldPhoneFileModel.fileType isEqualToString:@"video"])
  197. {
  198. _fileImageView.image = [UIImage imageNamed:@"uploadFile_Video"];
  199. }
  200. }
  201. NSString *fileName = [_curCouldPhoneFileModel getFileNameFun];
  202. NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadThumbnail];
  203. UIImage *curImage = [UIImage imageWithContentsOfFile:pathStr];
  204. if(curImage){
  205. _fileImageView.image = curImage;
  206. }
  207. else{
  208. //不处理
  209. }
  210. _fileNamelabel.text = curCouldPhoneFileModel.name;
  211. //进度设置
  212. CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2);
  213. NSInteger totalSize = curCouldPhoneFileModel.length;
  214. if(totalSize == 0.0){
  215. totalSize += 0.0001;
  216. }
  217. CGFloat curProgress = curCouldPhoneFileModel.didDownloadBytes/(totalSize*1.0);
  218. if(curProgress > 1){
  219. curProgress = 1;
  220. }
  221. frame.size.width = frame.size.width * curProgress;
  222. if (isnan(frame.size.width))
  223. {
  224. HLog(@"计算出错了");
  225. frame.size.width = 10.0;
  226. }
  227. // HLog(@"%f, %f, %f, %f,",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
  228. // HLog(@"%@",_progressSelectView);
  229. _progressSelectView.frame = frame;
  230. //_glayer.frame = frame;
  231. NSString * totalSizeStr = nil;
  232. NSInteger totalSize_k = totalSize / 1024;
  233. if(totalSize_k < 1024){
  234. totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  235. }
  236. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  237. totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  238. }
  239. else{
  240. totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  241. }
  242. NSString * didUploadStr = nil;
  243. NSInteger didUploadSize_k = curCouldPhoneFileModel.didDownloadBytes / 1024;
  244. if(didUploadSize_k < 1024){
  245. didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k];
  246. }
  247. else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){
  248. didUploadStr = [[NSString alloc] initWithFormat:@"%.1fMB",didUploadSize_k/1024.0];
  249. }
  250. else{
  251. didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0];
  252. }
  253. if(curCouldPhoneFileModel.curDownloadStateType == downloadStateWait){
  254. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_wait"] forState:UIControlStateNormal];
  255. }
  256. else if(curCouldPhoneFileModel.curDownloadStateType == downloadStateUploading){
  257. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  258. _fileSpeedlabel.hidden = NO;
  259. //
  260. _fileSpeedlabel.text = @"";
  261. if(curCouldPhoneFileModel.preTimeInterval <= 0){
  262. _fileSpeedlabel.text = @"--";
  263. }
  264. else{
  265. NSTimeInterval timers = curCouldPhoneFileModel.curTimeInterval - curCouldPhoneFileModel.preTimeInterval;
  266. if(timers > 0){
  267. //long speed= 1*1024*1024 / timers;
  268. long speed= (curCouldPhoneFileModel.didDownloadBytes - curCouldPhoneFileModel.preDownloadBytes) / timers;
  269. NSString * speedStr = nil;
  270. NSInteger speed_k = speed / (1024);
  271. if(speed_k < 1024){
  272. speedStr = [[NSString alloc] initWithFormat:@"%ldKB/s",speed_k];
  273. }
  274. else {
  275. speedStr = [[NSString alloc] initWithFormat:@"%.1fMB/s",speed_k/1024.0];
  276. }
  277. _fileSpeedlabel.text = speedStr;
  278. }
  279. }
  280. curCouldPhoneFileModel.preTimeInterval = curCouldPhoneFileModel.curTimeInterval;
  281. curCouldPhoneFileModel.preDownloadBytes = curCouldPhoneFileModel.didDownloadBytes;
  282. }
  283. else if(curCouldPhoneFileModel.curDownloadStateType == downloadStateSuspend){
  284. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  285. }
  286. else if(curCouldPhoneFileModel.curDownloadStateType == downloadStateFail){
  287. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_reStart"] forState:UIControlStateNormal];
  288. _progressBgView.hidden = YES;
  289. }
  290. else{
  291. _upLoadStateButton.hidden = YES;
  292. _progressBgView.hidden = YES;
  293. }
  294. if(curCouldPhoneFileModel.curDownloadStateType == downloadStateDone){
  295. NSString*tipStr = NSLocalizedString(@"File_download_Path_default_done",nil);
  296. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  297. }
  298. else if(curCouldPhoneFileModel.curDownloadStateType == downloadStateFail){
  299. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@",totalSizeStr];
  300. }
  301. else{
  302. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  303. }
  304. }
  305. -(void)longPressClick:(UILongPressGestureRecognizer *)press{
  306. //state属性是所有手势父类提供的方法,用于记录手势的状态
  307. if(press.state == UIGestureRecognizerStateBegan){
  308. //NSLog(@"长按手势开始响应!");
  309. if(_didLongPressClick){
  310. _didLongPressClick();
  311. }
  312. }else if (press.state == UIGestureRecognizerStateChanged){
  313. //NSLog(@"长按手势状态发生改变!");
  314. }else{
  315. //NSLog(@"长按手势结束!");
  316. }
  317. }
  318. - (void)didClickSelectButFun:(UIButton*)but
  319. {
  320. but.selected = !but.selected;
  321. if(_didClckSelectBut){
  322. _didClckSelectBut(but.selected);
  323. }
  324. }
  325. - (void)tapClick:(UIButton*)but{
  326. if(_didTapPressClick){
  327. _didTapPressClick();
  328. }
  329. if(_curCouldPhoneFileModel.curDownloadStateType == downloadStateUploading){
  330. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  331. }
  332. else if(_curCouldPhoneFileModel.curDownloadStateType == downloadStateSuspend){
  333. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  334. }
  335. but.userInteractionEnabled = NO;
  336. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  337. but.userInteractionEnabled = YES;
  338. });
  339. }
  340. @end