boxDownloadFileRecordCell.m 26 KB

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