// // RQUpdateView.m // SDJK // // Created by 张嵘 on 2022/5/19. // #import "RQUpdateView.h" #import "RQYDTXCQuestionModule.h" #import "RQYDTHCQuestionModule.h" #import "RQYDTKCQuestionModule.h" #import "RQYDTMTCQuestionModule.h" #import "RQYDTZGZJLYQuestionModule.h" #import "RQYDTZGZKYQuestionModule.h" #import "RQYDTZGZHYQuestionModule.h" #import "RQYDTZGZWXPQuestionModule.h" #import "RQYDTZGZCZCQuestionModule.h" #import "RQYDTZGZWYCQuestionModule.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_YDTQuestion_Module getCarTypeCNTiKuUpdateNameWithCarType:RQ_YDTQuestion_Module.carType]]; _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_ExplainDownload: { _updateHeadImageView.hidden = YES; _headerImageView.hidden = NO; _headerTitleView.hidden = NO; _headerTitleLabel.text = @"让你距离拿本更进一步"; _titleLabel.text = [NSString stringWithFormat:@"%@下载",@"题目解析"]; _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(updateJS) forControlEvents:UIControlEventTouchUpInside]; [_downloadBtn setTitleNormal:@"立即下载"]; _closeBtn.hidden = NO; break; } case RQQuestionUpdateViewType_ExplainDownloading: { _updateHeadImageView.hidden = YES; _headerImageView.hidden = NO; _headerTitleView.hidden = NO; _headerTitleLabel.text = @"让你距离拿本更进一步"; _titleLabel.text = [NSString stringWithFormat:@"%@下载中",@"题目解析"]; _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_ExplainDownloaded: { _updateHeadImageView.hidden = YES; _headerImageView.hidden = NO; _headerTitleView.hidden = NO; _headerTitleLabel.text = @"下载完成,祝你早日拿本"; _titleLabel.text = [NSString stringWithFormat:@"%@下载完成",@"题目解析"]; _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_Request: { _updateHeadImageView.hidden = YES; _headerImageView.hidden = NO; _headerTitleView.hidden = NO; _headerTitleLabel.text = @"让你距离拿本更进一步"; _titleLabel.text = [NSString stringWithFormat:@"%@题库下载中",[RQ_YDTQuestion_Module getCarTypeCNTiKuUpdateNameWithCarType:RQ_YDTQuestion_Module.carType]]; _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_YDTQuestion_Module getCarTypeCNTiKuUpdateNameWithCarType:RQ_YDTQuestion_Module.carType]]; _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; NSString *dbNameStr = [[self.downloadUrl componentsSeparatedByString:@"/"].lastObject componentsSeparatedByString:@"."].firstObject; @weakify(self) NSError *error; /// 题库沙盒路径 NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.db",dbNameStr]]; /// 题库存储路径 NSString *resourcePath = [[NSBundle mainBundle] pathForResource:dbNameStr 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; switch (RQ_YDTQuestion_Module.carType) { case RQHomePageCarType_Car: [RQ_YDT_XC_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_Truck: [RQ_YDT_HC_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_Bus: [RQ_YDT_KC_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_Motorcycle: [RQ_YDT_MTC_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_ZGZ_JLY: [RQ_YDT_ZGZ_JLY_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_ZGZ_KY: [RQ_YDT_ZGZ_KY_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_ZGZ_HY: [RQ_YDT_ZGZ_HY_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_ZGZ_WXP: [RQ_YDT_ZGZ_WXP_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_ZGZ_CZC: [RQ_YDT_ZGZ_CZC_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; case RQHomePageCarType_ZGZ_WYC: [RQ_YDT_ZGZ_WYC_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; default: [RQ_YDT_XC_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; break; } self.questionUpdateViewType = RQQuestionUpdateViewType_Complete; [MBProgressHUD rq_showProgressHUD:@"升级题库中..."]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ @strongify(self) /// 处理耗时操作的代码块... [RQ_YDTQuestion_Module saveQuestionsWithSubjectOneArr:[RQ_YDTQuestion_Module getQuestionWithSubject:RQHomePageSubjectType_SubjectOne exerciseType:RQExerciseType_Sequential]]; [RQ_YDTQuestion_Module saveQuestionsWithSubjectFourArr:[RQ_YDTQuestion_Module getQuestionWithSubject:RQHomePageSubjectType_SubjectFour exerciseType:RQExerciseType_Sequential]]; /// 通知主线程刷新 dispatch_async(dispatch_get_main_queue(), ^{ /// 回调或者说是通知主线程刷新, [MBProgressHUD rq_hideHUD]; [RQ_APPDELEGATE.services popViewModelAnimated:YES]; [MBProgressHUD rq_showTips:@"题库升级成功"]; if (RQ_USER_MANAGER.isLogin) { [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectOne] subscribeNext:^(NSArray *examResultModelArr) { RQ_COMMON_MANAGER.myExamOneCount = examResultModelArr.count; RQ_COMMON_MANAGER.examResultOneListArr = examResultModelArr; }]; [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectFour] subscribeNext:^(NSArray *examResultModelArr) { RQ_COMMON_MANAGER.myExamFourCount = examResultModelArr.count; RQ_COMMON_MANAGER.examResultFourListArr = examResultModelArr; }]; } else { RQ_COMMON_MANAGER.myExamOneCount = 0; RQ_COMMON_MANAGER.examResultOneListArr = @[]; RQ_COMMON_MANAGER.myExamFourCount = 0; RQ_COMMON_MANAGER.examResultFourListArr = @[]; } }); }); } else { } }]; [task resume]; } else { [[RQFileManager fileManager] copyItemAtPath:resourcePath toPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.db",dbNameStr]] error:&error]; switch (RQ_YDTQuestion_Module.carType) { case RQHomePageCarType_Car: [RQ_YDT_XC_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_Truck: [RQ_YDT_HC_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_Bus: [RQ_YDT_KC_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_Motorcycle: [RQ_YDT_MTC_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_ZGZ_JLY: [RQ_YDT_ZGZ_JLY_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_ZGZ_KY: [RQ_YDT_ZGZ_KY_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_ZGZ_HY: [RQ_YDT_ZGZ_HY_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_ZGZ_WXP: [RQ_YDT_ZGZ_WXP_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_ZGZ_CZC: [RQ_YDT_ZGZ_CZC_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; case RQHomePageCarType_ZGZ_WYC: [RQ_YDT_ZGZ_WYC_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; default: [RQ_YDT_XC_Question_Module initDatabaseQueueWithResourcePath:resourcePath]; break; } } } - (void)updateJS { self.questionUpdateViewType = RQQuestionUpdateViewType_ExplainDownloading; NSString *dbNameStr = [[self.jsDownloadUrl componentsSeparatedByString:@"/"].lastObject componentsSeparatedByString:@"."].firstObject; @weakify(self) NSError *error; /// 题库沙盒路径 NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.db",dbNameStr]]; /// 题库存储路径 NSString *resourcePath = [[NSBundle mainBundle] pathForResource:dbNameStr ofType:@"db"]; if ([RQFileManager isPathExist:dbPath]) { NSLog(@"数据库path-----%@",dbPath); NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *url = [NSURL URLWithString:self.jsDownloadUrl]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { @strongify(self) dispatch_sync(dispatch_get_main_queue(), ^{ @strongify(self) self.progress = downloadProgress.fractionCompleted; self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",self.progress * 100]; [self.myProgress setProgress:self.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; if([resourcePath1 containsString:@"kt_jq"]){ [YN_YDT_JQ_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; }else { [RQ_YDT_JS_Question_Module initDatabaseQueueWithResourcePath:resourcePath1]; } self.questionUpdateViewType = RQQuestionUpdateViewType_ExplainDownloaded; } else { } }]; [task resume]; } else { [[RQFileManager fileManager] copyItemAtPath:resourcePath toPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.db",dbNameStr]] error:&error]; [RQ_YDT_JS_Question_Module 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/ji-su-jia-pei-kua-jia-xiao-ban/id1214200225?l=en&mt=8"]; [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) { @strongify(self) if (success) { [self closeAction]; } }]; } - (IBAction)closeBtnAction:(id)sender { [self closeAction]; } @end