RQUpdateView.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // RQUpdateView.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2022/5/19.
  6. //
  7. #import "RQUpdateView.h"
  8. @interface RQUpdateView ()
  9. @property (weak, nonatomic) IBOutlet UIImageView *updateHeadImageView;
  10. @property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
  11. @property (weak, nonatomic) IBOutlet UIStackView *headerTitleView;
  12. @property (weak, nonatomic) IBOutlet UILabel *headerTitleLabel;
  13. @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
  14. @property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;
  15. @property (weak, nonatomic) IBOutlet UIButton *downloadBtn;
  16. @property (weak, nonatomic) IBOutlet UIStackView *progressView;
  17. @property (weak, nonatomic) IBOutlet UILabel *progressLabel;
  18. @property (weak, nonatomic) IBOutlet UIProgressView *myProgress;
  19. @property (weak, nonatomic) IBOutlet UILabel *requestLabel;
  20. @property (weak, nonatomic) IBOutlet UIImageView *completeImageView;
  21. @property (weak, nonatomic) IBOutlet UIButton *closeBtn;
  22. @property (nonatomic, readwrite, assign) CGFloat progress;
  23. @end
  24. @implementation RQUpdateView
  25. + (instancetype)updateView {
  26. return [super rq_viewFromXib];
  27. }
  28. - (void)awakeFromNib {
  29. [super awakeFromNib];
  30. _progress = 0.f;
  31. _downloadBtn.layer.cornerRadius = RQ_FIT_HORIZONTAL(40.f) / 2.f;
  32. }
  33. - (void)setQuestionUpdateViewType:(RQQuestionUpdateViewType)questionUpdateViewType {
  34. switch (questionUpdateViewType) {
  35. case RQQuestionUpdateViewType_LibDownload: {
  36. _updateHeadImageView.hidden = YES;
  37. _headerImageView.hidden = NO;
  38. _headerTitleView.hidden = NO;
  39. _headerTitleLabel.text = @"让你距离拿本更进一步";
  40. _titleLabel.text = [NSString stringWithFormat:@"%@题库下载",RQ_COMMON_MANAGER.carTypeStr];
  41. _titleLabel.textAlignment = NSTextAlignmentLeft;
  42. _subTitleLabel.hidden = NO;
  43. _subTitleLabel.textAlignment = NSTextAlignmentLeft;
  44. _subTitleLabel.text = @"更新题库不会影响做题记录";
  45. _progressView.hidden = YES;
  46. _completeImageView.hidden = YES;
  47. _progressLabel.hidden = YES;
  48. _downloadBtn.hidden = NO;
  49. [_downloadBtn removeAllTargets];
  50. [_downloadBtn addTarget:self action:@selector(updateDB) forControlEvents:UIControlEventTouchUpInside];
  51. [_downloadBtn setTitleNormal:@"立即下载"];
  52. _closeBtn.hidden = NO;
  53. break;
  54. }
  55. case RQQuestionUpdateViewType_Request: {
  56. _updateHeadImageView.hidden = YES;
  57. _headerImageView.hidden = NO;
  58. _headerTitleView.hidden = NO;
  59. _headerTitleLabel.text = @"让你距离拿本更进一步";
  60. _titleLabel.text = [NSString stringWithFormat:@"%@题库下载中",RQ_COMMON_MANAGER.carTypeStr];
  61. _titleLabel.textAlignment = NSTextAlignmentLeft;
  62. _subTitleLabel.text = @"";
  63. _subTitleLabel.hidden = YES;
  64. _subTitleLabel.textAlignment = NSTextAlignmentLeft;
  65. _progressView.hidden = NO;
  66. _completeImageView.hidden = YES;
  67. _progressLabel.hidden = NO;
  68. _downloadBtn.hidden = YES;
  69. [_downloadBtn setTitleNormal:@"立即下载"];
  70. _closeBtn.hidden = YES;
  71. break;
  72. }
  73. case RQQuestionUpdateViewType_Complete: {
  74. _updateHeadImageView.hidden = YES;
  75. _headerImageView.hidden = NO;
  76. _headerTitleView.hidden = NO;
  77. _headerTitleLabel.text = @"下载完成,祝你早日拿本";
  78. _titleLabel.text = [NSString stringWithFormat:@"%@题库下载完成",RQ_COMMON_MANAGER.carTypeStr];
  79. _titleLabel.textAlignment = NSTextAlignmentLeft;
  80. _subTitleLabel.hidden = YES;
  81. _subTitleLabel.textAlignment = NSTextAlignmentLeft;
  82. _subTitleLabel.text = @"";
  83. _progressView.hidden = YES;
  84. _completeImageView.hidden = NO;
  85. _progressLabel.hidden = YES;
  86. _downloadBtn.hidden = NO;
  87. [_downloadBtn removeAllTargets];
  88. [_downloadBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  89. [_downloadBtn setTitleNormal:@"完成"];
  90. _closeBtn.hidden = YES;
  91. break;
  92. }
  93. case RQQuestionUpdateViewType_Default:
  94. default: {
  95. _updateHeadImageView.hidden = NO;
  96. _headerImageView.hidden = YES;
  97. _headerTitleView.hidden = YES;
  98. _headerTitleLabel.text = @"";
  99. _titleLabel.textAlignment = NSTextAlignmentCenter;
  100. _titleLabel.text = @"发现新版本";
  101. _subTitleLabel.hidden = NO;
  102. _subTitleLabel.textAlignment = NSTextAlignmentCenter;
  103. _subTitleLabel.text = @"全新版本,全新体验,赶快更新吧";
  104. _progressView.hidden = YES;
  105. _completeImageView.hidden = YES;
  106. _progressLabel.hidden = YES;
  107. _downloadBtn.hidden = NO;
  108. [_downloadBtn removeAllTargets];
  109. [_downloadBtn addTarget:self action:@selector(updateApp) forControlEvents:UIControlEventTouchUpInside];
  110. [_downloadBtn setTitleNormal:@"立即更新"];
  111. _closeBtn.hidden = NO;
  112. break;
  113. }
  114. }
  115. [self layoutIfNeeded];
  116. }
  117. - (void)updateDB {
  118. self.questionUpdateViewType = RQQuestionUpdateViewType_Request;
  119. @weakify(self)
  120. NSError *error;
  121. /// 题库沙盒路径
  122. NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"question.db"]];
  123. /// 题库存储路径
  124. NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"question" ofType:@"db"];
  125. if ([RQFileManager isPathExist:dbPath]) {
  126. NSLog(@"数据库path-----%@",dbPath);
  127. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  128. AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
  129. NSURL *url = [NSURL URLWithString:self.downloadUrl];
  130. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  131. NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
  132. @strongify(self)
  133. dispatch_sync(dispatch_get_main_queue(), ^{
  134. _progress = downloadProgress.fractionCompleted;
  135. NSLog(@"%f",_progress);
  136. self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",_progress * 100];
  137. [self.myProgress setProgress:_progress];
  138. });
  139. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  140. NSURL *pathURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
  141. return [pathURL URLByAppendingPathComponent:[NSString stringWithFormat:@"new%@",[response suggestedFilename]]];
  142. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  143. @strongify(self)
  144. if (!error) {
  145. /// 下载完成的存储路径
  146. NSString *resourcePath1 = filePath.path;
  147. [RQ_QUESTION_DB_MANAGER initDatabaseQueueWithResourcePath:resourcePath1];
  148. self.questionUpdateViewType = RQQuestionUpdateViewType_Complete;
  149. } else {
  150. }
  151. }];
  152. [task resume];
  153. } else {
  154. [[RQFileManager fileManager] copyItemAtPath:resourcePath toPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"question.db"]] error:&error];
  155. [RQ_QUESTION_DB_MANAGER initDatabaseQueueWithResourcePath:resourcePath];
  156. }
  157. }
  158. - (void)closeAction {
  159. @weakify(self)
  160. [self.modalViewController hideWithAnimated:YES completion:^(BOOL finished) {
  161. @strongify(self)
  162. if (finished) {
  163. self.modalViewController.delegate = nil;
  164. }
  165. }];
  166. }
  167. - (void)updateApp {
  168. @weakify(self)
  169. NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/su-da-jia-kao/id1586064774?l=en&mt=8"];
  170. if (@available(iOS 10.0, *)) {
  171. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
  172. @strongify(self)
  173. if (success) {
  174. [self closeAction];
  175. }
  176. }];
  177. } else {
  178. // Fallback on earlier versions
  179. [[UIApplication sharedApplication] openURL:url];
  180. }
  181. }
  182. - (IBAction)closeBtnAction:(id)sender {
  183. [self closeAction];
  184. }
  185. @end