boxDownloadFileRecordCell.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 "SGDownloadManager.h"
  12. @interface boxDownloadFileRecordCell ()
  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 boxDownloadFileRecordCell
  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:kDownloadTaskDownloadingNoti 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. _fileNamelabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  110. [self.contentView addSubview:_fileNamelabel];
  111. [_fileNamelabel mas_makeConstraints:^(MASConstraintMaker *make) {
  112. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  113. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  114. make.height.mas_equalTo(19);
  115. make.bottom.mas_equalTo(_progressBgView.mas_top).offset(-3);
  116. }];
  117. _fileProgresslabel = [[UILabel alloc] init];
  118. _fileProgresslabel.font = [UIFont systemFontOfSize:12.0];
  119. _fileProgresslabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  120. //_fileProgresslabel.backgroundColor = [UIColor greenColor];
  121. [self.contentView addSubview:_fileProgresslabel];
  122. [_fileProgresslabel mas_makeConstraints:^(MASConstraintMaker *make) {
  123. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  124. make.right.mas_equalTo(_progressBgView.mas_centerX).offset(100);
  125. make.height.mas_equalTo(15);
  126. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  127. }];
  128. _fileSpeedlabel = [[UILabel alloc] init];
  129. _fileSpeedlabel.font = [UIFont systemFontOfSize:12.0];
  130. _fileSpeedlabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  131. _fileSpeedlabel.textAlignment = NSTextAlignmentRight;
  132. //_fileSpeedlabel.backgroundColor = [UIColor blackColor];
  133. [self.contentView addSubview:_fileSpeedlabel];
  134. [_fileSpeedlabel mas_makeConstraints:^(MASConstraintMaker *make) {
  135. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  136. make.left.mas_equalTo(_progressBgView.mas_centerX);
  137. make.height.mas_equalTo(15);
  138. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  139. }];
  140. //长按手势
  141. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
  142. //用几个手指触屏,默认1
  143. longPressGesture.numberOfTouchesRequired = 1;
  144. //设置最短长按时间,单位为秒(默认0.5)
  145. longPressGesture.minimumPressDuration = 1;
  146. //设置手势识别期间所允许的手势可移动范围
  147. longPressGesture.allowableMovement = 10;
  148. [self.contentView addGestureRecognizer:longPressGesture];
  149. }
  150. - (void)setIsEditType:(BOOL)isEditType
  151. {
  152. _isEditType = isEditType;
  153. if(isEditType){
  154. self.selectButton.hidden = NO;
  155. // if(_curUploadFileDataModel.curUploadStateType == uploadStateWait
  156. // ||_curUploadFileDataModel.curUploadStateType == uploadStateUploading
  157. // ||_curUploadFileDataModel.curUploadStateType == uploadStateSuspend){
  158. _upLoadStateButton.hidden = YES;
  159. //}
  160. }
  161. else{
  162. self.selectButton.hidden = YES;
  163. if(//_curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin
  164. _curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading
  165. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  166. _upLoadStateButton.hidden = NO;
  167. }
  168. }
  169. }
  170. - (void)setIsSelectType:(BOOL)isSelectType
  171. {
  172. _isSelectType = isSelectType;
  173. if(isSelectType){
  174. self.selectButton.selected = YES;
  175. }
  176. else{
  177. self.selectButton.selected = NO;
  178. }
  179. }
  180. - (void)setCurShareFileDataModel:(ShareFileDataModel *)curShareFileDataModel
  181. {
  182. _fileSpeedlabel.hidden = YES;
  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. //HLog(@"1111111: %@ %@",curShareFileDataModel.fileUrl,curShareFileDataModel.fileSize);
  201. //进度设置
  202. CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2);
  203. if(curShareFileDataModel.totalSize == 0){
  204. curShareFileDataModel.totalSize = curShareFileDataModel.fileSize.longLongValue;
  205. }
  206. NSInteger totalSize = curShareFileDataModel.totalSize;
  207. if(totalSize == 0.0){
  208. totalSize += 0.0001;
  209. }
  210. CGFloat curProgress = curShareFileDataModel.currentSize/(totalSize*1.0);
  211. if(curProgress > 1){
  212. curProgress = 1;
  213. }
  214. frame.size.width = frame.size.width * curProgress;
  215. if (isnan(frame.size.width))
  216. {
  217. HLog(@"计算出错了");
  218. frame.size.width = 10.0;
  219. }
  220. // HLog(@"%f, %f, %f, %f,",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
  221. // HLog(@"%@",_progressSelectView);
  222. _progressSelectView.frame = frame;
  223. //_glayer.frame = frame;
  224. NSString * totalSizeStr = nil;
  225. NSInteger totalSize_k = totalSize / 1024;
  226. if(totalSize_k < 1024){
  227. totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  228. }
  229. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  230. totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  231. }
  232. else{
  233. totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  234. }
  235. NSString * didUploadStr = nil;
  236. NSInteger didUploadSize_k = curShareFileDataModel.currentSize / 1024;
  237. if(didUploadSize_k < 1024){
  238. didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k];
  239. }
  240. else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){
  241. didUploadStr = [[NSString alloc] initWithFormat:@"%.1fMB",didUploadSize_k/1024.0];
  242. }
  243. else{
  244. didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0];
  245. }
  246. // if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin){
  247. // [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_wait"] forState:UIControlStateNormal];
  248. // }
  249. // else
  250. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  251. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  252. _fileSpeedlabel.hidden = NO;
  253. //
  254. _fileSpeedlabel.text = @"";
  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:@"%.1fMB/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. _fileSpeedlabel.text = @"";
  289. }
  290. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone){
  291. NSString*tipStr = NSLocalizedString(@"File_download_Path_other_done",nil);
  292. if([iTools canSaveFileToAlbumByPhoto:YES withName:curShareFileDataModel.fileName]
  293. ||[iTools canSaveFileToAlbumByPhoto:NO withName:curShareFileDataModel.fileName]){
  294. tipStr = NSLocalizedString(@"File_download_Path_default_done",nil);
  295. }
  296. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  297. }
  298. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  299. NSString*tipStr = curShareFileDataModel.failMsg ? curShareFileDataModel.failMsg : @"";
  300. NSString *allStr = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  301. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:allStr];
  302. NSRange redRange = NSMakeRange([allStr rangeOfString:tipStr].location, [allStr rangeOfString:tipStr].length);
  303. UIColor *noteColor =[UIColor hwColor:@"#DD4E4E" alpha:1.0];
  304. [attrStr addAttribute:NSForegroundColorAttributeName value:noteColor range:redRange];
  305. //[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:redRange];
  306. _fileProgresslabel.attributedText = attrStr;
  307. }
  308. else{
  309. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  310. }
  311. }
  312. #pragma mark setImage
  313. - (void)setFixedValueByModel:(ShareFileDataModel *)curShareFileDataModel
  314. {
  315. if ([_fileNamelabel.text isEqualToString:curShareFileDataModel.fileName]) {
  316. return;
  317. }
  318. _fileNamelabel.text = curShareFileDataModel.fileName;
  319. NSArray *nameArr= [curShareFileDataModel.fileName componentsSeparatedByString:@"."];
  320. //HLog(@"fileName: %@",curShareFileDataModel.fileName);
  321. if (nameArr.count >= 2) {
  322. NSString *lastName = nameArr.lastObject;
  323. lastName = [lastName lowercaseString];
  324. //HLog(@"lastName: %@",lastName);
  325. if ([lastName rangeOfString:@"txt"].location != NSNotFound) {
  326. _fileImageView.image = [UIImage imageNamed:@"uploadFile_TXT"];
  327. }
  328. else if ([lastName rangeOfString:@"pdf"].location != NSNotFound) {
  329. _fileImageView.image = [UIImage imageNamed:@"uploadFile_pdf"];
  330. }
  331. else if ([lastName rangeOfString:@"xlsx"].location != NSNotFound) {
  332. _fileImageView.image = [UIImage imageNamed:@"uploadFile_xlsx"];
  333. }
  334. else if ([lastName rangeOfString:@"doc"].location != NSNotFound
  335. ||[lastName rangeOfString:@"docx"].location != NSNotFound) {
  336. _fileImageView.image = [UIImage imageNamed:@"uploadFile_doc"];
  337. }
  338. else if ([iTools isAudioFilewithName:lastName]) {
  339. _fileImageView.image = [UIImage imageNamed:@"uploadFile_audio"];
  340. }
  341. else if ([iTools isPhotoFilewithName:lastName]) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  342. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  343. NSString *fileUrl = curShareFileDataModel.headUrl;
  344. HLog(@"11图片1:%@",fileUrl);
  345. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  346. __block UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_image"];
  347. UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:fileUrl];
  348. if(cacheImage){
  349. _fileImageView.image = cacheImage;
  350. }
  351. else{
  352. [_fileImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  353. if(image){
  354. //HLog(@"11图片1:%@",imageURL.absoluteString);
  355. }
  356. else{
  357. //HLog(@"11图片0:%@",imageURL.absoluteString);
  358. //self->_fileImageView.image = defaultImage;
  359. }
  360. }];
  361. }
  362. }
  363. else if ([iTools isVideoFilewithName:lastName]) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  364. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  365. NSString *fileUrl = curShareFileDataModel.headUrl;
  366. HLog(@"11图片1:%@",fileUrl);
  367. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  368. __block UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_Video"];
  369. UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:fileUrl];
  370. if(cacheImage){
  371. _fileImageView.image = cacheImage;
  372. }
  373. else{
  374. [_fileImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  375. if(image){
  376. //HLog(@"11图片1:%@",imageURL.absoluteString);
  377. }
  378. else{
  379. //HLog(@"11图片0:%@",imageURL.absoluteString);
  380. //self->_fileImageView.image = defaultImage;
  381. }
  382. }];
  383. }
  384. }
  385. else{
  386. _fileImageView.image = [UIImage imageNamed:@"uploadFile_other"];
  387. }
  388. }
  389. }
  390. //下载中最更进度
  391. - (void)updateDataDownloadingBy:(ShareFileDataModel*)curShareFileDataModel{
  392. if(_curShareFileDataModel
  393. &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  394. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  395. ){
  396. return;
  397. }
  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. // HLog(@"%f, %f, %f, %f,",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
  418. // HLog(@"%@",_progressSelectView);
  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. // if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin){
  444. // [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_wait"] forState:UIControlStateNormal];
  445. // }
  446. // else
  447. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  448. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  449. _fileSpeedlabel.hidden = NO;
  450. //
  451. _fileSpeedlabel.text = @"";
  452. if(curShareFileDataModel.preTimeInterval <= 0
  453. || curShareFileDataModel.currentSize == curShareFileDataModel.preDownloadBytes){
  454. //_fileSpeedlabel.text = @"--";
  455. }
  456. else{
  457. NSTimeInterval timers = curShareFileDataModel.curTimeInterval - curShareFileDataModel.preTimeInterval;
  458. if(timers > 0){
  459. //long speed= 1*1024*1024 / timers;
  460. long speed= (curShareFileDataModel.currentSize - curShareFileDataModel.preDownloadBytes) / timers;
  461. NSString * speedStr = nil;
  462. NSInteger speed_k = speed / (1024);
  463. if(speed_k < 1024){
  464. speedStr = [[NSString alloc] initWithFormat:@"%ldKB/s",speed_k];
  465. }
  466. else {
  467. speedStr = [[NSString alloc] initWithFormat:@"%.1fMB/s",speed_k/1024.0];
  468. }
  469. _fileSpeedlabel.text = speedStr;
  470. }
  471. }
  472. curShareFileDataModel.preTimeInterval = curShareFileDataModel.curTimeInterval;
  473. curShareFileDataModel.preDownloadBytes = curShareFileDataModel.currentSize;
  474. }
  475. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  476. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  477. }
  478. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  479. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_reStart"] forState:UIControlStateNormal];
  480. _progressBgView.hidden = YES;
  481. }
  482. else{
  483. _upLoadStateButton.hidden = YES;
  484. _progressBgView.hidden = YES;
  485. }
  486. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone){
  487. NSString*tipStr = NSLocalizedString(@"File_download_Path_other_done",nil);
  488. if([iTools canSaveFileToAlbumByPhoto:YES withName:curShareFileDataModel.fileName]
  489. ||[iTools canSaveFileToAlbumByPhoto:NO withName:curShareFileDataModel.fileName]){
  490. tipStr = NSLocalizedString(@"File_download_Path_default_done",nil);
  491. }
  492. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  493. }
  494. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  495. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@",totalSizeStr];
  496. }
  497. else{
  498. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  499. }
  500. }
  501. -(void)longPressClick:(UILongPressGestureRecognizer *)press{
  502. //state属性是所有手势父类提供的方法,用于记录手势的状态
  503. if(press.state == UIGestureRecognizerStateBegan){
  504. //NSLog(@"长按手势开始响应!");
  505. if(_didLongPressClick){
  506. _didLongPressClick();
  507. }
  508. }else if (press.state == UIGestureRecognizerStateChanged){
  509. //NSLog(@"长按手势状态发生改变!");
  510. }else{
  511. //NSLog(@"长按手势结束!");
  512. }
  513. }
  514. - (void)didClickSelectButFun:(UIButton*)but
  515. {
  516. but.selected = !but.selected;
  517. if(_didClckSelectBut){
  518. _didClckSelectBut(but.selected);
  519. }
  520. }
  521. - (void)tapClick:(UIButton*)but{
  522. if(_didTapPressClick){
  523. _didTapPressClick();
  524. }
  525. if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  526. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  527. }
  528. else if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  529. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  530. }
  531. but.userInteractionEnabled = NO;
  532. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  533. but.userInteractionEnabled = YES;
  534. });
  535. }
  536. //#pragma mark 下载通知
  537. - (void)downloadTaskDownloadingNoti:(NSNotification *)notification
  538. {
  539. YCDownloadItem *item = notification.object;
  540. if(!item)return;
  541. if (item.downloadStatus != YCDownloadStatusDownloading) {
  542. return;
  543. }
  544. mainBlock(^{
  545. [self downloadItem:item downloadedSize:item.downloadedSize totalSize:item.fileSize];
  546. });
  547. }
  548. #pragma mark 下载代理
  549. - (void)downloadItemStatusChanged:(YCDownloadItem *)item {
  550. //[self setDownloadStatus:item.downloadStatus];
  551. }
  552. - (void)downloadItem:(YCDownloadItem *)item downloadedSize:(int64_t)downloadedSize totalSize:(int64_t)totalSize {
  553. //解码
  554. NSString * urlString = [item.downloadURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  555. HLog(@"下载中:%lld --- %lld \n%@\n%@",downloadedSize,totalSize,urlString,_curShareFileDataModel.fileUrl);
  556. if(_curShareFileDataModel
  557. &&(_curShareFileDataModel.downloadBoxStateType != downloadBoxStateDownloadloading)
  558. ){
  559. HLog(@"当前状态 -- %u",_curShareFileDataModel.downloadBoxStateType);
  560. return;
  561. }
  562. if (![urlString isEqualToString:_curShareFileDataModel.fileUrl]
  563. ||downloadedSize == totalSize) { // 不是这一个任务
  564. return;
  565. }
  566. _curShareFileDataModel.downloadBoxStateType = downloadBoxStateDownloadloading;
  567. _curShareFileDataModel.currentSize = downloadedSize;
  568. _curShareFileDataModel.curTimeInterval = [[NSDate date] timeIntervalSince1970];
  569. mainBlock(^{
  570. [self updateDataDownloadingBy:self->_curShareFileDataModel];
  571. });
  572. }
  573. - (void)downloadItem:(YCDownloadItem *)item speed:(NSUInteger)speed speedDesc:(NSString *)speedDesc {
  574. //self.speedLbl.text = speedDesc;
  575. }
  576. @end