JXShareFileTableViewCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // JXShareFileTableViewCell.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/7/6.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXShareFileTableViewCell.h"
  9. #import "JXShareFileObject.h"
  10. #import "MCDownloader.h"
  11. #import "UIImageView+FileType.h"
  12. @interface JXShareFileTableViewCell ()
  13. @property (nonatomic, strong) MCDownloadReceipt * receipt;
  14. @end
  15. @implementation JXShareFileTableViewCell
  16. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  17. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  18. if (self) {
  19. self.selectionStyle = UITableViewCellSelectionStyleNone;
  20. [self customView];
  21. }
  22. return self;
  23. }
  24. -(void)prepareForReuse{
  25. [super prepareForReuse];
  26. // [_receipt.progress removeObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) context:nil];
  27. // [_receipt.progress removeObserver:self forKeyPath:NSStringFromSelector(@selector(state)) context:nil];
  28. }
  29. -(void)customView{
  30. if(!_typeView){
  31. _typeView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 45, 45)];
  32. _typeView.layer.cornerRadius = 3;
  33. _typeView.layer.masksToBounds = YES;
  34. // _typeView.backgroundColor = [UIColor redColor];
  35. [self.contentView addSubview:_typeView];
  36. }
  37. if(!_fileTitleLabel){
  38. _fileTitleLabel = [UIFactory createLabelWith:CGRectZero text:@"--.--" font:g_factory.font15 textColor:[UIColor blackColor] backgroundColor:[UIColor clearColor]];
  39. _fileTitleLabel.frame = CGRectMake(CGRectGetMaxX(_typeView.frame) +5, CGRectGetMinY(_typeView.frame), 150, 25);
  40. _fileTitleLabel.textAlignment = NSTextAlignmentLeft;
  41. [self.contentView addSubview:_fileTitleLabel];
  42. }
  43. if(!_sizeLabel){
  44. _sizeLabel = [UIFactory createLabelWith:CGRectZero text:@"--kb" font:g_factory.font10 textColor:[UIColor lightGrayColor] backgroundColor:[UIColor clearColor]];
  45. _sizeLabel.frame = CGRectMake(CGRectGetMinX(_fileTitleLabel.frame), CGRectGetMaxY(_typeView.frame)-15, 50, 15);
  46. _sizeLabel.textAlignment = NSTextAlignmentLeft;
  47. [self.contentView addSubview:_sizeLabel];
  48. }
  49. if(!_fromLabel){
  50. _fromLabel = [UIFactory createLabelWith:CGRectZero text:Localized(@"JXFile_from") font:g_factory.font10 textColor:[UIColor lightGrayColor] backgroundColor:[UIColor clearColor]];
  51. _fromLabel.frame = CGRectMake(CGRectGetMaxX(_sizeLabel.frame)+3, CGRectGetMinY(_sizeLabel.frame), 24, 15);
  52. [self.contentView addSubview:_fromLabel];
  53. }
  54. if(!_fromUserLabel){
  55. _fromUserLabel = [UIFactory createLabelWith:CGRectZero text:@"--" font:g_factory.font11 textColor:[UIColor blueColor] backgroundColor:[UIColor clearColor]];
  56. _fromUserLabel.frame = CGRectMake(CGRectGetMaxX(_fromLabel.frame), CGRectGetMinY(_sizeLabel.frame), 55, 15);
  57. [self.contentView addSubview:_fromUserLabel];
  58. }
  59. if(!_timeLabel){
  60. _timeLabel = [UIFactory createLabelWith:CGRectZero text:@"00-00" font:g_factory.font9 textColor:[UIColor lightGrayColor] backgroundColor:[UIColor clearColor]];
  61. _timeLabel.frame = CGRectMake(CGRectGetWidth(self.contentView.frame)-20-35, CGRectGetMinY(_typeView.frame), 35, 15);
  62. _timeLabel.textAlignment = NSTextAlignmentRight;
  63. [self.contentView addSubview:_timeLabel];
  64. }
  65. if(!_didDownView){
  66. _didDownView = [[UIImageView alloc] init];
  67. _didDownView.image = [UIImage imageNamed:@"finishDownload"];
  68. // _didDownView.backgroundColor = [UIColor greenColor];
  69. _didDownView.frame = CGRectMake(CGRectGetMaxX(_typeView.frame)-15, CGRectGetMaxY(_typeView.frame)-15, 15, 15);
  70. [self.contentView addSubview:_didDownView];
  71. }
  72. if(!_progressView){
  73. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
  74. _progressView.frame = CGRectMake(CGRectGetMinX(_fileTitleLabel.frame), CGRectGetMaxY(_typeView.frame), CGRectGetWidth(_fileTitleLabel.frame), 2);
  75. _progressView.progressTintColor = [UIColor greenColor];
  76. _progressView.trackTintColor = [UIColor lightGrayColor];
  77. _progressView.progress = 0.0;
  78. [self.contentView addSubview:_progressView];
  79. }
  80. if(!_downloadStateBtn){
  81. _downloadStateBtn = [[UIButton alloc] init];
  82. _downloadStateBtn.frame = CGRectMake(0, 0, 21, 21);
  83. _downloadStateBtn.center = CGPointMake(_timeLabel.center.x, _typeView.center.y);
  84. [_downloadStateBtn setBackgroundImage:[UIImage imageNamed:@"pauseDownload"] forState:UIControlStateNormal];
  85. [_downloadStateBtn setBackgroundImage:[UIImage imageNamed:@"starDownload"] forState:UIControlStateSelected];
  86. // [_downloadStateBtn setBackgroundImage:[UIImage imageNamed:@"errorDownload"] forState:UIControlStateDisabled];
  87. [self.contentView addSubview:_downloadStateBtn];
  88. [_downloadStateBtn addTarget:self action:@selector(downStateAction) forControlEvents:UIControlEventTouchUpInside];
  89. }
  90. }
  91. -(void)setShareFileListCellWith:(JXShareFileObject *)shareFileObjcet indexPath:(NSIndexPath *)indexPath{
  92. self.shareFile = shareFileObjcet;
  93. [_typeView setFileType:[shareFileObjcet.type integerValue]];
  94. _fileTitleLabel.text = shareFileObjcet.fileName;
  95. _fileTitleLabel.frame = CGRectMake(CGRectGetMaxX(_typeView.frame) +5, CGRectGetMinY(_typeView.frame), CGRectGetWidth(self.contentView.frame)-20-35-20-CGRectGetMaxX(_typeView.frame) -5, 25);
  96. NSString * downStr = nil;
  97. if ([_shareFile.size longValue]/1024.0/1024 >= 1) {
  98. downStr = [NSString stringWithFormat:@"%.02fMB",[_shareFile.size longValue]/1024.0/1024];
  99. }else{
  100. downStr = [NSString stringWithFormat:@"%.02fKB",[_shareFile.size longValue]/1024.0];
  101. }
  102. _sizeLabel.text = downStr;
  103. _fromLabel.text = Localized(@"JXFile_from");
  104. _fromUserLabel.text = shareFileObjcet.createUserName;
  105. NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
  106. [formatter setDateFormat:@"MM-dd"];
  107. _timeLabel.text = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[shareFileObjcet.time doubleValue]]];
  108. _timeLabel.frame = CGRectMake(CGRectGetWidth(self.contentView.frame)-20-35, CGRectGetMinY(_typeView.frame), 35, 15);
  109. _didDownView.hidden = YES;
  110. _progressView.hidden = YES;
  111. _downloadStateBtn.hidden = YES;
  112. }
  113. - (void)awakeFromNib {
  114. [super awakeFromNib];
  115. // Initialization code
  116. }
  117. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  118. [super setSelected:selected animated:animated];
  119. // Configure the view for the selected state
  120. }
  121. -(void)setShareFile:(JXShareFileObject *)shareFile{
  122. _shareFile = shareFile;
  123. _receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:shareFile.url];
  124. self.progressView.progress = 0;
  125. [self.progressView setProgress:_receipt.progress.fractionCompleted animated:YES];
  126. // [_receipt.progress addObserver:self forKeyPath:NSStringFromSelector(@selector(fractionCompleted)) options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
  127. // [_receipt addObserver:self forKeyPath:NSStringFromSelector(@selector(state)) options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
  128. if (_receipt.state == MCDownloadStateDownloading || _receipt.state == MCDownloadStateWillResume) {
  129. //正在下载
  130. _didDownView.hidden = YES;
  131. _progressView.hidden = NO;
  132. _downloadStateBtn.hidden = NO;
  133. _downloadStateBtn.selected = NO;
  134. }else if (_receipt.state == MCDownloadStateCompleted) {
  135. //下载完成
  136. _didDownView.hidden = NO;
  137. _progressView.hidden = YES;
  138. _downloadStateBtn.hidden = YES;
  139. }else{
  140. //没有开始下载
  141. }
  142. __weak typeof(_receipt) weakReeceeipt = _receipt;
  143. __weak typeof(self) weakSelf = self;
  144. _receipt.downloaderProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, NSInteger speed, NSURL * _Nullable targetURL) {
  145. // __strong typeof(weakReeceeipt) strongReceipt = weakReeceeipt;
  146. if ([targetURL.absoluteString isEqualToString:weakSelf.shareFile.url]) {
  147. // [weakSelf.downloadStateBtn setTitle:@"pause" forState:UIControlStateNormal];
  148. // weakSelf.progressView.progress = (receivedSize/1024.0/1024) / (expectedSize/1024.0/1024);
  149. weakSelf.didDownView.hidden = YES;
  150. weakSelf.progressView.hidden = NO;
  151. weakSelf.downloadStateBtn.hidden = NO;
  152. [weakSelf.progressView setProgress:weakReeceeipt.progress.fractionCompleted animated:YES];
  153. }
  154. };
  155. _receipt.downloaderCompletedBlock = ^(MCDownloadReceipt * _Nullable receipt, NSError * _Nullable error, BOOL finished) {
  156. if (error) {
  157. }else{
  158. weakSelf.didDownView.hidden = NO;
  159. weakSelf.progressView.hidden = YES;
  160. weakSelf.downloadStateBtn.hidden = YES;
  161. }
  162. };
  163. }
  164. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  165. // if ([keyPath isEqualToString:NSStringFromSelector(@selector(fractionCompleted))]) {
  166. // [_progressView setProgress:[change[@"new"] doubleValue] animated:YES];
  167. // }else if ([keyPath isEqualToString:NSStringFromSelector(@selector(state))]){
  168. // if ([change[@"new"] integerValue] == MCDownloadStateDownloading) {
  169. // _didDownView.hidden = YES;
  170. // _progressView.hidden = NO;
  171. // _downloadStateBtn.hidden = NO;
  172. // }
  173. // if ([change[@"new"] integerValue] == MCDownloadStateCompleted) {
  174. // _didDownView.hidden = NO;
  175. // _progressView.hidden = YES;
  176. // _downloadStateBtn.hidden = YES;
  177. // }
  178. // }
  179. }
  180. -(void)downloadBtnAction{
  181. [[MCDownloader sharedDownloader] downloadDataWithURL:[NSURL URLWithString:_shareFile.url] progress:^(NSInteger receivedSize, NSInteger expectedSize, NSInteger speed, NSURL * _Nullable targetURL) {
  182. // dispatch_async(dispatch_get_main_queue(), ^{
  183. // [self setViewDataWith:receivedSize expectedSize:expectedSize speed:speed];
  184. //
  185. // });
  186. } completed:^(MCDownloadReceipt *receipt, NSError * _Nullable error, BOOL finished) {
  187. // dispatch_async(dispatch_get_main_queue(), ^{
  188. // [self setViewDataWith:0 expectedSize:0 speed:0];
  189. // if (!error && finished) {
  190. // [self openFileAction];
  191. // }else{
  192. // [g_App showAlert:error.description];
  193. // }
  194. //
  195. // });
  196. }];
  197. }
  198. -(void)stopDownloadFile{
  199. // MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:_shareFile.url];
  200. [[MCDownloader sharedDownloader] cancel:_receipt completed:^{
  201. }];
  202. }
  203. -(void)retryDownload{
  204. MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:_shareFile.url];
  205. [[MCDownloader sharedDownloader] remove:receipt completed:^{
  206. [self downloadBtnAction];
  207. }];
  208. }
  209. -(void)downStateAction{
  210. if (_receipt.state == MCDownloadStateDownloading || _receipt.state == MCDownloadStateWillResume) {
  211. //正在下载
  212. [self stopDownloadFile];
  213. _downloadStateBtn.selected = YES;
  214. }else if (_receipt.state == MCDownloadStateCompleted) {
  215. //下载完成
  216. }else if (_receipt.state == MCDownloadStateFailed) {
  217. [_downloadStateBtn setBackgroundImage:[UIImage imageNamed:@"errorDownload"] forState:UIControlStateNormal];
  218. [self retryDownload];
  219. }else if (_receipt.state == MCDownloadStateNone) {
  220. [self downloadBtnAction];
  221. _downloadStateBtn.selected = NO;
  222. [_downloadStateBtn setBackgroundImage:[UIImage imageNamed:@"pauseDownload"] forState:UIControlStateNormal];
  223. }
  224. }
  225. @end