boxDownloadFileRecordCell.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@",totalSizeStr];
  302. }
  303. else{
  304. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  305. }
  306. }
  307. #pragma mark setImage
  308. - (void)setFixedValueByModel:(ShareFileDataModel *)curShareFileDataModel
  309. {
  310. _fileNamelabel.text = curShareFileDataModel.fileName;
  311. NSArray *nameArr= [curShareFileDataModel.fileName componentsSeparatedByString:@"."];
  312. //HLog(@"fileName: %@",curShareFileDataModel.fileName);
  313. if (nameArr.count >= 2) {
  314. NSString *lastName = nameArr.lastObject;
  315. lastName = [lastName lowercaseString];
  316. //HLog(@"lastName: %@",lastName);
  317. if ([lastName rangeOfString:@"txt"].location != NSNotFound) {
  318. _fileImageView.image = [UIImage imageNamed:@"uploadFile_TXT"];
  319. }
  320. else if ([lastName rangeOfString:@"pdf"].location != NSNotFound) {
  321. _fileImageView.image = [UIImage imageNamed:@"uploadFile_pdf"];
  322. }
  323. else if ([lastName rangeOfString:@"xlsx"].location != NSNotFound) {
  324. _fileImageView.image = [UIImage imageNamed:@"uploadFile_xlsx"];
  325. }
  326. else if ([lastName rangeOfString:@"doc"].location != NSNotFound
  327. ||[lastName rangeOfString:@"docx"].location != NSNotFound) {
  328. _fileImageView.image = [UIImage imageNamed:@"uploadFile_doc"];
  329. }
  330. else if ([lastName rangeOfString:@"mp3"].location != NSNotFound
  331. ||[lastName rangeOfString:@"wav"].location != NSNotFound
  332. ||[lastName rangeOfString:@"flac"].location != NSNotFound
  333. ||[lastName rangeOfString:@"aac"].location != NSNotFound
  334. ||[lastName rangeOfString:@"ogg"].location != NSNotFound
  335. ) {
  336. _fileImageView.image = [UIImage imageNamed:@"uploadFile_audio"];
  337. }
  338. else if ([lastName rangeOfString:@"jpg"].location != NSNotFound
  339. ||[lastName rangeOfString:@"jpeg"].location != NSNotFound
  340. ||[lastName rangeOfString:@"png"].location != NSNotFound
  341. ||[lastName rangeOfString:@"gif"].location != NSNotFound
  342. ||[lastName rangeOfString:@"bmp"].location != NSNotFound
  343. ||[lastName rangeOfString:@"svg"].location != NSNotFound
  344. ||[lastName rangeOfString:@"tiff"].location != NSNotFound
  345. ||[lastName rangeOfString:@"heic"].location != NSNotFound
  346. ) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  347. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  348. NSString *fileUrl = curShareFileDataModel.headUrl;
  349. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  350. UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_image"];
  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. }
  358. }];
  359. }
  360. else if ([lastName rangeOfString:@"mp4"].location != NSNotFound
  361. ||[lastName rangeOfString:@"avi"].location != NSNotFound
  362. ||[lastName rangeOfString:@"flv"].location != NSNotFound
  363. ||[lastName rangeOfString:@"wmv"].location != NSNotFound
  364. ||[lastName rangeOfString:@"wkm"].location != NSNotFound
  365. ||[lastName rangeOfString:@"ts"].location != NSNotFound
  366. ||[lastName rangeOfString:@"mov"].location != NSNotFound
  367. ||[lastName rangeOfString:@"rmvb"].location != NSNotFound
  368. ) {//JPG、JPEG、PNG、GIF、BMP、SVG、TIFF、HEIC。
  369. //MP4、AVI、FLV、WMV、MKV、TS、MOV、RMVB。
  370. NSString *fileUrl = curShareFileDataModel.headUrl;
  371. fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  372. UIImage * defaultImage = [UIImage imageNamed:@"uploadFile_Video"];
  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. }
  380. }];
  381. }
  382. else{
  383. _fileImageView.image = [UIImage imageNamed:@"uploadFile_other"];
  384. }
  385. }
  386. }
  387. //下载中最更进度
  388. - (void)updateDataDownloadingBy:(ShareFileDataModel*)curShareFileDataModel{
  389. if(_curShareFileDataModel
  390. &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  391. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  392. ){
  393. return;
  394. }
  395. //进度设置
  396. CGRect frame = CGRectMake(0, 0, SCREEN_W - 15*2 - 34*2, 2);
  397. if(curShareFileDataModel.totalSize == 0){
  398. curShareFileDataModel.totalSize = curShareFileDataModel.fileSize.longLongValue;
  399. }
  400. NSInteger totalSize = curShareFileDataModel.totalSize;
  401. if(totalSize == 0.0){
  402. totalSize += 0.0001;
  403. }
  404. CGFloat curProgress = curShareFileDataModel.currentSize/(totalSize*1.0);
  405. if(curProgress > 1){
  406. curProgress = 1;
  407. }
  408. frame.size.width = frame.size.width * curProgress;
  409. if (isnan(frame.size.width))
  410. {
  411. HLog(@"计算出错了");
  412. frame.size.width = 10.0;
  413. }
  414. // HLog(@"%f, %f, %f, %f,",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
  415. // HLog(@"%@",_progressSelectView);
  416. _progressSelectView.frame = frame;
  417. //_glayer.frame = frame;
  418. NSString * totalSizeStr = nil;
  419. NSInteger totalSize_k = totalSize / 1024;
  420. if(totalSize_k < 1024){
  421. totalSizeStr = [[NSString alloc] initWithFormat:@"%ldKB",totalSize_k];
  422. }
  423. else if( totalSize_k >= 1024 && totalSize_k < 1024*1024){
  424. totalSizeStr = [[NSString alloc] initWithFormat:@"%.1fMB",totalSize_k/1024.0];
  425. }
  426. else{
  427. totalSizeStr = [[NSString alloc] initWithFormat:@"%.2fG",totalSize_k/1024.0/1024.0];
  428. }
  429. NSString * didUploadStr = nil;
  430. NSInteger didUploadSize_k = curShareFileDataModel.currentSize / 1024;
  431. if(didUploadSize_k < 1024){
  432. didUploadStr = [[NSString alloc] initWithFormat:@"%ldKB",didUploadSize_k];
  433. }
  434. else if( didUploadSize_k >= 1024 && didUploadSize_k < 1024*1024){
  435. didUploadStr = [[NSString alloc] initWithFormat:@"%.1fMB",didUploadSize_k/1024.0];
  436. }
  437. else{
  438. didUploadStr = [[NSString alloc] initWithFormat:@"%.2fG",didUploadSize_k/1024.0/1024.0];
  439. }
  440. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateBegin){
  441. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_wait"] forState:UIControlStateNormal];
  442. }
  443. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  444. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  445. _fileSpeedlabel.hidden = NO;
  446. //
  447. _fileSpeedlabel.text = @"";
  448. if(curShareFileDataModel.preTimeInterval <= 0
  449. || curShareFileDataModel.currentSize == curShareFileDataModel.preDownloadBytes){
  450. //_fileSpeedlabel.text = @"--";
  451. }
  452. else{
  453. NSTimeInterval timers = curShareFileDataModel.curTimeInterval - curShareFileDataModel.preTimeInterval;
  454. if(timers > 0){
  455. //long speed= 1*1024*1024 / timers;
  456. long speed= (curShareFileDataModel.currentSize - curShareFileDataModel.preDownloadBytes) / timers;
  457. NSString * speedStr = nil;
  458. NSInteger speed_k = speed / (1024);
  459. if(speed_k < 1024){
  460. speedStr = [[NSString alloc] initWithFormat:@"%ldKB/s",speed_k];
  461. }
  462. else {
  463. speedStr = [[NSString alloc] initWithFormat:@"%.1fMB/s",speed_k/1024.0];
  464. }
  465. _fileSpeedlabel.text = speedStr;
  466. }
  467. }
  468. curShareFileDataModel.preTimeInterval = curShareFileDataModel.curTimeInterval;
  469. curShareFileDataModel.preDownloadBytes = curShareFileDataModel.currentSize;
  470. }
  471. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  472. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  473. }
  474. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  475. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_reStart"] forState:UIControlStateNormal];
  476. _progressBgView.hidden = YES;
  477. }
  478. else{
  479. _upLoadStateButton.hidden = YES;
  480. _progressBgView.hidden = YES;
  481. }
  482. if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone){
  483. NSString*tipStr = NSLocalizedString(@"File_download_Path_other_done",nil);
  484. if([iTools canSaveFileToAlbumByPhoto:YES withName:curShareFileDataModel.fileName]
  485. ||[iTools canSaveFileToAlbumByPhoto:NO withName:curShareFileDataModel.fileName]){
  486. tipStr = NSLocalizedString(@"File_download_Path_default_done",nil);
  487. }
  488. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@ %@",totalSizeStr,tipStr];
  489. }
  490. else if(curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail){
  491. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@",totalSizeStr];
  492. }
  493. else{
  494. _fileProgresslabel.text = [[NSString alloc] initWithFormat:@"%@/%@",didUploadStr,totalSizeStr];
  495. }
  496. }
  497. -(void)longPressClick:(UILongPressGestureRecognizer *)press{
  498. //state属性是所有手势父类提供的方法,用于记录手势的状态
  499. if(press.state == UIGestureRecognizerStateBegan){
  500. //NSLog(@"长按手势开始响应!");
  501. if(_didLongPressClick){
  502. _didLongPressClick();
  503. }
  504. }else if (press.state == UIGestureRecognizerStateChanged){
  505. //NSLog(@"长按手势状态发生改变!");
  506. }else{
  507. //NSLog(@"长按手势结束!");
  508. }
  509. }
  510. - (void)didClickSelectButFun:(UIButton*)but
  511. {
  512. but.selected = !but.selected;
  513. if(_didClckSelectBut){
  514. _didClckSelectBut(but.selected);
  515. }
  516. }
  517. - (void)tapClick:(UIButton*)but{
  518. if(_didTapPressClick){
  519. _didTapPressClick();
  520. }
  521. if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDownloadloading){
  522. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_start"] forState:UIControlStateNormal];
  523. }
  524. else if(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateSuspend){
  525. [_upLoadStateButton setImage:[UIImage imageNamed:@"uploadFile_suspend"] forState:UIControlStateNormal];
  526. }
  527. but.userInteractionEnabled = NO;
  528. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  529. but.userInteractionEnabled = YES;
  530. });
  531. }
  532. - (void)downloadTaskExeIng:(NSNotification *)notification
  533. {
  534. SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  535. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  536. //HLog(@"%@\n %@",urlString,_curShareFileDataModel.fileUrl);
  537. if (![urlString isEqualToString:_curShareFileDataModel.fileUrl]
  538. ||model.totalSize == model.currentSize) { // 不是这一个任务
  539. return;
  540. }
  541. if(_curShareFileDataModel
  542. &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  543. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  544. ){
  545. return;
  546. }
  547. _curShareFileDataModel.downloadBoxStateType = downloadBoxStateDownloadloading;
  548. _curShareFileDataModel.currentSize = model.currentSize;
  549. _curShareFileDataModel.curTimeInterval = [[NSDate date] timeIntervalSince1970];
  550. mainBlock(^{
  551. [self updateDataDownloadingBy:self->_curShareFileDataModel];
  552. });
  553. HLog(@"下载中");
  554. }
  555. - (void)downloadTaskExeSupend:(NSNotification *)notification
  556. {
  557. SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  558. NSString * urlString = [model.url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  559. if (![urlString isEqualToString:_curShareFileDataModel.fileUrl]) { // 不是这一个任务
  560. return;
  561. }
  562. HLog(@"%@",_curShareFileDataModel);
  563. if(_curShareFileDataModel
  564. &&(_curShareFileDataModel.downloadBoxStateType == downloadBoxStateDone
  565. ||_curShareFileDataModel.downloadBoxStateType == downloadBoxStateFail)
  566. ){
  567. return;
  568. }
  569. _curShareFileDataModel.downloadBoxStateType = downloadBoxStateSuspend;
  570. mainBlock(^{
  571. [self updateDataDownloadingBy:self->_curShareFileDataModel];
  572. });
  573. HLog(@"暂停/下载等待中");
  574. }
  575. //- (void)downloadTaskExeEnd:(NSNotification *)notification
  576. //{
  577. // SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  578. //
  579. // if (![model.url isEqualToString:_curShareFileDataModel.fileUrl]) { // 不是这一个任务
  580. // return;
  581. // }
  582. //
  583. // _curShareFileDataModel.downloadBoxStateType = downloadBoxStateDone;
  584. // mainBlock(^{
  585. // [self updateDataDownloadingBy:self->_curShareFileDataModel];
  586. // });
  587. // HLog(@"下载成功");
  588. //}
  589. //- (void)downloadTaskExeError:(NSNotification *)notification
  590. //{
  591. // SGDownloadOperation *model = notification.userInfo.allValues.firstObject;
  592. //
  593. // if (![model.url isEqualToString:_curShareFileDataModel.fileUrl]) { // 不是这一个任务
  594. // return;
  595. // }
  596. //
  597. // _curShareFileDataModel.downloadBoxStateType = downloadBoxStateFail;
  598. // mainBlock(^{
  599. // [self updateDataDownloadingBy:self->_curShareFileDataModel];
  600. // });
  601. // HLog(@"下载失败");
  602. //}
  603. @end