JXFileDetailViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // JXFileDetailViewController.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/7/7.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXFileDetailViewController.h"
  9. #import "JXShareFileObject.h"
  10. #import "MCDownloader.h"
  11. #import "webpageVC.h"
  12. #import "UIImageView+FileType.h"
  13. @interface JXFileDetailViewController ()<UITextViewDelegate>
  14. @property (strong, nonatomic) UIImageView * typeView;
  15. @property (strong, nonatomic) UILabel * fileTitleLabel;
  16. @property (strong, nonatomic) UITextView * textView;
  17. @property (strong, nonatomic) UIButton * downloadBtn;
  18. @property (strong, nonatomic) UIView * downloadingView;
  19. @property (strong, nonatomic) UILabel * receivedLabel;
  20. @property (strong, nonatomic) UIProgressView * progressView;
  21. @property (strong, nonatomic) UIButton * stopButton;
  22. @property (assign, atomic) BOOL dalay;
  23. @end
  24. @implementation JXFileDetailViewController
  25. -(instancetype)init{
  26. self = [super init];
  27. if (self) {
  28. self.heightHeader = JX_SCREEN_TOP;
  29. self.heightFooter = 0;
  30. self.isGotoBack = YES;
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. self.title = _shareFile.fileName;
  37. [self createHeadAndFoot];
  38. [self customView];
  39. [self setViewDataWith:0 expectedSize:0 speed:0];
  40. }
  41. -(void)viewWillAppear:(BOOL)animated{
  42. [super viewWillAppear:animated];
  43. MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:_shareFile.url];
  44. if (receipt.state == MCDownloadStateDownloading) {
  45. [self downloadBtnAction];
  46. }
  47. }
  48. -(void)customView{
  49. if(!_typeView){
  50. _typeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 60, 55)];
  51. _typeView.center = CGPointMake(self.tableBody.center.x, _typeView.center.y);
  52. _typeView.layer.cornerRadius = 3;
  53. _typeView.layer.masksToBounds = YES;
  54. _typeView.backgroundColor = [UIColor redColor];
  55. [_typeView setFileType:[_shareFile.type integerValue]];
  56. [self.tableBody addSubview:_typeView];
  57. }
  58. if(!_fileTitleLabel){
  59. _fileTitleLabel = [UIFactory createLabelWith:CGRectZero text:_shareFile.fileName font:g_factory.font16 textColor:[UIColor blackColor] backgroundColor:[UIColor clearColor]];
  60. _fileTitleLabel.frame = CGRectMake(15, CGRectGetMaxY(_typeView.frame)+15, JX_SCREEN_WIDTH - 30, 25);
  61. _fileTitleLabel.center = CGPointMake(self.tableBody.center.x, _fileTitleLabel.center.y);
  62. _fileTitleLabel.textAlignment = NSTextAlignmentCenter;
  63. [self.tableBody addSubview:_fileTitleLabel];
  64. }
  65. if (!_textView) {
  66. _textView = [[UITextView alloc] init];
  67. _textView.frame = CGRectMake(0, CGRectGetMaxY(_fileTitleLabel.frame), 150, 25);
  68. _textView.center = CGPointMake(self.tableBody.center.x, _textView.center.y);
  69. _textView.delegate = self;
  70. [self.tableBody addSubview:_textView];
  71. [self protocolIsSelect];
  72. }
  73. if(!_downloadBtn){
  74. _downloadBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  75. _downloadBtn.backgroundColor = THEMECOLOR;
  76. _downloadBtn.layer.masksToBounds = YES;
  77. _downloadBtn.layer.cornerRadius = 7.f;
  78. _downloadBtn.titleLabel.font = SYSFONT(16);
  79. [_downloadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  80. [_downloadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
  81. _downloadBtn.frame = CGRectMake(0, CGRectGetMaxY(_fileTitleLabel.frame) +45, 200, 30);
  82. _downloadBtn.center = CGPointMake(self.tableBody.center.x, _downloadBtn.center.y);
  83. [self.tableBody addSubview:_downloadBtn];
  84. [_downloadBtn addTarget:self action:@selector(downloadBtnAction) forControlEvents:UIControlEventTouchUpInside];
  85. }
  86. if (!_downloadingView) {
  87. _downloadingView = [[UIView alloc] init];
  88. _downloadingView.backgroundColor = [UIColor clearColor];
  89. _downloadingView.frame = CGRectMake(30, CGRectGetMaxY(_fileTitleLabel.frame) +85, self_width-30*2, 40);
  90. _downloadingView.center = CGPointMake(self.tableBody.center.x, _downloadingView.center.y);
  91. [self.tableBody addSubview:_downloadingView];
  92. if(!_receivedLabel){
  93. _receivedLabel = [[UILabel alloc] init];
  94. _receivedLabel.font = g_factory.font13;
  95. _receivedLabel.textAlignment = NSTextAlignmentCenter;
  96. _receivedLabel.frame = CGRectMake(0, 0, CGRectGetWidth(_downloadingView.frame), 17);
  97. [_downloadingView addSubview:_receivedLabel];
  98. }
  99. if (!_progressView) {
  100. _progressView = [[UIProgressView alloc] init];
  101. _progressView.frame = CGRectMake(0, 20, CGRectGetWidth(_downloadingView.frame)-30, 5);
  102. _progressView.progress = 0;
  103. _progressView.progressTintColor = [UIColor greenColor];
  104. _progressView.trackTintColor = [UIColor lightGrayColor];
  105. [_downloadingView addSubview:_progressView];
  106. // progressImage
  107. // trackImage
  108. }
  109. if (!_stopButton) {
  110. _stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
  111. _stopButton.frame = CGRectMake(CGRectGetMaxX(_progressView.frame)+3, 0, 25, 25);
  112. _stopButton.center = CGPointMake(_stopButton.center.x, _progressView.center.y);
  113. [_stopButton setBackgroundImage:[UIImage imageNamed:@"stopDownload"] forState:UIControlStateNormal];
  114. [_downloadingView addSubview:_stopButton];
  115. [_stopButton addTarget:self action:@selector(stopDownloadFile) forControlEvents:UIControlEventTouchUpInside];
  116. }
  117. }
  118. }
  119. -(void)setViewDataWith:(NSInteger)receivedSize expectedSize:(NSInteger)expectedSize speed:(NSInteger)speed{
  120. MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:_shareFile.url];
  121. if (receipt.state == MCDownloadStateNone) {
  122. _downloadBtn.hidden = NO;
  123. _downloadingView.hidden = YES;
  124. NSString * downStr = nil;
  125. if ([_shareFile.size longValue]/1000.0/1000 >= 1) {
  126. downStr = [NSString stringWithFormat:@"%@(%.02fMB)",Localized(@"JX_Download"),[_shareFile.size longValue]/1000.0/1000];
  127. }else{
  128. downStr = [NSString stringWithFormat:@"%@(%.02fKB)",Localized(@"JX_Download"),[_shareFile.size longValue]/1000.0];
  129. }
  130. [_downloadBtn setTitle:downStr forState:UIControlStateNormal];
  131. [_downloadBtn setTitle:downStr forState:UIControlStateHighlighted];
  132. }else if (receipt.state == MCDownloadStateWillResume){
  133. _downloadBtn.hidden = YES;
  134. _downloadingView.hidden = NO;
  135. NSString * progressStr = nil;
  136. if ([_shareFile.size longValue]/1024.0/1024 >= 1) {
  137. progressStr = [NSString stringWithFormat:@"%@...(%.02fMB / %.02fMB)",Localized(@"JX_Downloading"),receipt.progress.completedUnitCount/1024.0/1024,receipt.progress.totalUnitCount/1024.0/1024];
  138. }else{
  139. progressStr = [NSString stringWithFormat:@"%@...(%.02fKB / %.02fKB)",Localized(@"JX_Downloading"),receipt.progress.completedUnitCount/1024.0,receipt.progress.totalUnitCount/1024.0];
  140. }
  141. // float progress = receivedSize / [_shareFile.size doubleValue];
  142. // [_progressView setProgress:progress animated:YES];
  143. [_progressView setProgress:receipt.progress.fractionCompleted animated:YES];
  144. _receivedLabel.text = progressStr;
  145. }else if (receipt.state == MCDownloadStateDownloading){
  146. _downloadBtn.hidden = YES;
  147. _downloadingView.hidden = NO;
  148. NSString * progressStr = nil;
  149. if ([_shareFile.size longValue]/1024.0/1024 >= 1) {
  150. progressStr = [NSString stringWithFormat:@"%@...(%.02fMB / %.02fMB)",Localized(@"JX_Downloading"),receipt.progress.completedUnitCount/1024.0/1024,receipt.progress.totalUnitCount/1024.0/1024];
  151. }else{
  152. progressStr = [NSString stringWithFormat:@"%@...(%.02fKB / %.02fKB)",Localized(@"JX_Downloading"),receipt.progress.completedUnitCount/1024.0,receipt.progress.totalUnitCount/1024.0];
  153. }
  154. // float progress = receivedSize / [_shareFile.size doubleValue];
  155. [_progressView setProgress:receipt.progress.fractionCompleted animated:YES];
  156. _receivedLabel.text = progressStr;
  157. }else if (receipt.state == MCDownloadStateCompleted) {
  158. _downloadBtn.hidden = NO;
  159. _downloadingView.hidden = YES;
  160. [_downloadBtn setTitle:Localized(@"JX_Open") forState:UIControlStateNormal];
  161. [_downloadBtn setTitle:Localized(@"JX_Open") forState:UIControlStateHighlighted];
  162. [_downloadBtn removeTarget:self action:@selector(downloadBtnAction) forControlEvents:UIControlEventTouchUpInside];
  163. [_downloadBtn addTarget:self action:@selector(openFileAction) forControlEvents:UIControlEventTouchUpInside];
  164. }else if (receipt.state == MCDownloadStateFailed) {
  165. _downloadBtn.hidden = NO;
  166. _downloadingView.hidden = YES;
  167. [_downloadBtn setTitle:Localized(@"JX_ReDownload") forState:UIControlStateNormal];
  168. [_downloadBtn setTitle:Localized(@"JX_ReDownload") forState:UIControlStateHighlighted];
  169. [_downloadBtn removeTarget:self action:@selector(downloadBtnAction) forControlEvents:UIControlEventTouchUpInside];
  170. [_downloadBtn addTarget:self action:@selector(retryDownload) forControlEvents:UIControlEventTouchUpInside];
  171. }else if (receipt.state == MCDownloadStateSuspened) {
  172. }
  173. }
  174. -(void)downloadBtnAction{
  175. [[MCDownloader sharedDownloader] downloadDataWithURL:[NSURL URLWithString:_shareFile.url] progress:^(NSInteger receivedSize, NSInteger expectedSize, NSInteger speed, NSURL * _Nullable targetURL) {
  176. dispatch_async(dispatch_get_main_queue(), ^{
  177. [self setViewDataWith:receivedSize expectedSize:expectedSize speed:speed];
  178. });
  179. } completed:^(MCDownloadReceipt *receipt, NSError * _Nullable error, BOOL finished) {
  180. dispatch_async(dispatch_get_main_queue(), ^{
  181. [self setViewDataWith:0 expectedSize:0 speed:0];
  182. if (!error && finished) {
  183. [self openFileAction];
  184. NSData *data = [NSData dataWithContentsOfFile:receipt.filePath];
  185. NSString *filePath = [NSString stringWithFormat:@"%@%@",myTempFilePath,[receipt.filePath lastPathComponent]];
  186. NSFileManager *manager = [NSFileManager defaultManager];
  187. BOOL isExist = [manager fileExistsAtPath:filePath];
  188. if (!isExist) {
  189. BOOL isSave = [data writeToFile:filePath atomically:YES];
  190. NSLog(@"isSave");
  191. }
  192. }else{
  193. [g_App showAlert:error.description];
  194. }
  195. });
  196. }];
  197. }
  198. -(void)openFileAction{
  199. if (_dalay){
  200. return;
  201. }
  202. _dalay = YES;
  203. [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.4];
  204. MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:_shareFile.url];
  205. webpageVC * webVC = [webpageVC alloc];
  206. webVC.url = receipt.filePath;
  207. webVC.shareUrl = _shareFile.url;
  208. webVC.titleString = _shareFile.fileName;
  209. webVC.isSend = YES;
  210. webVC = [webVC init];
  211. [g_navigation.navigationView addSubview:webVC.view];
  212. // [g_navigation pushViewController:webVC animated:YES];
  213. }
  214. -(void)stopDownloadFile{
  215. MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:_shareFile.url];
  216. [[MCDownloader sharedDownloader] cancel:receipt completed:^{
  217. [self setViewDataWith:0 expectedSize:0 speed:0];
  218. }];
  219. }
  220. -(void)delayClick{
  221. if (_dalay) {
  222. _dalay = NO;
  223. }
  224. }
  225. -(void)retryDownload{
  226. MCDownloadReceipt *receipt = [[MCDownloader sharedDownloader] downloadReceiptForURLString:_shareFile.url];
  227. [[MCDownloader sharedDownloader] remove:receipt completed:^{
  228. [self downloadBtnAction];
  229. }];
  230. }
  231. - (void)didReceiveMemoryWarning {
  232. [super didReceiveMemoryWarning];
  233. // Dispose of any resources that can be recreated.
  234. }
  235. - (void)protocolIsSelect {
  236. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:Localized(@"JXFile_fileBeforeOnline")];
  237. [attributedString addAttribute:NSLinkAttributeName
  238. value:_shareFile.url
  239. range:[[attributedString string] rangeOfString:Localized(@"JXFile_online")]];
  240. [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:[[attributedString string] rangeOfString:Localized(@"JXFile_fileBefore")]];
  241. [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, attributedString.length)];
  242. _textView.attributedText = attributedString;
  243. _textView.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
  244. NSUnderlineColorAttributeName: [UIColor lightGrayColor],
  245. NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
  246. // _textView.delegate = self;
  247. _textView.editable = NO; //必须禁止输入,否则点击将弹出输入键盘
  248. _textView.scrollEnabled = NO;
  249. }
  250. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
  251. if ([[URL absoluteString] isEqualToString:_shareFile.url]) {
  252. webpageVC * webVC = [webpageVC alloc];
  253. webVC.url = _shareFile.url;
  254. webVC.isSend = YES;
  255. webVC.titleString = _shareFile.fileName;
  256. webVC = [webVC init];
  257. [g_navigation.navigationView addSubview:webVC.view];
  258. // [g_navigation pushViewController:webVC animated:YES];
  259. return NO;
  260. }
  261. return YES;
  262. }
  263. @end