boxDownloadFileRecordCell.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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(downloadTaskExeIng:) name:SGDownloadTaskExeing object:nil];
  30. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskExeEnd:) name:SGDownloadTaskExeEnd object:nil];
  31. //
  32. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskExeError:) name:SGDownloadTaskExeError object:nil];
  33. //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskExeSupend:) name:SGDownloadTaskExeSuspend object:nil];
  34. [self drawView];
  35. }
  36. return self;
  37. }
  38. - (void)drawView
  39. {
  40. _fileImageView = [[UIImageView alloc] init];
  41. //_fileImageView.backgroundColor = [UIColor greenColor];
  42. [_fileImageView setContentMode:(UIViewContentModeScaleAspectFit)];
  43. [self.contentView addSubview:_fileImageView];
  44. [_fileImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.left.mas_equalTo(15);
  46. make.width.mas_equalTo(34);
  47. make.height.mas_equalTo(34);
  48. make.bottom.mas_equalTo(-18);
  49. }];
  50. _upLoadStateButton = [[UIButton alloc] init];
  51. //_upLoadStateButton.backgroundColor = [UIColor redColor];
  52. [self.contentView addSubview:_upLoadStateButton];
  53. [_upLoadStateButton addTarget:self action:@selector(tapClick:) forControlEvents:UIControlEventTouchUpInside];
  54. [_upLoadStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.right.mas_equalTo(-5);
  56. make.width.mas_equalTo(50);
  57. make.height.mas_equalTo(50);
  58. make.bottom.mas_equalTo(-18);
  59. }];
  60. if (self.selectButton == nil) {
  61. UIButton *but = [[UIButton alloc] init];
  62. [but setImage:[UIImage imageNamed:@"upload_file_uncheck"] forState:UIControlStateNormal];
  63. [but setImage:[UIImage imageNamed:@"upload_file_check"] forState:UIControlStateSelected];
  64. [self.contentView addSubview:but];
  65. [but addTarget:self action:@selector(didClickSelectButFun:) forControlEvents:UIControlEventTouchUpInside];
  66. but.hidden = YES;
  67. //but.backgroundColor = [UIColor greenColor];
  68. //but.frame = CGRectMake([UIScreen mainScreen].bounds.size.width -10 - 25, 0, 25, 25);
  69. [but mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.width.mas_equalTo(30);
  71. make.height.mas_equalTo(30);
  72. make.centerX.mas_equalTo(_upLoadStateButton.mas_centerX);
  73. make.centerY.mas_equalTo(_upLoadStateButton.mas_centerY);
  74. }];
  75. self.selectButton = but;
  76. }
  77. _progressBgView = [[UIView alloc] init];
  78. _progressBgView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
  79. _progressBgView.layer.cornerRadius = 1.5;
  80. _progressBgView.layer.masksToBounds = YES;
  81. [self.contentView addSubview:_progressBgView];
  82. [_progressBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  84. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  85. make.height.mas_equalTo(2);
  86. make.centerY.mas_equalTo(0);
  87. }];
  88. _progressSelectView = [[UIView alloc] init];
  89. //_progressSelectView.backgroundColor = [UIColor hwColor:@"#F9F9F9" alpha:1.0];
  90. _progressSelectView.layer.cornerRadius = 1.5;
  91. _progressSelectView.layer.masksToBounds = YES;
  92. [_progressBgView addSubview:_progressSelectView];
  93. // [_progressSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
  94. // make.left.mas_equalTo(0);
  95. // make.right.mas_equalTo(-100);
  96. // make.height.mas_equalTo(3);
  97. // make.centerY.mas_equalTo(0);
  98. // }];
  99. // gradient
  100. _glayer = [CAGradientLayer layer];
  101. _glayer.startPoint = CGPointMake(0, 0.5);
  102. _glayer.endPoint = CGPointMake(1, 0.5);
  103. _glayer.colors = @[(__bridge id)[UIColor hwColor:@"#0BDDFD" alpha:1.0].CGColor, (__bridge id)[UIColor hwColor:@"#048CFB" alpha:1.0].CGColor];
  104. _glayer.locations = @[@(0), @(1.0f)];
  105. [_progressSelectView.layer addSublayer:_glayer];
  106. CGRect frame = CGRectMake(0, 0, SCREEN_W, 2);
  107. //_progressSelectView.frame = frame;
  108. _glayer.frame = frame;
  109. _fileNamelabel = [[UILabel alloc] init];
  110. _fileNamelabel.font = [UIFont boldSystemFontOfSize:16.0];
  111. _fileNamelabel.textColor = [UIColor blackColor];
  112. //_fileNamelabel.backgroundColor = [UIColor greenColor];
  113. _fileNamelabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  114. [self.contentView addSubview:_fileNamelabel];
  115. [_fileNamelabel mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  117. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  118. make.height.mas_equalTo(19);
  119. make.bottom.mas_equalTo(_progressBgView.mas_top).offset(-3);
  120. }];
  121. _fileProgresslabel = [[UILabel alloc] init];
  122. _fileProgresslabel.font = [UIFont systemFontOfSize:12.0];
  123. _fileProgresslabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  124. //_fileProgresslabel.backgroundColor = [UIColor greenColor];
  125. [self.contentView addSubview:_fileProgresslabel];
  126. [_fileProgresslabel mas_makeConstraints:^(MASConstraintMaker *make) {
  127. make.left.mas_equalTo(_fileImageView.mas_right).offset(10);
  128. make.right.mas_equalTo(_progressBgView.mas_centerX).offset(100);
  129. make.height.mas_equalTo(15);
  130. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  131. }];
  132. _fileSpeedlabel = [[UILabel alloc] init];
  133. _fileSpeedlabel.font = [UIFont systemFontOfSize:12.0];
  134. _fileSpeedlabel.textColor = [UIColor hwColor:@"#959799" alpha:1.0];
  135. _fileSpeedlabel.textAlignment = NSTextAlignmentRight;
  136. //_fileSpeedlabel.backgroundColor = [UIColor blackColor];
  137. [self.contentView addSubview:_fileSpeedlabel];
  138. [_fileSpeedlabel mas_makeConstraints:^(MASConstraintMaker *make) {
  139. make.right.mas_equalTo(_upLoadStateButton.mas_left).offset(-10);
  140. make.left.mas_equalTo(_progressBgView.mas_centerX);
  141. make.height.mas_equalTo(15);
  142. make.top.mas_equalTo(_progressBgView.mas_bottom).offset(3);
  143. }];
  144. //长按手势
  145. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
  146. //用几个手指触屏,默认1
  147. longPressGesture.numberOfTouchesRequired = 1;
  148. //设置最短长按时间,单位为秒(默认0.5)
  149. longPressGesture.minimumPressDuration = 1;
  150. //设置手势识别期间所允许的手势可移动范围
  151. longPressGesture.allowableMovement = 10;
  152. [self.contentView addGestureRecognizer:longPressGesture];
  153. }
  154. - (void)setIsEditType:(BOOL)isEditType
  155. {
  156. _isEditType = isEditType;
  157. if(isEditType){
  158. self.selectButton.hidden = NO;
  159. // if(_curUploadFileDataModel.curUploadStateType == uploadStateWait
  160. // ||_curUploadFileDataModel.curUploadStateType == uploadStateUploading
  161. // ||_curUploadFileDataModel.curUploadStateType == uploadStateSuspend){
  162. _upLoadStateButton.hidden = YES;
  163. //}
  164. }
  165. else{
  166. self.selectButton.hidden = YES;
  167. if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin
  168. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading
  169. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  170. _upLoadStateButton.hidden = NO;
  171. }
  172. }
  173. }
  174. - (void)setIsSelectType:(BOOL)isSelectType
  175. {
  176. _isSelectType = isSelectType;
  177. if(isSelectType){
  178. self.selectButton.selected = YES;
  179. }
  180. else{
  181. self.selectButton.selected = NO;
  182. }
  183. }
  184. - (void)setCurShareFileDataModel:(ShareFileDataModel *)curShareFileDataModel
  185. {
  186. _fileSpeedlabel.hidden = YES;
  187. _progressBgView.hidden = NO;
  188. _upLoadStateButton.hidden = NO;
  189. if (!_curShareFileDataModel
  190. || ![_curShareFileDataModel.fileUrl isEqualToString:curShareFileDataModel.fileUrl]) {
  191. if(_curShareFileDataModel){
  192. HLog(@"%@\n%@",_curShareFileDataModel.fileUrl,curShareFileDataModel.fileUrl);
  193. }
  194. [self setFixedValueByModel:curShareFileDataModel];
  195. }
  196. // if(_curShareFileDataModel
  197. // &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  198. // ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  199. // ){
  200. // return;
  201. // }
  202. _curShareFileDataModel = curShareFileDataModel;
  203. //HLog(@"1111111: %@ %@",curShareFileDataModel.fileUrl,curShareFileDataModel.fileSize);
  204. //进度设置
  205. CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2);
  206. if(curShareFileDataModel.totalSize == 0){
  207. curShareFileDataModel.totalSize = curShareFileDataModel.fileSize.longLongValue;
  208. }
  209. NSInteger totalSize = curShareFileDataModel.totalSize;
  210. if(totalSize == 0.0){
  211. totalSize += 0.0001;
  212. }
  213. CGFloat curProgress = curShareFileDataModel.currentSize/(totalSize*1.0);
  214. if(curProgress > 1){
  215. curProgress = 1;
  216. }
  217. frame.size.width = frame.size.width * curProgress;
  218. if (isnan(frame.size.width))
  219. {
  220. HLog(@"计算出错了");
  221. frame.size.width = 10.0;
  222. }
  223. // HLog(@"%f, %f, %f, %f,",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
  224. // HLog(@"%@",_progressSelectView);
  225. _progressSelectView.frame = frame;
  226. //_glayer.frame = frame;
  227. NSString * totalSizeStr = nil;
  228. NSInteger totalSize_k = totalSize / 1024;
  229. if(totalSize_k < 1024){
  230. totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  231. }
  232. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  233. totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  234. }
  235. else{
  236. totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  237. }
  238. NSString * didUploadStr = nil;
  239. NSInteger didUploadSize_k = curShareFileDataModel.currentSize / 1024;
  240. if(didUploadSize_k < 1024){
  241. didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k];
  242. }
  243. else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){
  244. didUploadStr = [[NSString alloc] initWithFormat:@"%.1fMB",didUploadSize_k/1024.0];
  245. }
  246. else{
  247. didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0];
  248. }
  249. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin){
  250. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_wait"] forState:UIControlStateNormal];
  251. }
  252. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  253. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  254. _fileSpeedlabel.hidden = NO;
  255. //
  256. _fileSpeedlabel.text = @"";
  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. _fileSpeedlabel.text = @"";
  291. }
  292. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone){
  293. NSString*tipStr = NSLocalizedString(@"File_download_Path_other_done",nil);
  294. if([iTools canSaveFileToAlbumByPhoto:YES withName:curShareFileDataModel.fileName]
  295. ||[iTools canSaveFileToAlbumByPhoto:NO withName:curShareFileDataModel.fileName]){
  296. tipStr = NSLocalizedString(@"File_download_Path_default_done",nil);
  297. }
  298. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  299. }
  300. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  301. NSString*tipStr = curShareFileDataModel.failMsg ? curShareFileDataModel.failMsg : @"";
  302. NSString *allStr = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  303. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:allStr];
  304. NSRange redRange = NSMakeRange([allStr rangeOfString:tipStr].location, [allStr rangeOfString:tipStr].length);
  305. UIColor *noteColor =[UIColor hwColor:@"#DD4E4E" alpha:1.0];
  306. [attrStr addAttribute:NSForegroundColorAttributeName value:noteColor range:redRange];
  307. //[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:redRange];
  308. _fileProgresslabel.attributedText = attrStr;
  309. }
  310. else{
  311. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  312. }
  313. }
  314. #pragma mark setImage
  315. - (void)setFixedValueByModel:(ShareFileDataModel *)curShareFileDataModel
  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 ([lastName rangeOfString:@"mp3"].location != NSNotFound
  338. ||[lastName rangeOfString:@"wav"].location != NSNotFound
  339. ||[lastName rangeOfString:@"flac"].location != NSNotFound
  340. ||[lastName rangeOfString:@"aac"].location != NSNotFound
  341. ||[lastName rangeOfString:@"ogg"].location != NSNotFound
  342. ) {
  343. _fileImageView.image = [UIImage imageNamed:@"uploadFile_audio"];
  344. }
  345. else if ([lastName rangeOfString:@"jpg"].location != NSNotFound
  346. ||[lastName rangeOfString:@"jpeg"].location != NSNotFound
  347. ||[lastName rangeOfString:@"png"].location != NSNotFound
  348. ||[lastName rangeOfString:@"gif"].location != NSNotFound
  349. ||[lastName rangeOfString:@"bmp"].location != NSNotFound
  350. ||[lastName rangeOfString:@"svg"].location != NSNotFound
  351. ||[lastName rangeOfString:@"tiff"].location != NSNotFound
  352. ||[lastName rangeOfString:@"heic"].location != NSNotFound
  353. ) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  354. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  355. NSString *fileUrl = curShareFileDataModel.headUrl;
  356. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  357. UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_image"];
  358. [_fileImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  359. if(image){
  360. //HLog(@"11图片1:%@",imageURL.absoluteString);
  361. }
  362. else{
  363. //HLog(@"11图片0:%@",imageURL.absoluteString);
  364. }
  365. }];
  366. }
  367. else if ([lastName rangeOfString:@"mp4"].location != NSNotFound
  368. ||[lastName rangeOfString:@"avi"].location != NSNotFound
  369. ||[lastName rangeOfString:@"flv"].location != NSNotFound
  370. ||[lastName rangeOfString:@"wmv"].location != NSNotFound
  371. ||[lastName rangeOfString:@"wkm"].location != NSNotFound
  372. ||[lastName rangeOfString:@"ts"].location != NSNotFound
  373. ||[lastName rangeOfString:@"mov"].location != NSNotFound
  374. ||[lastName rangeOfString:@"rmvb"].location != NSNotFound
  375. ) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  376. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  377. NSString *fileUrl = curShareFileDataModel.headUrl;
  378. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  379. UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_Video"];
  380. [_fileImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:defaultImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  381. if(image){
  382. //HLog(@"11图片1:%@",imageURL.absoluteString);
  383. }
  384. else{
  385. //HLog(@"11图片0:%@",imageURL.absoluteString);
  386. }
  387. }];
  388. }
  389. else{
  390. _fileImageView.image = [UIImage imageNamed:@"uploadFile_other"];
  391. }
  392. }
  393. }
  394. //下载中最更进度
  395. - (void)updateDataDownloadingBy:(ShareFileDataModel*)curShareFileDataModel{
  396. if(_curShareFileDataModel
  397. &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  398. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  399. ){
  400. return;
  401. }
  402. //进度设置
  403. CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2);
  404. if(curShareFileDataModel.totalSize == 0){
  405. curShareFileDataModel.totalSize = curShareFileDataModel.fileSize.longLongValue;
  406. }
  407. NSInteger totalSize = curShareFileDataModel.totalSize;
  408. if(totalSize == 0.0){
  409. totalSize += 0.0001;
  410. }
  411. CGFloat curProgress = curShareFileDataModel.currentSize/(totalSize*1.0);
  412. if(curProgress > 1){
  413. curProgress = 1;
  414. }
  415. frame.size.width = frame.size.width * curProgress;
  416. if (isnan(frame.size.width))
  417. {
  418. HLog(@"计算出错了");
  419. frame.size.width = 10.0;
  420. }
  421. // HLog(@"%f, %f, %f, %f,",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
  422. // HLog(@"%@",_progressSelectView);
  423. _progressSelectView.frame = frame;
  424. //_glayer.frame = frame;
  425. NSString * totalSizeStr = nil;
  426. NSInteger totalSize_k = totalSize / 1024;
  427. if(totalSize_k < 1024){
  428. totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  429. }
  430. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  431. totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  432. }
  433. else{
  434. totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  435. }
  436. NSString * didUploadStr = nil;
  437. NSInteger didUploadSize_k = curShareFileDataModel.currentSize / 1024;
  438. if(didUploadSize_k < 1024){
  439. didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k];
  440. }
  441. else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){
  442. didUploadStr = [[NSString alloc] initWithFormat:@"%.1fMB",didUploadSize_k/1024.0];
  443. }
  444. else{
  445. didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0];
  446. }
  447. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin){
  448. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_wait"] forState:UIControlStateNormal];
  449. }
  450. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  451. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  452. _fileSpeedlabel.hidden = NO;
  453. //
  454. _fileSpeedlabel.text = @"";
  455. if(curShareFileDataModel.preTimeInterval <= 0
  456. || curShareFileDataModel.currentSize == curShareFileDataModel.preDownloadBytes){
  457. //_fileSpeedlabel.text = @"--";
  458. }
  459. else{
  460. NSTimeInterval timers = curShareFileDataModel.curTimeInterval - curShareFileDataModel.preTimeInterval;
  461. if(timers > 0){
  462. //long speed= 1*1024*1024 / timers;
  463. long speed= (curShareFileDataModel.currentSize - curShareFileDataModel.preDownloadBytes) / timers;
  464. NSString * speedStr = nil;
  465. NSInteger speed_k = speed / (1024);
  466. if(speed_k < 1024){
  467. speedStr = [[NSString alloc] initWithFormat:@"%ldKB/s",speed_k];
  468. }
  469. else {
  470. speedStr = [[NSString alloc] initWithFormat:@"%.1fMB/s",speed_k/1024.0];
  471. }
  472. _fileSpeedlabel.text = speedStr;
  473. }
  474. }
  475. curShareFileDataModel.preTimeInterval = curShareFileDataModel.curTimeInterval;
  476. curShareFileDataModel.preDownloadBytes = curShareFileDataModel.currentSize;
  477. }
  478. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  479. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  480. }
  481. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  482. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_reStart"] forState:UIControlStateNormal];
  483. _progressBgView.hidden = YES;
  484. }
  485. else{
  486. _upLoadStateButton.hidden = YES;
  487. _progressBgView.hidden = YES;
  488. }
  489. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone){
  490. NSString*tipStr = NSLocalizedString(@"File_download_Path_other_done",nil);
  491. if([iTools canSaveFileToAlbumByPhoto:YES withName:curShareFileDataModel.fileName]
  492. ||[iTools canSaveFileToAlbumByPhoto:NO withName:curShareFileDataModel.fileName]){
  493. tipStr = NSLocalizedString(@"File_download_Path_default_done",nil);
  494. }
  495. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  496. }
  497. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  498. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@",totalSizeStr];
  499. }
  500. else{
  501. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  502. }
  503. }
  504. -(void)longPressClick:(UILongPressGestureRecognizer *)press{
  505. //state属性是所有手势父类提供的方法,用于记录手势的状态
  506. if(press.state == UIGestureRecognizerStateBegan){
  507. //NSLog(@"长按手势开始响应!");
  508. if(_didLongPressClick){
  509. _didLongPressClick();
  510. }
  511. }else if (press.state == UIGestureRecognizerStateChanged){
  512. //NSLog(@"长按手势状态发生改变!");
  513. }else{
  514. //NSLog(@"长按手势结束!");
  515. }
  516. }
  517. - (void)didClickSelectButFun:(UIButton*)but
  518. {
  519. but.selected = !but.selected;
  520. if(_didClckSelectBut){
  521. _didClckSelectBut(but.selected);
  522. }
  523. }
  524. - (void)tapClick:(UIButton*)but{
  525. if(_didTapPressClick){
  526. _didTapPressClick();
  527. }
  528. if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  529. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  530. }
  531. else if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  532. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  533. }
  534. but.userInteractionEnabled = NO;
  535. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  536. but.userInteractionEnabled = YES;
  537. });
  538. }
  539. - (void)downloadTaskExeIng:(NSNotification *)notification
  540. {
  541. //test code
  542. // NSMutableArray *allOperation = [[SGDownloadManager shareManager] getAllOperation];
  543. // NSInteger index = 0;
  544. // for (SGDownloadOperation *model in allOperation){
  545. // HLog(@"%ld %ld %@",index,model.downloadState,model.fileName);
  546. // index++;
  547. // }
  548. SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  549. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  550. //HLog(@"%@\n %@",urlString,_curShareFileDataModel.fileUrl);
  551. if (![urlString isEqualToString:_curShareFileDataModel.fileUrl]
  552. ||model.totalSize == model.currentSize) { // 不是这一个任务
  553. return;
  554. }
  555. if(_curShareFileDataModel
  556. &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  557. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  558. ){
  559. return;
  560. }
  561. _curShareFileDataModel.downloadBoxStateType = downloadBoxStateDownloadloading;
  562. _curShareFileDataModel.currentSize = model.currentSize;
  563. _curShareFileDataModel.curTimeInterval = [[NSDate date] timeIntervalSince1970];
  564. mainBlock(^{
  565. [self updateDataDownloadingBy:self->_curShareFileDataModel];
  566. });
  567. HLog(@"下载中");
  568. }
  569. #pragma mark 下载代理
  570. - (void)downloadItemStatusChanged:(YCDownloadItem *)item {
  571. //[self setDownloadStatus:item.downloadStatus];
  572. }
  573. - (void)downloadItem:(YCDownloadItem *)item downloadedSize:(int64_t)downloadedSize totalSize:(int64_t)totalSize {
  574. //解码
  575. NSString * urlString = [item.downloadURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  576. HLog(@"下载中:%lld --- %lld \n%@\n%@",downloadedSize,totalSize,urlString,_curShareFileDataModel.fileUrl);
  577. if (![urlString isEqualToString:_curShareFileDataModel.fileUrl]
  578. ||downloadedSize== totalSize) { // 不是这一个任务
  579. return;
  580. }
  581. if(_curShareFileDataModel
  582. &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  583. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  584. ){
  585. return;
  586. }
  587. _curShareFileDataModel.downloadBoxStateType = downloadBoxStateDownloadloading;
  588. _curShareFileDataModel.currentSize = downloadedSize;
  589. _curShareFileDataModel.curTimeInterval = [[NSDate date] timeIntervalSince1970];
  590. mainBlock(^{
  591. [self updateDataDownloadingBy:self->_curShareFileDataModel];
  592. });
  593. }
  594. - (void)downloadItem:(YCDownloadItem *)item speed:(NSUInteger)speed speedDesc:(NSString *)speedDesc {
  595. //self.speedLbl.text = speedDesc;
  596. }
  597. @end