downloadFileRecordCell.m 24 KB

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