// // RQExerciseSubViewController.m // SDJK // // Created by 张嵘 on 2021/8/12. // #import "RQExerciseSubViewController.h" #import "HWPanModal.h" @interface RQExerciseSubViewController () /// viewModel @property (nonatomic, readonly, strong) RQExerciseSubViewModel *viewModel; @property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView); @property (nonatomic, readwrite, weak) DZMCoverController *coverVC; @property (nonatomic, readwrite, assign) NSInteger firstNum;///再次进来的第一题 @property (nonatomic, readwrite, retain) FMDatabase *db; @property (nonatomic, readwrite, retain) FMDatabaseQueue *dbQueue; @property (nonatomic, readwrite, strong) RQExerciseToolBarView *exerciseToolBarView; @property (nonatomic, readwrite, strong) RQHandInThePaperView *handInThePaperView; @property (nonatomic, readwrite, strong) NSMutableArray *correctArr; @property (nonatomic, readwrite, strong) NSMutableArray *errorArr; @end @implementation RQExerciseSubViewController @dynamic viewModel; #pragma mark - SystemMethod - (void)viewDidLoad { [super viewDidLoad]; /// 初始化 [self rq_setup]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // int popNum = _firstNum subscriber) { [subscriber sendNext:@"延时2秒"]; return nil; }] delay:2] subscribeNext:^(id x) { [self jumpNextQuestion]; NSLog(@"-->%@",x); }]; } else { /// 自动读题模式下: 当前题目正在自动读题 暂不自动跳到下一题 NSLog(@"自动读题模式下: 当前题目正在自动读题 暂不自动跳到下一题"); } } } break; case RQAnswerResultsType_Error: { if ([self.errorArr indexOfObject:exerciseModel] == NSNotFound && ![self.errorArr containsObject:exerciseModel]) { [_errorArr addObject:exerciseModel]; if (self.viewModel.exerciseType != RQExerciseType_Recitation && self.viewModel.exerciseType != RQExerciseType_Test && self.viewModel.exerciseType != RQExerciseType_Exam) { [RQ_ALERTVIEW_MANAGER rq_showSkillExplanationAlertWithMessage:exerciseModel.explain_jq skillkeyword:exerciseModel.skillkeyword explainGifUrl:exerciseModel.explain_gif mp3Url:exerciseModel.explain_mp3 completeBlock:nil]; } RQWrongModel *wrongModel = [[RQWrongModel alloc] init]; wrongModel.questionId = exerciseModel._id; [[RQ_HTTP_Service queryAddWrongRecordWithQuestionId:exerciseModel._id carType:notifiCarType subject:notifiSubjectType] subscribeNext:^(id _Nullable x) { [RQ_SDJK_DB_MANAGER addWrongRecordWithRQWrongModel:wrongModel]; NSLog(@"%@",x); }]; } if (self.viewModel.exerciseType != RQExerciseType_Test && self.viewModel.exerciseType != RQExerciseType_Exam) { _exerciseToolBarView.errorOptionNumLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.errorArr.count]; } } break; default: break; } switch (exerciseDoType) { case RQExerciseDoType_Default: { break; } case RQExerciseDoType_Auto: { break; } case RQExerciseDoType_SkillExplanation: { [RQ_ALERTVIEW_MANAGER rq_showSkillExplanationAlertWithMessage:exerciseModel.explain_jq skillkeyword:exerciseModel.skillkeyword explainGifUrl:exerciseModel.explain_gif mp3Url:exerciseModel.explain_mp3 completeBlock:nil]; break; } case RQExerciseDoType_ReadAndAnswer: { // [RQ_MUSIC_MANAGER rq_resetStreamerWithURLString:exerciseModel.explainjsmp3]; break; } default: break; } }]; } - (void)creatCoverVC { DZMCoverController *coverVC = [[DZMCoverController alloc] init]; coverVC.delegate = self; [self.view addSubview:coverVC.view]; [self addChildViewController:coverVC]; self.coverVC = coverVC; [self.coverVC setController:[self creatSubVC]]; } - (RQTestQuestionsViewController *)creatSubVC { RQExerciseModel *exerciseModel = self.questionArr[self.number]; exerciseModel.num = self.number; exerciseModel.allNum = self.questionArr.count; exerciseModel.isShowSkillExplanation = NO; RQTestQuestionsViewModel *testQuestionsViewModel = [[RQTestQuestionsViewModel alloc] initWithServices:self.viewModel.services params:@{ RQViewModelUtilKey : exerciseModel, RQExerciseTypeKey : @(self.viewModel.exerciseType), RQHomePageCarTypeKey : @(self.viewModel.homePageCarType), RQHomePageSubjectTypeKey: @(self.viewModel.homePageSubjectType), }]; testQuestionsViewModel.lastQuestionCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) { [self jumpLastQuestion]; return [RACSignal empty]; }]; testQuestionsViewModel.nextQuestionCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) { [self jumpNextQuestion]; return [RACSignal empty]; }]; testQuestionsViewModel.showCatalogueCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) { [self showCatalogue]; return [RACSignal empty]; }]; RQTestQuestionsViewController *vc = [[RQTestQuestionsViewController alloc] initWithViewModel:testQuestionsViewModel]; return vc; } /// 根据车型取题 - (void)changeCityQuestion { self.questionArr = [RQ_QUESTION_DB_MANAGER getQuestionWithCarType:self.viewModel.homePageCarType subject:self.viewModel.homePageSubjectType pageType:self.viewModel.homeSubPageType name:self.viewModel.titleStr exerciseType:self.viewModel.exerciseType].mutableCopy; // [self searchExamRecord]; //顺序练习 if (self.questionArr.count > 0) { [self creatCoverVC]; [self creatToolsBar]; } } /// 查看答题记录 - (void)searchExamRecord { // FMResultSet *RightresultSet = [_db executeQuery:@"select * from dt_practice_record where course = 1 and mode = 1 and cert_type = 1"]; // // 逐行读取数据 // while ( [ RightresultSet next ] ) // { // NSInteger ID = [RightresultSet intForColumn:@"_id"]; // for (RQExerciseModel *item in self.questionArr) { // if (item._id == ID) { // item.answer = [RightresultSet stringForColumn:@"answer"]; // if ([item.userAnswer isEqualToString:item.answer]) { // item.answerResultsType=1;//答对 // }else{ // item.answerResultsType=2;//答错 // } // } // } // // } } /// 保存更新答题记录 ,如果没有打完自动跳转到下一题 ,model:1顺序2随机3专项4答对5打错6未答7收藏8模拟9考试纪录10章节 . */ - (void)saveTheAnswerRecordActionWithRQExerciseModel:(RQExerciseModel *_Nullable)exerciseModel { /* *自动跳转到下一题 */ _firstNum = self.number; // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // [self.dbQueue inDatabase:^(FMDatabase *db) { // BOOL issecect = [db executeUpdate:@"INSERT INTO dt_practice_record (course,mode,cert_type,question_id,user_answer,RIGHT,more_types) VALUES (1,1,1,?,?,?)", // [NSNumber numberWithInteger:exerciseModel._id], // exerciseModel.userAnswer, // exerciseModel.answer]; // if (issecect) { // NSLog(@"____________插入成功%ld",(long)exerciseModel._id); // } // }]; // }); } - (void)jumpNextQuestion { if (self.number + 1 >= self.questionArr.count) { return; } self.number += 1; [self.coverVC setController:[self creatSubVC] animated:YES isAbove:NO]; if (self.viewModel.exerciseType != RQExerciseType_Test && self.viewModel.exerciseType != RQExerciseType_Exam) { [self.view bringSubviewToFront:self.exerciseToolBarView]; } else { [self.view bringSubviewToFront:self.handInThePaperView]; } } - (void)jumpLastQuestion{ if (self.number == 0) { return; } self.number -= 1; [self.coverVC setController:[self creatSubVC] animated:YES isAbove:YES]; if (self.viewModel.exerciseType != RQExerciseType_Test && self.viewModel.exerciseType != RQExerciseType_Exam) { [self.view bringSubviewToFront:self.exerciseToolBarView]; } else { [self.view bringSubviewToFront:self.handInThePaperView]; } } - (void)jumpQuestionWithIndex:(NSUInteger)index { self.number = (int)index; [self.coverVC setController:[self creatSubVC] animated:NO isAbove:NO]; if (self.viewModel.exerciseType != RQExerciseType_Test && self.viewModel.exerciseType != RQExerciseType_Exam) { [self.view bringSubviewToFront:self.exerciseToolBarView]; } else { [self.view bringSubviewToFront:self.handInThePaperView]; } } - (void)creatToolsBar { if (self.viewModel.exerciseType != RQExerciseType_Test && self.viewModel.exerciseType != RQExerciseType_Exam) { [self.view addSubview:self.exerciseToolBarView]; [_exerciseToolBarView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.view); make.bottom.mas_equalTo(self.view).mas_offset(-(RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT + RQ_APPLICATION_NAV_BAR_HEIGHT)); make.size.mas_equalTo(CGSizeMake(RQ_SCREEN_WIDTH, 50)); }]; } else { [self.view addSubview:self.handInThePaperView]; [_handInThePaperView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.view); make.bottom.mas_equalTo(self.view).mas_offset(-(RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT + RQ_APPLICATION_NAV_BAR_HEIGHT)); make.size.mas_equalTo(CGSizeMake(RQ_SCREEN_WIDTH, 50)); }]; } } - (void)showCatalogue { RQCatalogueViewModel *catalogueViewModel = [[RQCatalogueViewModel alloc] initWithServices:self.viewModel.services params:@{ RQViewModelIDKey : @(self.number), RQViewModelUtilKey : self.questionArr, RQViewCommonValueKey : @(self.correctArr.count), RQViewModelRequestKey : @(self.errorArr.count), }]; RQCatalogueViewController *catalogueViewController = [[RQCatalogueViewController alloc] initWithViewModel:catalogueViewModel]; [catalogueViewController initCompletionWithSelectIndexBlock:^(NSInteger index) { [self jumpQuestionWithIndex:index]; }]; [self presentPanModal:catalogueViewController]; } #pragma mark - DZMCoverControllerDelegate /// 切换结果 - (void)coverController:(DZMCoverController *)coverController currentController:(UIViewController *)currentController finish:(BOOL)isFinish { if (!isFinish) { // 切换失败 // RQTestQuestionsViewController *vc = (RQTestQuestionsViewController *)currentController; } } /// 上一个控制器 - (UIViewController *)coverController:(DZMCoverController *)coverController getAboveControllerWithCurrentController:(UIViewController *)currentController { if (self.number == 0) { return nil; } self.number -= 1; return [self creatSubVC]; } /// 下一个控制器 - (UIViewController *)coverController:(DZMCoverController *)coverController getBelowControllerWithCurrentController:(UIViewController *)currentController { if (self.number==self.questionArr.count-1) { return nil; } self.number += 1; return [self creatSubVC]; } #pragma mark - UITableViewDataSource, UITableViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { !self.scrollCallback ?: self.scrollCallback(scrollView); } #pragma mark - JXPagingViewListViewDelegate - (UIView *)listView { return self.view; } - (UIScrollView *)listScrollView { return self.tableView; } - (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback { self.scrollCallback = callback; } #pragma mark - LazyLoad - (NSMutableArray *)questionArr { if (!_questionArr) { _questionArr = @[].mutableCopy; } return _questionArr; } - (RQExerciseToolBarView *)exerciseToolBarView { if (!_exerciseToolBarView) { @weakify(self) _exerciseToolBarView = [RQExerciseToolBarView exerciseToolBarView]; [RACObserve(self, number) subscribeNext:^(id _Nullable x) { _exerciseToolBarView.totalNumberOfQuestionsLabel.text = [NSString stringWithFormat:@"%ld/%lu",[x integerValue] + 1,(unsigned long)self.questionArr.count]; }]; [_exerciseToolBarView.lastQuestionView setTapActionWithBlock:^(UITapGestureRecognizer *tap) { [self jumpLastQuestion]; }]; [_exerciseToolBarView.nextQuestionView setTapActionWithBlock:^(UITapGestureRecognizer *tap) { [self jumpNextQuestion]; }]; [_exerciseToolBarView.totalNumberOfQuestionsView setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) [self showCatalogue]; }]; [_exerciseToolBarView.officialInterpretationView setTapActionWithBlock:^(UITapGestureRecognizer *tap) { RQExerciseModel *exerciseModel = self.questionArr[self.number]; if (RQ_Exercise_Module.isAutoRead) RQ_Exercise_Module.isAutoRead = NO; [RQ_ALERTVIEW_MANAGER rq_showOfficialInterpretationAlertWithMessage:exerciseModel.explain_js mp3Url:exerciseModel.explainjsmp3]; }]; } return _exerciseToolBarView; } - (RQHandInThePaperView *)handInThePaperView { if (!_handInThePaperView) { _handInThePaperView = [RQHandInThePaperView handInThePaperView]; [_handInThePaperView.handInThePaperBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { NSInteger score = (self.viewModel.homePageSubjectType == RQHomePageSubjectType_SubjectOne && (self.viewModel.homePageCarType == RQHomePageCarType_Car || self.viewModel.homePageCarType == RQHomePageCarType_Bus || self.viewModel.homePageCarType == RQHomePageCarType_Truck))? self.correctArr.count : self.correctArr.count * 2; RQTestResultsViewModel *testResultsViewModel = [[RQTestResultsViewModel alloc] initWithServices:self.viewModel.services params:@{ RQHomePageCarTypeKey : @(self.viewModel.homePageCarType), RQHomePageSubjectTypeKey : @(self.viewModel.homePageSubjectType), RQHomeSubPageTypeKey : @(self.viewModel.homeSubPageType), RQExerciseTypeKey : @(self.viewModel.exerciseType), RQViewModelIDKey : @(score), RQViewCommonValueKey : @(self.count), RQViewModelUtilKey : self.questionArr, }]; [self.viewModel.services pushViewModel:testResultsViewModel animated:YES]; }]; } return _handInThePaperView; } - (NSMutableArray *)correctArr { if (!_correctArr) { _correctArr = @[].mutableCopy; } return _correctArr; } - (NSMutableArray *)errorArr { if (!_errorArr) { _errorArr = @[].mutableCopy; } return _errorArr; } @end