JhtDownloadView.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // JhtDownloadView.m
  3. // JhtDocViewerDemo
  4. //
  5. // GitHub主页: https://github.com/jinht
  6. // CSDN博客: http://blog.csdn.net/anticipate91
  7. //
  8. // Created by Jht on 2017/1/5.
  9. // Copyright © 2017年 JhtDocViewer. All rights reserved.
  10. //
  11. #import "JhtDownloadView.h"
  12. #import "JhtFileModel.h"
  13. #import "JhtDefaultManager.h"
  14. #import "JhtDownloadRequest.h"
  15. #import "JhtDocViewer_Define.h"
  16. #import "JhtNetworkCheckTools.h"
  17. @interface JhtDownloadView() {
  18. // 点击按钮回调的block 0: 重新加载,1: 点击关闭
  19. clickBlock _block;
  20. }
  21. /** 下载文件的model */
  22. @property (nonatomic, strong) JhtFileModel *currentFileModel;
  23. @end
  24. @implementation JhtDownloadView
  25. #pragma mark - Public Method
  26. - (id)initWithFrame:(CGRect)frame downloadingHint:(NSString *)downloadingHint lostNetHint:(NSString *)lostNetHint downloadProgressTintColor:(UIColor *)downloadProgressTintColor fileModel:(JhtFileModel *)model {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. self.downloadProgressTintColor = downloadProgressTintColor;
  30. self.downloadingHint = downloadingHint;
  31. self.lostNetHint = lostNetHint;
  32. self.currentFileModel = model;
  33. // 文件类型图片
  34. [self addSubview:self.iconFileImageView];
  35. // 文件名称
  36. [self addSubview:self.iconFileDescribeLabel];
  37. // 进度条
  38. [self addSubview:self.fileProgressView];
  39. // 关闭按钮
  40. [self addSubview:self.closeBtn];
  41. // 下载按钮
  42. [self addSubview:self.downloadBtn];
  43. // 下载进度文案label
  44. [self addSubview:self.downloadingStateLabel];
  45. // 初始化相关参数
  46. [self ldvInitParam];
  47. }
  48. return self;
  49. }
  50. - (void)clickBtnBlock:(clickBlock)block {
  51. _block = block;
  52. }
  53. #pragma mark - Private Method
  54. /** 初始化相关参数 */
  55. - (void)ldvInitParam {
  56. NSString *fileIconName = @"";
  57. if (self.currentFileModel.viewFileType == Type_Docx) {
  58. fileIconName = @"word.png";
  59. } else if (self.currentFileModel.viewFileType == Type_Xlsx) {
  60. fileIconName = @"excel.png";
  61. } else if (self.currentFileModel.viewFileType == Type_Pptx) {
  62. fileIconName = @"ppt.png";
  63. } else if (self.currentFileModel.viewFileType == Type_Pdf) {
  64. fileIconName = @"pdf.png";
  65. } else if (self.currentFileModel.viewFileType == Type_Txt) {
  66. fileIconName = @"txt.png";
  67. } else {
  68. fileIconName = @"unknow.png";
  69. }
  70. // 文件类型图片
  71. self.iconFileImageView.frame = CGRectMake((CGRectGetWidth(self.frame) - 100) / 2.f, 65.f, 100.f, 100.f);
  72. NSString *fileTypeImagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"JhtDocViewer.bundle/images/%@", fileIconName]];
  73. self.iconFileImageView.image = [UIImage imageWithContentsOfFile:fileTypeImagePath];
  74. // 计算文件大小
  75. CGSize filesize = [self.currentFileModel.fileName boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.frame) - 90, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]} context:nil].size;
  76. CGFloat describeHeight = filesize.height;
  77. // 文件名称
  78. self.iconFileDescribeLabel.frame = CGRectMake(45, CGRectGetMaxY(_iconFileImageView.frame) + 25, CGRectGetWidth(self.frame) - 90, describeHeight);
  79. self.iconFileDescribeLabel.text = self.currentFileModel.fileName;
  80. // 下载条
  81. self.fileProgressView.frame = CGRectMake(45 / 2.0, CGRectGetMaxY(self.iconFileDescribeLabel.frame) + 29, CGRectGetWidth(self.frame) - (45 / 2.0 * 2 + 5 / 2.0 + 20), 10);
  82. CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 3.0f);
  83. // 更改进度条 高度
  84. _fileProgressView.transform = transform;
  85. // 关闭按钮
  86. self.closeBtn.frame = CGRectMake(CGRectGetMaxX(self.fileProgressView.frame), CGRectGetMaxY(self.iconFileDescribeLabel.frame) + 19, 20, 20);
  87. // 下载进度文案label
  88. self.downloadingStateLabel.text = self.downloadingHint;
  89. [self.downloadingStateLabel sizeToFit];
  90. self.downloadingStateLabel.frame = CGRectMake(0, CGRectGetMaxY(self.iconFileDescribeLabel.frame) + 29 + 19, CGRectGetWidth(self.frame), CGRectGetHeight(_downloadingStateLabel.frame));
  91. // 重试按钮
  92. self.downloadBtn.frame = CGRectMake((CGRectGetWidth(self.frame) - 110.f) / 2.f, CGRectGetMaxY(_downloadingStateLabel.frame) + 10, 110, 30);
  93. [self.downloadBtn setTitle:[NSString stringWithFormat:@"下载 %@", self.currentFileModel.fileSize] forState:UIControlStateNormal];
  94. }
  95. #pragma mark - Getter
  96. - (UIImageView *)iconFileImageView {
  97. if (!_iconFileImageView) {
  98. _iconFileImageView = [[UIImageView alloc] init];
  99. }
  100. return _iconFileImageView;
  101. }
  102. - (UILabel *)iconFileDescribeLabel {
  103. if (!_iconFileDescribeLabel) {
  104. _iconFileDescribeLabel = [[UILabel alloc] init];
  105. _iconFileDescribeLabel.textAlignment = NSTextAlignmentCenter;
  106. _iconFileDescribeLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  107. _iconFileDescribeLabel.font = [UIFont systemFontOfSize:16.f];
  108. _iconFileDescribeLabel.textColor = UIColorFromRGB(0x333333);
  109. _iconFileDescribeLabel.numberOfLines = 0;
  110. }
  111. return _iconFileDescribeLabel;
  112. }
  113. - (UILabel *)downloadingStateLabel {
  114. if (!_downloadingStateLabel) {
  115. _downloadingStateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  116. _downloadingStateLabel.text = self.downloadingHint;
  117. _downloadingStateLabel.font = [UIFont systemFontOfSize:14.f];
  118. _downloadingStateLabel.textColor = UIColorFromRGB(0x808080);
  119. _downloadingStateLabel.textAlignment = NSTextAlignmentCenter;
  120. }
  121. return _downloadingStateLabel;
  122. }
  123. - (UIColor *)downloadProgressTintColor {
  124. return _downloadProgressTintColor ? _downloadProgressTintColor : UIColorFromRGB(0x61CBF5);
  125. }
  126. - (UIProgressView *)fileProgressView {
  127. if (!_fileProgressView) {
  128. _fileProgressView = [[UIProgressView alloc] init];
  129. [_fileProgressView setProgressViewStyle:UIProgressViewStyleDefault];
  130. _fileProgressView.progressTintColor = self.downloadProgressTintColor;
  131. _fileProgressView.layer.masksToBounds = YES;
  132. _fileProgressView.layer.cornerRadius = 2.f;
  133. _fileProgressView.hidden = YES;
  134. }
  135. return _fileProgressView;
  136. }
  137. - (UIButton *)closeBtn {
  138. if (!_closeBtn) {
  139. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  140. NSString *closeImagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"JhtDocViewer.bundle/images/close"];
  141. UIImage *closeBtnImage = [UIImage imageWithContentsOfFile:closeImagePath];
  142. [_closeBtn setImage:closeBtnImage forState:UIControlStateNormal];
  143. [_closeBtn addTarget:self action:@selector(ldvTwoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  144. _closeBtn.hidden = YES;
  145. _closeBtn.tag = 201;
  146. }
  147. return _closeBtn;
  148. }
  149. - (UIButton *)downloadBtn {
  150. if (!_downloadBtn) {
  151. _downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  152. _downloadBtn.backgroundColor = UIColorFromRGB(0x61cbf5);
  153. [_downloadBtn setTitle:@"下载" forState:UIControlStateNormal];
  154. _downloadBtn.titleLabel.font = [UIFont systemFontOfSize:15.f];
  155. [_downloadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  156. _downloadBtn.layer.cornerRadius = 3.f;
  157. _downloadBtn.tag = 200;
  158. _downloadBtn.layer.masksToBounds = YES;
  159. [_downloadBtn addTarget:self action:@selector(ldvTwoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  160. // _downloadBtn.hidden = YES;
  161. }
  162. return _downloadBtn;
  163. }
  164. - (NSString *)downloadingHint {
  165. if ([JhtNetworkCheckTools currentNetWork_Status] == NetWorkStatus_None) {
  166. return self.lostNetHint;
  167. } else {
  168. NSDictionary *dic = [JhtDefaultManager getConfigDataWithType:JhtDefaultType_LoadDocViewParam];
  169. return _downloadingHint.length > 0 ? _downloadingHint : dic[@"downloadingHint"];
  170. }
  171. }
  172. - (NSString *)lostNetHint {
  173. NSDictionary *dic = [JhtDefaultManager getConfigDataWithType:JhtDefaultType_LoadDocViewParam];
  174. return _lostNetHint.length > 0 ? _lostNetHint : dic[@"lostNetHint"];
  175. }
  176. #pragma mark Getter Method
  177. /** 点击按钮触发 */
  178. - (void)ldvTwoBtnClick:(UIButton *)btn {
  179. _block(btn.tag - 200);
  180. }
  181. @end