boxDownloadFileRecordCell.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. //#import "mixDownloadManager.h"
  12. #import "customDownloadManager.h"
  13. @interface boxDownloadFileRecordCell ()
  14. @property(nonatomic,strong) UIImageView *fileImageView;
  15. @property(nonatomic,strong) UIButton *upLoadStateButton;
  16. @property (nonatomic, strong) UIButton *selectButton;
  17. @property(nonatomic,strong) UIView *progressBgView;
  18. @property(nonatomic,strong) UIView *progressSelectView;
  19. @property(nonatomic,strong) CAGradientLayer *glayer;
  20. @property(nonatomic,strong) UILabel *fileNamelabel;
  21. @property(nonatomic,strong) UILabel *fileProgresslabel;
  22. @property(nonatomic,strong) UILabel *fileSpeedlabel;
  23. @end
  24. @implementation boxDownloadFileRecordCell
  25. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  26. {
  27. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  28. if (self)
  29. {
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskDownloadingNoti:) name:customDownloadTaskExeing object:nil];
  31. [self drawView];
  32. }
  33. return self;
  34. }
  35. - (void)drawView
  36. {
  37. _fileImageView = [[UIImageView alloc] init];
  38. //_fileImageView.backgroundColor = [UIColor greenColor];
  39. [_fileImageView setContentMode:(UIViewContentModeScaleAspectFit)];
  40. [self.contentView addSubview:_fileImageView];
  41. [_fileImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.left.mas_equalTo(15);
  43. make.width.mas_equalTo(34);
  44. make.height.mas_equalTo(34);
  45. make.bottom.mas_equalTo(-18);
  46. }];
  47. _upLoadStateButton = [[UIButton alloc] init];
  48. //_upLoadStateButton.backgroundColor = [UIColor redColor];
  49. [self.contentView addSubview:_upLoadStateButton];
  50. [_upLoadStateButton addTarget:self action:@selector(tapClick:) forControlEvents:UIControlEventTouchUpInside];
  51. [_upLoadStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.right.mas_equalTo(-5);
  53. make.width.mas_equalTo(50);
  54. make.height.mas_equalTo(50);
  55. make.bottom.mas_equalTo(-18);
  56. }];
  57. if (self.selectButton == nil) {
  58. UIButton *but = [[UIButton alloc] init];
  59. [but setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal];
  60. [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
  61. [self.contentView addSubview:but];
  62. [but addTarget:self action:@selector(didClickSelectButFun:) forControlEvents:UIControlEventTouchUpInside];
  63. but.hidden = YES;
  64. //but.backgroundColor = [UIColor greenColor];
  65. //but.frame = CGRectMake([UIScreen mainScreen].bounds.size.width -10 - 25, 0, 25, 25);
  66. [but mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.width.mas_equalTo(30);
  68. make.height.mas_equalTo(30);
  69. make.centerX.mas_equalTo(_upLoadStateButton.mas_centerX);
  70. make.centerY.mas_equalTo(_upLoadStateButton.mas_centerY);
  71. }];
  72. self.selectButton = but;
  73. }
  74. _progressBgView = [[UIView alloc] init];
  75. _progressBgView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
  76. _progressBgView.layer.cornerRadius = 1.5;
  77. _progressBgView.layer.masksToBounds = YES;
  78. [self.contentView addSubview:_progressBgView];
  79. [_progressBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  81. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  82. make.height.mas_equalTo(2);
  83. make.centerY.mas_equalTo(0);
  84. }];
  85. _progressSelectView = [[UIView alloc] init];
  86. //_progressSelectView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
  87. _progressSelectView.layer.cornerRadius = 1.5;
  88. _progressSelectView.layer.masksToBounds = YES;
  89. [_progressBgView addSubview:_progressSelectView];
  90. // [_progressSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
  91. // make.left.mas_equalTo(0);
  92. // make.right.mas_equalTo(-100);
  93. // make.height.mas_equalTo(3);
  94. // make.centerY.mas_equalTo(0);
  95. // }];
  96. // gradient
  97. _glayer = [CAGradientLayer layer];
  98. _glayer.startPoint = CGPointMake(0, 0.5);
  99. _glayer.endPoint = CGPointMake(1, 0.5);
  100. _glayer.colors = @[(__bridge id)[UIColor hwColor:@"#0BDDFD" alpha:1.0].CGColor, (__bridge id)[UIColor hwColor:@"#048CFB" alpha:1.0].CGColor];
  101. _glayer.locations = @[@(0), @(1.0f)];
  102. [_progressSelectView.layer addSublayer:_glayer];
  103. CGRect frame = CGRectMake(0, 0, SCREEN_W, 2);
  104. //_progressSelectView.frame = frame;
  105. _glayer.frame = frame;
  106. _fileNamelabel = [[UILabel alloc] init];
  107. _fileNamelabel.font = [UIFont boldSystemFontOfSize:16.0];
  108. _fileNamelabel.textColor = [UIColor blackColor];
  109. //_fileNamelabel.backgroundColor = [UIColor greenColor];
  110. _fileNamelabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  111. [self.contentView addSubview:_fileNamelabel];
  112. [_fileNamelabel mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  114. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  115. make.height.mas_equalTo(19);
  116. make.bottom.mas_equalTo(_progressBgView.mas_top).offset(-3);
  117. }];
  118. _fileProgresslabel = [[UILabel alloc] init];
  119. _fileProgresslabel.font = [UIFont systemFontOfSize:12.0];
  120. _fileProgresslabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  121. //_fileProgresslabel.backgroundColor = [UIColor greenColor];
  122. [self.contentView addSubview:_fileProgresslabel];
  123. [_fileProgresslabel mas_makeConstraints:^(MASConstraintMaker *make) {
  124. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  125. make.right.mas_equalTo(_progressBgView.mas_centerX).offset(100);
  126. make.height.mas_equalTo(15);
  127. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  128. }];
  129. _fileSpeedlabel = [[UILabel alloc] init];
  130. _fileSpeedlabel.font = [UIFont systemFontOfSize:12.0];
  131. _fileSpeedlabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  132. _fileSpeedlabel.textAlignment = NSTextAlignmentRight;
  133. //_fileSpeedlabel.backgroundColor = [UIColor blackColor];
  134. [self.contentView addSubview:_fileSpeedlabel];
  135. [_fileSpeedlabel mas_makeConstraints:^(MASConstraintMaker *make) {
  136. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  137. make.left.mas_equalTo(_progressBgView.mas_centerX);
  138. // make.height.mas_equalTo(15);
  139. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  140. }];
  141. //长按手势
  142. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
  143. //用几个手指触屏,默认1
  144. longPressGesture.numberOfTouchesRequired = 1;
  145. //设置最短长按时间,单位为秒(默认0.5)
  146. longPressGesture.minimumPressDuration = 1;
  147. //设置手势识别期间所允许的手势可移动范围
  148. longPressGesture.allowableMovement = 10;
  149. [self.contentView addGestureRecognizer:longPressGesture];
  150. }
  151. - (void)setIsEditType:(BOOL)isEditType
  152. {
  153. _isEditType = isEditType;
  154. if(isEditType){
  155. self.selectButton.hidden = NO;
  156. // if(_curUploadFileDataModel.curUploadStateType == uploadStateWait
  157. // ||_curUploadFileDataModel.curUploadStateType == uploadStateUploading
  158. // ||_curUploadFileDataModel.curUploadStateType == uploadStateSuspend){
  159. _upLoadStateButton.hidden = YES;
  160. //}
  161. }
  162. else{
  163. self.selectButton.hidden = YES;
  164. if(//_curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin
  165. _curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading
  166. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  167. _upLoadStateButton.hidden = NO;
  168. }
  169. }
  170. }
  171. - (void)setIsSelectType:(BOOL)isSelectType
  172. {
  173. _isSelectType = isSelectType;
  174. if(isSelectType){
  175. self.selectButton.selected = YES;
  176. }
  177. else{
  178. self.selectButton.selected = NO;
  179. }
  180. }
  181. - (void)setCurShareFileDataModel:(ShareFileDataModel *)curShareFileDataModel
  182. {
  183. _progressBgView.hidden = NO;
  184. _upLoadStateButton.hidden = NO;
  185. if (!_curShareFileDataModel ||
  186. ![_curShareFileDataModel.fileUrl isEqualToString:curShareFileDataModel.fileUrl])
  187. {
  188. if(_curShareFileDataModel){
  189. HLog(@"%@\n%@",_curShareFileDataModel.fileUrl,curShareFileDataModel.fileUrl);
  190. }
  191. [self setFixedValueByModel:curShareFileDataModel];
  192. }
  193. // if(_curShareFileDataModel
  194. // &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  195. // ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  196. // ){
  197. // return;
  198. // }
  199. _curShareFileDataModel = curShareFileDataModel;
  200. if(_curShareFileDataModel.downloadBoxStateType != downloadBoxStateDownloadloading){
  201. _fileSpeedlabel.hidden = YES;
  202. _fileSpeedlabel.text = @" ";
  203. }
  204. //HLog(@"1111111: %@ %@",curShareFileDataModel.fileUrl,curShareFileDataModel.fileSize);
  205. //进度设置
  206. CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2);
  207. if(curShareFileDataModel.totalSize == 0){
  208. curShareFileDataModel.totalSize = curShareFileDataModel.fileSize.longLongValue;
  209. }
  210. NSInteger totalSize = curShareFileDataModel.totalSize;
  211. if(totalSize == 0.0){
  212. totalSize += 0.0001;
  213. }
  214. CGFloat curProgress = curShareFileDataModel.currentSize/(totalSize*1.0);
  215. if(curProgress > 1){
  216. curProgress = 1;
  217. }
  218. frame.size.width = frame.size.width * curProgress;
  219. if (isnan(frame.size.width))
  220. {
  221. HLog(@"计算出错了");
  222. frame.size.width = 10.0;
  223. }
  224. // HLog(@"%f, %f, %f, %f,",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
  225. // HLog(@"%@",_progressSelectView);
  226. _progressSelectView.frame = frame;
  227. //_glayer.frame = frame;
  228. NSString * totalSizeStr = nil;
  229. NSInteger totalSize_k = totalSize / 1024;
  230. if(totalSize_k < 1024){
  231. totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  232. }
  233. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  234. totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  235. }
  236. else{
  237. totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  238. }
  239. NSString * didUploadStr = nil;
  240. NSInteger didUploadSize_k = curShareFileDataModel.currentSize / 1024;
  241. if(didUploadSize_k < 1024){
  242. didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k];
  243. }
  244. else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){
  245. didUploadStr = [[NSString alloc] initWithFormat:@"%.1fMB",didUploadSize_k/1024.0];
  246. }
  247. else{
  248. didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0];
  249. }
  250. // if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin){
  251. // [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_wait"] forState:UIControlStateNormal];
  252. // }
  253. // else
  254. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  255. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  256. _fileSpeedlabel.hidden = NO;
  257. if(curShareFileDataModel.preTimeInterval <= 0
  258. || curShareFileDataModel.currentSize == curShareFileDataModel.preDownloadBytes){
  259. //_fileSpeedlabel.text = @"--";
  260. }
  261. else{
  262. NSTimeInterval timers = curShareFileDataModel.curTimeInterval - curShareFileDataModel.preTimeInterval;
  263. if(timers > 0){
  264. //long speed= 1*1024*1024 / timers;
  265. long speed= (curShareFileDataModel.currentSize - curShareFileDataModel.preDownloadBytes) / timers;
  266. NSString * speedStr = nil;
  267. NSInteger speed_k = speed / (1024);
  268. if(speed_k < 1024){
  269. speedStr = [[NSString alloc] initWithFormat:@"%ldKB/s",speed_k];
  270. }
  271. else {
  272. speedStr = [[NSString alloc] initWithFormat:@"%.1fMB/s",speed_k/1024.0];
  273. }
  274. _fileSpeedlabel.text = speedStr;
  275. }
  276. }
  277. curShareFileDataModel.preTimeInterval = curShareFileDataModel.curTimeInterval;
  278. curShareFileDataModel.preDownloadBytes = curShareFileDataModel.currentSize;
  279. }
  280. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  281. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  282. }
  283. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  284. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_reStart"] forState:UIControlStateNormal];
  285. _progressBgView.hidden = YES;
  286. }
  287. else{
  288. _upLoadStateButton.hidden = YES;
  289. _progressBgView.hidden = YES;
  290. }
  291. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone){
  292. NSString*tipStr = NSLocalizedString(@"File_download_Path_other_done",nil);
  293. if([iTools canSaveFileToAlbumByPhoto:YES withName:curShareFileDataModel.fileName]
  294. ||[iTools canSaveFileToAlbumByPhoto:NO withName:curShareFileDataModel.fileName]){
  295. tipStr = NSLocalizedString(@"File_download_Path_default_done",nil);
  296. }
  297. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  298. }
  299. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  300. NSString*tipStr = curShareFileDataModel.failMsg ? curShareFileDataModel.failMsg : @"";
  301. NSString *allStr = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  302. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:allStr];
  303. NSRange redRange = NSMakeRange([allStr rangeOfString:tipStr].location, [allStr rangeOfString:tipStr].length);
  304. UIColor *noteColor =[UIColor hwColor:@"#DD4E4E" alpha:1.0];
  305. [attrStr addAttribute:NSForegroundColorAttributeName value:noteColor range:redRange];
  306. //[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:redRange];
  307. _fileProgresslabel.attributedText = attrStr;
  308. }
  309. else{
  310. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  311. }
  312. }
  313. #pragma mark setImage
  314. - (void)setFixedValueByModel:(ShareFileDataModel *)curShareFileDataModel
  315. {
  316. if ([_fileNamelabel.text isEqualToString:curShareFileDataModel.fileName]) {
  317. return;
  318. }
  319. _fileNamelabel.text = curShareFileDataModel.fileName;
  320. NSArray *nameArr= [curShareFileDataModel.fileName componentsSeparatedByString:@"."];
  321. //HLog(@"fileName: %@",curShareFileDataModel.fileName);
  322. if (nameArr.count >= 2) {
  323. NSString *lastName = nameArr.lastObject;
  324. lastName = [lastName lowercaseString];
  325. //HLog(@"lastName: %@",lastName);
  326. if ([lastName rangeOfString:@"txt"].location != NSNotFound) {
  327. _fileImageView.image = [UIImage imageNamed:@"uploadFile_TXT"];
  328. }
  329. else if ([lastName rangeOfString:@"pdf"].location != NSNotFound) {
  330. _fileImageView.image = [UIImage imageNamed:@"uploadFile_pdf"];
  331. }
  332. else if ([lastName rangeOfString:@"xlsx"].location != NSNotFound) {
  333. _fileImageView.image = [UIImage imageNamed:@"uploadFile_xlsx"];
  334. }
  335. else if ([lastName rangeOfString:@"doc"].location != NSNotFound
  336. ||[lastName rangeOfString:@"docx"].location != NSNotFound) {
  337. _fileImageView.image = [UIImage imageNamed:@"uploadFile_doc"];
  338. }
  339. else if ([iTools isAudioFilewithName:lastName]) {
  340. _fileImageView.image = [UIImage imageNamed:@"uploadFile_audio"];
  341. }
  342. else if ([iTools isPhotoFilewithName:lastName]) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  343. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  344. NSString *fileUrl = curShareFileDataModel.headUrl;
  345. HLog(@"11图片1:%@",fileUrl);
  346. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  347. __block UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_image"];
  348. UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:fileUrl];
  349. if(cacheImage){
  350. _fileImageView.image = cacheImage;
  351. }
  352. else{
  353. [_fileImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  354. if(image){
  355. //HLog(@"11图片1:%@",imageURL.absoluteString);
  356. }
  357. else{
  358. //HLog(@"11图片0:%@",imageURL.absoluteString);
  359. //self->_fileImageView.image = defaultImage;
  360. }
  361. }];
  362. }
  363. }
  364. else if ([iTools isVideoFilewithName:lastName]) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  365. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  366. NSString *fileUrl = curShareFileDataModel.headUrl;
  367. HLog(@"11图片1:%@",fileUrl);
  368. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  369. __block UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_Video"];
  370. UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:fileUrl];
  371. if(cacheImage){
  372. _fileImageView.image = cacheImage;
  373. }
  374. else{
  375. [_fileImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  376. if(image){
  377. //HLog(@"11图片1:%@",imageURL.absoluteString);
  378. }
  379. else{
  380. //HLog(@"11图片0:%@",imageURL.absoluteString);
  381. //self->_fileImageView.image = defaultImage;
  382. }
  383. }];
  384. }
  385. }
  386. else{
  387. _fileImageView.image = [UIImage imageNamed:@"uploadFile_other"];
  388. }
  389. }
  390. }
  391. //下载中最更进度
  392. - (void)updateDataDownloadingBy:(ShareFileDataModel*)curShareFileDataModel{
  393. if(_curShareFileDataModel
  394. &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  395. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  396. ){
  397. return;
  398. }
  399. NSString * speedStr = nil;
  400. //进度设置
  401. CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2);
  402. if(curShareFileDataModel.totalSize == 0){
  403. curShareFileDataModel.totalSize = curShareFileDataModel.fileSize.longLongValue;
  404. }
  405. NSInteger totalSize = curShareFileDataModel.totalSize;
  406. if(totalSize == 0.0){
  407. totalSize += 0.0001;
  408. }
  409. CGFloat curProgress = curShareFileDataModel.currentSize/(totalSize*1.0);
  410. if(curProgress > 1){
  411. curProgress = 1;
  412. }
  413. frame.size.width = frame.size.width * curProgress;
  414. if (isnan(frame.size.width))
  415. {
  416. HLog(@"计算出错了");
  417. frame.size.width = 10.0;
  418. }
  419. _progressSelectView.frame = frame;
  420. //_glayer.frame = frame;
  421. NSString * totalSizeStr = nil;
  422. NSInteger totalSize_k = totalSize / 1024;
  423. if(totalSize_k < 1024){
  424. totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  425. }
  426. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  427. totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  428. }
  429. else{
  430. totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  431. }
  432. NSString * didUploadStr = nil;
  433. NSInteger didUploadSize_k = curShareFileDataModel.currentSize / 1024;
  434. if(didUploadSize_k < 1024){
  435. didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k];
  436. }
  437. else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){
  438. didUploadStr = [[NSString alloc] initWithFormat:@"%.1fMB",didUploadSize_k/1024.0];
  439. }
  440. else{
  441. didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0];
  442. }
  443. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  444. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  445. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  446. _fileSpeedlabel.hidden = NO;
  447. if(curShareFileDataModel.preTimeInterval <= 0
  448. || curShareFileDataModel.currentSize == curShareFileDataModel.preDownloadBytes){
  449. //_fileSpeedlabel.text = @"--";
  450. }
  451. else{
  452. NSTimeInterval timers = curShareFileDataModel.curTimeInterval - curShareFileDataModel.preTimeInterval;
  453. if(timers > 0){
  454. //long speed= 1*1024*1024 / timers;
  455. long speed= (curShareFileDataModel.currentSize - curShareFileDataModel.preDownloadBytes) / timers;
  456. NSInteger speed_k = speed / (1024);
  457. if(speed_k < 1024){
  458. speedStr = [[NSString alloc] initWithFormat:@"%ldKB/s",speed_k];
  459. }
  460. else {
  461. speedStr = [[NSString alloc] initWithFormat:@"%.1fMB/s",speed_k/1024.0];
  462. }
  463. //HLog(@"22211:%@",_fileSpeedlabel.description);
  464. }
  465. }
  466. curShareFileDataModel.preTimeInterval = curShareFileDataModel.curTimeInterval;
  467. curShareFileDataModel.preDownloadBytes = curShareFileDataModel.currentSize;
  468. }
  469. if (speedStr) {
  470. self->_fileSpeedlabel.text = speedStr;
  471. }
  472. }
  473. -(void)longPressClick:(UILongPressGestureRecognizer *)press{
  474. //state属性是所有手势父类提供的方法,用于记录手势的状态
  475. if(press.state == UIGestureRecognizerStateBegan){
  476. //NSLog(@"长按手势开始响应!");
  477. if(_didLongPressClick){
  478. _didLongPressClick();
  479. }
  480. }else if (press.state == UIGestureRecognizerStateChanged){
  481. //NSLog(@"长按手势状态发生改变!");
  482. }else{
  483. //NSLog(@"长按手势结束!");
  484. }
  485. }
  486. - (void)didClickSelectButFun:(UIButton*)but
  487. {
  488. but.selected = !but.selected;
  489. if(_didClckSelectBut){
  490. _didClckSelectBut(but.selected);
  491. }
  492. }
  493. - (void)tapClick:(UIButton*)but{
  494. if(_didTapPressClick){
  495. _didTapPressClick();
  496. }
  497. if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  498. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  499. }
  500. else if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  501. _fileSpeedlabel.text = @"";
  502. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  503. }
  504. but.userInteractionEnabled = NO;
  505. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  506. but.userInteractionEnabled = YES;
  507. });
  508. }
  509. //#pragma mark 下载通知
  510. - (void)downloadTaskDownloadingNoti:(NSNotification *)notification
  511. {
  512. customDownloadOperation *model = notification.userInfo.allValues.firstObject;
  513. if(!model)return;
  514. if (model.downloadState != customDownloadStateDoing) {
  515. return;
  516. }
  517. mainBlock(^{
  518. [self downloadItem:model downloadedSize:model.currentSize totalSize:model.totalSize];
  519. });
  520. }
  521. - (void)downloadItem:(customDownloadOperation *)model downloadedSize:(int64_t)downloadedSize totalSize:(int64_t)totalSize {
  522. //解码
  523. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  524. HLog(@"下载中:%lld --- %lld \n%@\n%@",downloadedSize,totalSize,urlString,_curShareFileDataModel.fileUrl);
  525. if(_curShareFileDataModel
  526. &&(_curShareFileDataModel.downloadBoxStateType != downloadBoxStateDownloadloading)
  527. ){
  528. HLog(@"当前状态 -- %u",_curShareFileDataModel.downloadBoxStateType);
  529. return;
  530. }
  531. if (![urlString isEqualToString:_curShareFileDataModel.fileUrl]
  532. ||downloadedSize == totalSize) { // 不是这一个任务
  533. return;
  534. }
  535. _curShareFileDataModel.downloadBoxStateType = downloadBoxStateDownloadloading;
  536. _curShareFileDataModel.currentSize = downloadedSize;
  537. _curShareFileDataModel.curTimeInterval = [[NSDate date] timeIntervalSince1970];
  538. KWeakSelf
  539. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  540. [weakSelf updateDataDownloadingBy:self->_curShareFileDataModel];
  541. });
  542. // mainBlock(^{
  543. // [self updateDataDownloadingBy:self->_curShareFileDataModel];
  544. // });
  545. }
  546. @end