// // RQUpdateView.m // SDJK // // Created by 张嵘 on 2022/5/19. // #import "RQUpdateView.h" @interface RQUpdateView () @property (weak, nonatomic) IBOutlet UIImageView *updateHeadImageView; @property (weak, nonatomic) IBOutlet UIImageView *headerImageView; @property (weak, nonatomic) IBOutlet UIStackView *headerTitleView; @property (weak, nonatomic) IBOutlet UILabel *headerTitleLabel; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *subTitleLabel; @property (weak, nonatomic) IBOutlet UIButton *downloadBtn; @property (weak, nonatomic) IBOutlet UIStackView *progressView; @property (weak, nonatomic) IBOutlet UILabel *progressLabel; @property (weak, nonatomic) IBOutlet UIProgressView *myProgress; @property (weak, nonatomic) IBOutlet UILabel *requestLabel; @property (weak, nonatomic) IBOutlet UIImageView *completeImageView; @property (weak, nonatomic) IBOutlet UIButton *closeBtn; @property (nonatomic, readwrite, assign) CGFloat progress; @end @implementation RQUpdateView + (instancetype)updateView { return [super rq_viewFromXib]; } - (void)awakeFromNib { [super awakeFromNib]; _progress = 0.f; _downloadBtn.layer.cornerRadius = RQ_FIT_HORIZONTAL(40.f) / 2.f; } - (void)setQuestionUpdateViewType:(RQQuestionUpdateViewType)questionUpdateViewType { switch (questionUpdateViewType) { case RQQuestionUpdateViewType_LibDownload: { _updateHeadImageView.hidden = YES; _headerImageView.hidden = NO; _headerTitleView.hidden = NO; _headerTitleLabel.text = @"让你距离拿本更进一步"; _titleLabel.text = [NSString stringWithFormat:@"%@题库下载",RQ_COMMON_MANAGER.carTypeStr]; _titleLabel.textAlignment = NSTextAlignmentLeft; _subTitleLabel.hidden = NO; _subTitleLabel.textAlignment = NSTextAlignmentLeft; _subTitleLabel.text = @"更新题库不会影响做题记录"; _progressView.hidden = YES; _completeImageView.hidden = YES; _progressLabel.hidden = YES; _downloadBtn.hidden = NO; [_downloadBtn removeAllTargets]; [_downloadBtn addTarget:self action:@selector(updateDB) forControlEvents:UIControlEventTouchUpInside]; [_downloadBtn setTitleNormal:@"立即下载"]; _closeBtn.hidden = NO; break; } case RQQuestionUpdateViewType_Request: { _updateHeadImageView.hidden = YES; _headerImageView.hidden = NO; _headerTitleView.hidden = NO; _headerTitleLabel.text = @"让你距离拿本更进一步"; _titleLabel.text = [NSString stringWithFormat:@"%@题库下载中",RQ_COMMON_MANAGER.carTypeStr]; _titleLabel.textAlignment = NSTextAlignmentLeft; _subTitleLabel.text = @""; _subTitleLabel.hidden = YES; _subTitleLabel.textAlignment = NSTextAlignmentLeft; _progressView.hidden = NO; _completeImageView.hidden = YES; _progressLabel.hidden = NO; _downloadBtn.hidden = YES; [_downloadBtn setTitleNormal:@"立即下载"]; _closeBtn.hidden = YES; break; } case RQQuestionUpdateViewType_Complete: { _updateHeadImageView.hidden = YES; _headerImageView.hidden = NO; _headerTitleView.hidden = NO; _headerTitleLabel.text = @"下载完成,祝你早日拿本"; _titleLabel.text = [NSString stringWithFormat:@"%@题库下载完成",RQ_COMMON_MANAGER.carTypeStr]; _titleLabel.textAlignment = NSTextAlignmentLeft; _subTitleLabel.hidden = YES; _subTitleLabel.textAlignment = NSTextAlignmentLeft; _subTitleLabel.text = @""; _progressView.hidden = YES; _completeImageView.hidden = NO; _progressLabel.hidden = YES; _downloadBtn.hidden = NO; [_downloadBtn removeAllTargets]; [_downloadBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside]; [_downloadBtn setTitleNormal:@"完成"]; _closeBtn.hidden = YES; break; } case RQQuestionUpdateViewType_Default: default: { _updateHeadImageView.hidden = NO; _headerImageView.hidden = YES; _headerTitleView.hidden = YES; _headerTitleLabel.text = @""; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.text = @"发现新版本"; _subTitleLabel.hidden = NO; _subTitleLabel.textAlignment = NSTextAlignmentCenter; _subTitleLabel.text = @"全新版本,全新体验,赶快更新吧"; _progressView.hidden = YES; _completeImageView.hidden = YES; _progressLabel.hidden = YES; _downloadBtn.hidden = NO; [_downloadBtn removeAllTargets]; [_downloadBtn addTarget:self action:@selector(updateApp) forControlEvents:UIControlEventTouchUpInside]; [_downloadBtn setTitleNormal:@"立即更新"]; _closeBtn.hidden = NO; break; } } [self layoutIfNeeded]; } - (void)updateDB { self.questionUpdateViewType = RQQuestionUpdateViewType_Request; @weakify(self) NSError *error; /// 题库沙盒路径 NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"question.db"]]; /// 题库存储路径 NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"question" ofType:@"db"]; if ([RQFileManager isPathExist:dbPath]) { NSLog(@"数据库path-----%@",dbPath); NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *url = [NSURL URLWithString:self.downloadUrl]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { @strongify(self) dispatch_sync(dispatch_get_main_queue(), ^{ _progress = downloadProgress.fractionCompleted; NSLog(@"%f",_progress); self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",_progress * 100]; [self.myProgress setProgress:_progress]; }); } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { NSURL *pathURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]; return [pathURL URLByAppendingPathComponent:[NSString stringWithFormat:@"new%@",[response suggestedFilename]]]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { @strongify(self) if (!error) { /// 下载完成的存储路径 NSString *resourcePath1 = filePath.path; [RQ_QUESTION_DB_MANAGER initDatabaseQueueWithResourcePath:resourcePath1]; self.questionUpdateViewType = RQQuestionUpdateViewType_Complete; } else { } }]; [task resume]; } else { [[RQFileManager fileManager] copyItemAtPath:resourcePath toPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"question.db"]] error:&error]; [RQ_QUESTION_DB_MANAGER initDatabaseQueueWithResourcePath:resourcePath]; } } - (void)closeAction { @weakify(self) [self.modalViewController hideWithAnimated:YES completion:^(BOOL finished) { @strongify(self) if (finished) { self.modalViewController.delegate = nil; } }]; } - (void)updateApp { @weakify(self) NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/su-da-jia-kao/id1586064774?l=en&mt=8"]; if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) { @strongify(self) if (success) { [self closeAction]; } }]; } else { // Fallback on earlier versions [[UIApplication sharedApplication] openURL:url]; } } - (IBAction)closeBtnAction:(id)sender { [self closeAction]; } @end