boxDownloadFileRecordCell.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //
  2. // boxDownloadFileRecordCell.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/5/25.
  6. //
  7. #import "boxDownloadFileRecordCell.h"
  8. #import "cachesFileManager.h"
  9. #import <SDWebImage/SDWebImage.h>
  10. #import "downloadThumbnailManager.h"
  11. @interface boxDownloadFileRecordCell ()
  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 boxDownloadFileRecordCell
  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. _fileNamelabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  108. [self.contentView addSubview:_fileNamelabel];
  109. [_fileNamelabel mas_makeConstraints:^(MASConstraintMaker *make) {
  110. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  111. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  112. make.height.mas_equalTo(19);
  113. make.bottom.mas_equalTo(_progressBgView.mas_top).offset(-3);
  114. }];
  115. _fileProgresslabel = [[UILabel alloc] init];
  116. _fileProgresslabel.font = [UIFont systemFontOfSize:12.0];
  117. _fileProgresslabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  118. //_fileProgresslabel.backgroundColor = [UIColor greenColor];
  119. [self.contentView addSubview:_fileProgresslabel];
  120. [_fileProgresslabel mas_makeConstraints:^(MASConstraintMaker *make) {
  121. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  122. make.right.mas_equalTo(_progressBgView.mas_centerX).offset(100);
  123. make.height.mas_equalTo(15);
  124. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  125. }];
  126. _fileSpeedlabel = [[UILabel alloc] init];
  127. _fileSpeedlabel.font = [UIFont systemFontOfSize:12.0];
  128. _fileSpeedlabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  129. _fileSpeedlabel.textAlignment = NSTextAlignmentRight;
  130. //_fileSpeedlabel.backgroundColor = [UIColor blackColor];
  131. [self.contentView addSubview:_fileSpeedlabel];
  132. [_fileSpeedlabel mas_makeConstraints:^(MASConstraintMaker *make) {
  133. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  134. make.left.mas_equalTo(_progressBgView.mas_centerX);
  135. make.height.mas_equalTo(15);
  136. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  137. }];
  138. //长按手势
  139. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
  140. //用几个手指触屏,默认1
  141. longPressGesture.numberOfTouchesRequired = 1;
  142. //设置最短长按时间,单位为秒(默认0.5)
  143. longPressGesture.minimumPressDuration = 1;
  144. //设置手势识别期间所允许的手势可移动范围
  145. longPressGesture.allowableMovement = 10;
  146. [self.contentView addGestureRecognizer:longPressGesture];
  147. }
  148. - (void)setIsEditType:(BOOL)isEditType
  149. {
  150. _isEditType = isEditType;
  151. if(isEditType){
  152. self.selectButton.hidden = NO;
  153. // if(_curUploadFileDataModel.curUploadStateType == uploadStateWait
  154. // ||_curUploadFileDataModel.curUploadStateType == uploadStateUploading
  155. // ||_curUploadFileDataModel.curUploadStateType == uploadStateSuspend){
  156. _upLoadStateButton.hidden = YES;
  157. //}
  158. }
  159. else{
  160. self.selectButton.hidden = YES;
  161. if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin
  162. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading
  163. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  164. _upLoadStateButton.hidden = NO;
  165. }
  166. }
  167. }
  168. - (void)setIsSelectType:(BOOL)isSelectType
  169. {
  170. _isSelectType = isSelectType;
  171. if(isSelectType){
  172. self.selectButton.selected = YES;
  173. }
  174. else{
  175. self.selectButton.selected = NO;
  176. }
  177. }
  178. - (void)setCurShareFileDataModel:(ShareFileDataModel *)curShareFileDataModel
  179. {
  180. _fileSpeedlabel.hidden = YES;
  181. _progressBgView.hidden = NO;
  182. _upLoadStateButton.hidden = NO;
  183. if (!_curShareFileDataModel
  184. || ![_curShareFileDataModel.fileUrl isEqualToString:curShareFileDataModel.fileUrl]) {
  185. if(_curShareFileDataModel){
  186. HLog(@"%@\n%@",_curShareFileDataModel.fileUrl,curShareFileDataModel.fileUrl);
  187. }
  188. [self setFixedValueByModel:curShareFileDataModel];
  189. }
  190. _curShareFileDataModel = curShareFileDataModel;
  191. //HLog(@"1111111: %@ %@",curShareFileDataModel.fileUrl,curShareFileDataModel.fileSize);
  192. //进度设置
  193. CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2);
  194. if(curShareFileDataModel.totalSize == 0){
  195. curShareFileDataModel.totalSize = curShareFileDataModel.fileSize.longLongValue;
  196. }
  197. NSInteger totalSize = curShareFileDataModel.totalSize;
  198. if(totalSize == 0.0){
  199. totalSize += 0.0001;
  200. }
  201. CGFloat curProgress = curShareFileDataModel.currentSize/(totalSize*1.0);
  202. if(curProgress > 1){
  203. curProgress = 1;
  204. }
  205. frame.size.width = frame.size.width * curProgress;
  206. if (isnan(frame.size.width))
  207. {
  208. HLog(@"计算出错了");
  209. frame.size.width = 10.0;
  210. }
  211. // HLog(@"%f, %f, %f, %f,",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
  212. // HLog(@"%@",_progressSelectView);
  213. _progressSelectView.frame = frame;
  214. //_glayer.frame = frame;
  215. NSString * totalSizeStr = nil;
  216. NSInteger totalSize_k = totalSize / 1024;
  217. if(totalSize_k < 1024){
  218. totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  219. }
  220. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  221. totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  222. }
  223. else{
  224. totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  225. }
  226. NSString * didUploadStr = nil;
  227. NSInteger didUploadSize_k = curShareFileDataModel.currentSize / 1024;
  228. if(didUploadSize_k < 1024){
  229. didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k];
  230. }
  231. else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){
  232. didUploadStr = [[NSString alloc] initWithFormat:@"%.1fMB",didUploadSize_k/1024.0];
  233. }
  234. else{
  235. didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0];
  236. }
  237. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin){
  238. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_wait"] forState:UIControlStateNormal];
  239. }
  240. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  241. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  242. _fileSpeedlabel.hidden = NO;
  243. //
  244. _fileSpeedlabel.text = @"";
  245. if(curShareFileDataModel.preTimeInterval <= 0
  246. || curShareFileDataModel.currentSize == curShareFileDataModel.preDownloadBytes){
  247. //_fileSpeedlabel.text = @"--";
  248. }
  249. else{
  250. NSTimeInterval timers = curShareFileDataModel.curTimeInterval - curShareFileDataModel.preTimeInterval;
  251. if(timers > 0){
  252. //long speed= 1*1024*1024 / timers;
  253. long speed= (curShareFileDataModel.currentSize - curShareFileDataModel.preDownloadBytes) / timers;
  254. NSString * speedStr = nil;
  255. NSInteger speed_k = speed / (1024);
  256. if(speed_k < 1024){
  257. speedStr = [[NSString alloc] initWithFormat:@"%ldKB/s",speed_k];
  258. }
  259. else {
  260. speedStr = [[NSString alloc] initWithFormat:@"%.1fMB/s",speed_k/1024.0];
  261. }
  262. _fileSpeedlabel.text = speedStr;
  263. }
  264. }
  265. curShareFileDataModel.preTimeInterval = curShareFileDataModel.curTimeInterval;
  266. curShareFileDataModel.preDownloadBytes = curShareFileDataModel.currentSize;
  267. }
  268. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  269. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  270. }
  271. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  272. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_reStart"] forState:UIControlStateNormal];
  273. _progressBgView.hidden = YES;
  274. }
  275. else{
  276. _upLoadStateButton.hidden = YES;
  277. _progressBgView.hidden = YES;
  278. }
  279. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone){
  280. NSString*tipStr = NSLocalizedString(@"File_download_Path_default_done",nil);
  281. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  282. }
  283. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  284. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@",totalSizeStr];
  285. }
  286. else{
  287. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  288. }
  289. }
  290. #pragma mark setImage
  291. - (void)setFixedValueByModel:(ShareFileDataModel *)curShareFileDataModel
  292. {
  293. _fileNamelabel.text = curShareFileDataModel.fileName;
  294. NSArray *nameArr= [curShareFileDataModel.fileName componentsSeparatedByString:@"."];
  295. //HLog(@"fileName: %@",curShareFileDataModel.fileName);
  296. if (nameArr.count >= 2) {
  297. NSString *lastName = nameArr.lastObject;
  298. lastName = [lastName lowercaseString];
  299. //HLog(@"lastName: %@",lastName);
  300. if ([lastName rangeOfString:@"txt"].location != NSNotFound) {
  301. _fileImageView.image = [UIImage imageNamed:@"uploadFile_TXT"];
  302. }
  303. else if ([lastName rangeOfString:@"pdf"].location != NSNotFound) {
  304. _fileImageView.image = [UIImage imageNamed:@"uploadFile_pdf"];
  305. }
  306. else if ([lastName rangeOfString:@"xlsx"].location != NSNotFound) {
  307. _fileImageView.image = [UIImage imageNamed:@"uploadFile_xlsx"];
  308. }
  309. else if ([lastName rangeOfString:@"doc"].location != NSNotFound
  310. ||[lastName rangeOfString:@"docx"].location != NSNotFound) {
  311. _fileImageView.image = [UIImage imageNamed:@"uploadFile_doc"];
  312. }
  313. else if ([lastName rangeOfString:@"mp3"].location != NSNotFound
  314. ||[lastName rangeOfString:@"wav"].location != NSNotFound
  315. ||[lastName rangeOfString:@"flac"].location != NSNotFound
  316. ||[lastName rangeOfString:@"aac"].location != NSNotFound
  317. ||[lastName rangeOfString:@"ogg"].location != NSNotFound
  318. ) {
  319. _fileImageView.image = [UIImage imageNamed:@"uploadFile_audio"];
  320. }
  321. else if ([lastName rangeOfString:@"jpg"].location != NSNotFound
  322. ||[lastName rangeOfString:@"jpeg"].location != NSNotFound
  323. ||[lastName rangeOfString:@"png"].location != NSNotFound
  324. ||[lastName rangeOfString:@"gif"].location != NSNotFound
  325. ||[lastName rangeOfString:@"bmp"].location != NSNotFound
  326. ||[lastName rangeOfString:@"svg"].location != NSNotFound
  327. ||[lastName rangeOfString:@"tiff"].location != NSNotFound
  328. ||[lastName rangeOfString:@"heic"].location != NSNotFound
  329. ) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  330. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  331. NSString *fileUrl = curShareFileDataModel.headUrl;
  332. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  333. UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_image"];
  334. [_fileImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  335. if(image){
  336. //HLog(@"11图片1:%@",imageURL.absoluteString);
  337. }
  338. else{
  339. //HLog(@"11图片0:%@",imageURL.absoluteString);
  340. }
  341. }];
  342. }
  343. else if ([lastName rangeOfString:@"mp4"].location != NSNotFound
  344. ||[lastName rangeOfString:@"avi"].location != NSNotFound
  345. ||[lastName rangeOfString:@"flv"].location != NSNotFound
  346. ||[lastName rangeOfString:@"wmv"].location != NSNotFound
  347. ||[lastName rangeOfString:@"wkm"].location != NSNotFound
  348. ||[lastName rangeOfString:@"ts"].location != NSNotFound
  349. ||[lastName rangeOfString:@"mov"].location != NSNotFound
  350. ||[lastName rangeOfString:@"rmvb"].location != NSNotFound
  351. ) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  352. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  353. NSString *fileUrl = curShareFileDataModel.headUrl;
  354. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  355. UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_Video"];
  356. [_fileImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  357. if(image){
  358. //HLog(@"11图片1:%@",imageURL.absoluteString);
  359. }
  360. else{
  361. //HLog(@"11图片0:%@",imageURL.absoluteString);
  362. }
  363. }];
  364. }
  365. else{
  366. _fileImageView.image = [UIImage imageNamed:@"uploadFile_other"];
  367. }
  368. }
  369. }
  370. -(void)longPressClick:(UILongPressGestureRecognizer *)press{
  371. //state属性是所有手势父类提供的方法,用于记录手势的状态
  372. if(press.state == UIGestureRecognizerStateBegan){
  373. //NSLog(@"长按手势开始响应!");
  374. if(_didLongPressClick){
  375. _didLongPressClick();
  376. }
  377. }else if (press.state == UIGestureRecognizerStateChanged){
  378. //NSLog(@"长按手势状态发生改变!");
  379. }else{
  380. //NSLog(@"长按手势结束!");
  381. }
  382. }
  383. - (void)didClickSelectButFun:(UIButton*)but
  384. {
  385. but.selected = !but.selected;
  386. if(_didClckSelectBut){
  387. _didClckSelectBut(but.selected);
  388. }
  389. }
  390. - (void)tapClick:(UIButton*)but{
  391. if(_didTapPressClick){
  392. _didTapPressClick();
  393. }
  394. if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  395. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  396. }
  397. else if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  398. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  399. }
  400. but.userInteractionEnabled = NO;
  401. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  402. but.userInteractionEnabled = YES;
  403. });
  404. }
  405. @end