RQExerciseViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // RQExerciseViewController.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2021/8/12.
  6. //
  7. #import "RQExerciseViewController.h"
  8. #import "YYTimer.h"
  9. @interface RQExerciseViewController () <JXCategoryViewDelegate, JXPagerViewDelegate, JXPagerMainTableViewGestureDelegate>
  10. /// viewModel
  11. @property (nonatomic, readonly, strong) RQExerciseViewModel *viewModel;
  12. @property (nonatomic, readwrite, strong) JXPagerView *pagerView;
  13. @property (nonatomic, readwrite, strong) JXCategoryTitleBackgroundView *categoryView;
  14. @property (nonatomic, readwrite, copy) NSArray <NSString *> *titles;
  15. @property (nonatomic, readwrite, strong) UIView *customPinView;
  16. @property (nonatomic, readwrite, strong) UIButton *autoReadBtn;
  17. @property (nonatomic, readwrite, strong) RQCountdownView *countdownView;
  18. /// Timer
  19. @property (nonatomic, readwrite, strong) YYTimer *timer;
  20. /// Count
  21. @property (nonatomic, readwrite, assign) NSUInteger count;
  22. @end
  23. @implementation RQExerciseViewController
  24. @dynamic viewModel;
  25. #pragma mark - SystemMethod
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. /// 初始化
  29. [self rq_setup];
  30. }
  31. - (void)viewDidLayoutSubviews {
  32. [super viewDidLayoutSubviews];
  33. self.pagerView.frame = CGRectMake(0, RQ_APPLICATION_TOP_BAR_HEIGHT, RQ_SCREEN_WIDTH, RQ_SCREEN_HEIGHT - RQ_APPLICATION_TOP_BAR_HEIGHT - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT);
  34. }
  35. - (void)dealloc {
  36. [RQ_Exercise_Module cancleAutoReadQuestion];
  37. }
  38. #pragma mark - PrivateMethods
  39. /// 初始化
  40. - (void)rq_setup {
  41. /// set up ...
  42. [self.view addSubview:self.pagerView];
  43. self.navigationItem.rightBarButtonItem = [UIBarButtonItem rq_customItemWithTitle:@"" font:RQMediumFont(15) titleColor:RQ_MAIN_TEXT_COLOR_1 imageName:@"SetUp" target:self selector:@selector(setAction) contentHorizontalAlignment:UIControlContentHorizontalAlignmentRight EdgeInsetsStyle:RQButtonEdgeInsetsStyleLeft space:4.f];
  44. self.count = (self.viewModel.homePageCarType == RQHomePageCarType_Motorcycle)? 30 * 60 : 45 * 60;
  45. self.timer = [YYTimer timerWithTimeInterval:1 target:self selector:@selector(_timerValueChanged:) repeats:YES];
  46. [self.navigationItem setLeftBarButtonItems:@[[UIBarButtonItem rq_backItemWithTitle:@"" imageName:@"back_black" target:self action:@selector(rq_back)]]];
  47. }
  48. - (void)rq_back {
  49. if (self.viewModel.exerciseType == RQExerciseType_Exam) {
  50. [self.navigationController popToRootViewControllerAnimated:YES];
  51. } else {
  52. [self.navigationController popViewControllerAnimated:YES];
  53. }
  54. }
  55. - (void)setAction {
  56. // @weakify(self)
  57. [RQ_Exercise_Module cancleAutoReadQuestion];
  58. RQExerciseSettingViewModel *exerciseSettingViewModel = [[RQExerciseSettingViewModel alloc] initWithServices:self.viewModel.services params:nil];
  59. RQExerciseSettingViewController *exerciseSettingViewController = [[RQExerciseSettingViewController alloc] initWithViewModel:exerciseSettingViewModel];
  60. // [catalogueViewController initCompletionWithSelectIndexBlock:^(NSInteger index) {
  61. // @strongify(self)
  62. // [self jumpQuestionWithIndex:index];
  63. // }];
  64. [self presentPanModal:exerciseSettingViewController];
  65. }
  66. - (void)_timerValueChanged:(YYTimer *)timer {
  67. self.count--;
  68. if (self.count == 0) {
  69. [timer invalidate];
  70. self.timer = nil;
  71. // [self _resetCaptcha];
  72. return;
  73. }
  74. NSUInteger sec = _count%60;
  75. NSUInteger min = _count/60;
  76. NSString* sec0,*min0;
  77. sec0 = sec<10 ? @"0" : @"";
  78. min0 = min<10 ? @"0" : @"";
  79. self.countdownView.timeLabel.text = [NSString stringWithFormat:@"%@%lu分%@%lu秒",min0,(unsigned long)min,sec0,(unsigned long)sec];
  80. }
  81. #pragma mark - JXPagerViewDelegate
  82. - (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView {
  83. return [UIView new];
  84. }
  85. - (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView {
  86. return 0;
  87. }
  88. - (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
  89. return (self.viewModel.exerciseType == RQExerciseType_Test || self.viewModel.exerciseType == RQExerciseType_Exam)? 58.f : 65.f;
  90. }
  91. - (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
  92. return (self.viewModel.exerciseType == RQExerciseType_Test || self.viewModel.exerciseType == RQExerciseType_Exam)? self.countdownView : self.customPinView;
  93. }
  94. - (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView {
  95. //和categoryView的item数量一致
  96. return self.categoryView.titles.count;
  97. }
  98. - (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index {
  99. RQExerciseSubViewModel *exerciseSubViewModel = [[RQExerciseSubViewModel alloc] initWithServices:self.viewModel.services params:@{
  100. RQExerciseTypeKey : (self.viewModel.exerciseType == RQExerciseType_Test)? @(RQExerciseType_Test) : ((self.viewModel.exerciseType == RQExerciseType_Exam)? @(RQExerciseType_Exam) : @(index)),
  101. RQHomePageCarTypeKey : @(self.viewModel.homePageCarType),
  102. RQHomePageSubjectTypeKey : @(self.viewModel.homePageSubjectType),
  103. RQHomeSubPageTypeKey : @(self.viewModel.homeSubPageType),
  104. RQHomeSubEditListTypeKey : @(self.viewModel.homeSubEditListType),
  105. RQViewModelIDKey : self.viewModel.titleStr? : @"",
  106. RQViewCommonValueKey : @(self.viewModel.number),
  107. RQViewModelUtilKey : self.viewModel.questionArr? : @[],
  108. }];
  109. exerciseSubViewModel.title = self.viewModel.title;
  110. RQExerciseSubViewController *exerciseSubViewController = [[RQExerciseSubViewController alloc] initWithViewModel:exerciseSubViewModel];
  111. RAC(exerciseSubViewController, count) = RACObserve(self, count);
  112. __weak typeof(exerciseSubViewController) weakController = exerciseSubViewController;
  113. return weakController;
  114. }
  115. #pragma mark - JXCategoryViewDelegate
  116. - (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
  117. [RQ_Exercise_Module cancleAutoReadQuestion];
  118. self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
  119. //根据选中的下标,实时更新currentListView
  120. RQExerciseSubViewController *list = (RQExerciseSubViewController *)self.pagerView.listContainerView.validListDict[@(index)];
  121. [list rq_setup];
  122. if (list.questionArr.count > 0) {
  123. if (list.number < list.questionArr.count) {
  124. RQ_Exercise_Module.currrentExerciseModel = list.questionArr[list.number];
  125. }
  126. }
  127. }
  128. #pragma mark - JXPagerMainTableViewGestureDelegate
  129. - (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  130. //禁止categoryView左右滑动的时候,上下和左右都可以滚动
  131. if (otherGestureRecognizer == self.categoryView.collectionView.panGestureRecognizer) {
  132. return NO;
  133. }
  134. return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
  135. }
  136. #pragma mark - LazyLoad
  137. - (JXPagerView *)pagerView {
  138. if (!_pagerView) {
  139. _pagerView = [[JXPagerView alloc] initWithDelegate:self];
  140. _pagerView.mainTableView.gestureDelegate = self;
  141. _pagerView.isListHorizontalScrollEnabled = NO;
  142. _pagerView.mainTableView.bounces = NO;
  143. }
  144. return _pagerView;
  145. }
  146. - (JXCategoryTitleBackgroundView *)categoryView {
  147. if (!_categoryView) {
  148. CGFloat autoReadBtnWidth = (RQ_SCREEN_WIDTH) / 4.f + 8;
  149. _categoryView = [[JXCategoryTitleBackgroundView alloc] initWithFrame:CGRectMake(0, 0, RQ_SCREEN_WIDTH - autoReadBtnWidth, 55)];
  150. _categoryView.delegate = self;
  151. _categoryView.titles = self.titles;
  152. _categoryView.titleSelectedColor = UIColor.whiteColor;
  153. _categoryView.titleColor = UIColor.whiteColor;
  154. _categoryView.titleFont = RQRegularFont(15);
  155. _categoryView.titleSelectedFont = RQRegularFont(15);
  156. _categoryView.titleNumberOfLines = 0;
  157. _categoryView.contentScrollViewClickTransitionAnimationEnabled = NO;
  158. _categoryView.borderLineWidth = 0;
  159. _categoryView.selectedBackgroundColor = RQ_MAIN_COLOR;
  160. _categoryView.normalBackgroundColor = RQ_BACKGROUNDCOLOR_3;
  161. _categoryView.contentEdgeInsetLeft = 16;
  162. _categoryView.contentEdgeInsetRight = 16;
  163. _categoryView.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  164. _categoryView.cellSpacing = 8;
  165. _categoryView.cellWidth = autoReadBtnWidth - (3 * 8);
  166. _categoryView.backgroundHeight = 30;
  167. _categoryView.backgroundWidth = _categoryView.cellWidth;
  168. _categoryView.backgroundCornerRadius = 15;
  169. _categoryView.defaultSelectedIndex = 0;
  170. /// !!!: 将列表容器视图关联到 categoryView
  171. _categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagerView.listContainerView;
  172. self.autoReadBtn.frame = CGRectMake(_categoryView.width, 12.5, autoReadBtnWidth - 16, 30);
  173. [self.customPinView addSubview:_autoReadBtn];
  174. }
  175. return _categoryView;
  176. }
  177. - (NSArray<NSString *> *)titles {
  178. return (self.viewModel.exerciseType == RQExerciseType_Test || self.viewModel.exerciseType == RQExerciseType_Exam)? @[@""] : @[@"顺序练习", @"随机练习", @"背题模式"];
  179. }
  180. - (UIButton *)autoReadBtn {
  181. if (!_autoReadBtn) {
  182. _autoReadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  183. [_autoReadBtn setImage:RQImageNamed(@"autoRead") withTitle:@"自动读题" textColor:UIColor.whiteColor Font:15 fotState:UIControlStateNormal];
  184. [_autoReadBtn setImage:RQImageNamed(@"stopRead") withTitle:@"停止读题" textColor:UIColor.whiteColor Font:15 fotState:UIControlStateSelected];
  185. _autoReadBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
  186. [_autoReadBtn setBackgroundImage:[UIImage imageWithColor:RQ_MAIN_COLOR] forState:UIControlStateSelected];
  187. [_autoReadBtn setBackgroundImage:[UIImage imageWithColor:RQ_BACKGROUNDCOLOR_3] forState:UIControlStateNormal];
  188. [_autoReadBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleLeft imageTitleSpace:2];
  189. _autoReadBtn.layer.cornerRadius = 15;
  190. _autoReadBtn.clipsToBounds = YES;
  191. _autoReadBtn.userInteractionEnabled = YES;
  192. // @weakify(_autoReadBtn);
  193. [_autoReadBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  194. // @strongify(_autoReadBtn);
  195. if (RQ_Exercise_Module.isAutoRead) {
  196. [RQ_Exercise_Module cancleAutoReadQuestion];
  197. } else {
  198. RQ_Exercise_Module.isAutoRead = YES;
  199. }
  200. }];
  201. RAC(_autoReadBtn, selected) = RACObserve(RQ_Exercise_Module, isAutoRead);
  202. }
  203. return _autoReadBtn;
  204. }
  205. - (UIView *)customPinView {
  206. if (!_customPinView) {
  207. _customPinView = [[UIView alloc] init];
  208. [_customPinView addSubview:self.categoryView];
  209. UIImageView *lineImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:RQ_LIST_BACKGROUNDCOLOR size:CGSizeMake(RQ_SCREEN_WIDTH, 10)]];
  210. lineImageView.rq_y = 55;
  211. [_customPinView addSubview:lineImageView];
  212. }
  213. return _customPinView;
  214. }
  215. - (RQCountdownView *)countdownView {
  216. if (!_countdownView) {
  217. _countdownView = [RQCountdownView countdownView];
  218. }
  219. return _countdownView;
  220. }
  221. @end