RQExerciseViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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(13) 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. }
  57. - (void)_timerValueChanged:(YYTimer *)timer {
  58. self.count--;
  59. if (self.count == 0) {
  60. [timer invalidate];
  61. self.timer = nil;
  62. // [self _resetCaptcha];
  63. return;
  64. }
  65. NSUInteger sec = _count%60;
  66. NSUInteger min = _count/60;
  67. NSString* sec0,*min0;
  68. sec0 = sec<10 ? @"0" : @"";
  69. min0 = min<10 ? @"0" : @"";
  70. self.countdownView.timeLabel.text = [NSString stringWithFormat:@"%@%lu分%@%lu秒",min0,(unsigned long)min,sec0,(unsigned long)sec];
  71. }
  72. #pragma mark - JXPagerViewDelegate
  73. - (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView {
  74. return [UIView new];
  75. }
  76. - (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView {
  77. return 0;
  78. }
  79. - (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
  80. return (self.viewModel.exerciseType == RQExerciseType_Test || self.viewModel.exerciseType == RQExerciseType_Exam)? 58.f : 65.f;
  81. }
  82. - (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
  83. return (self.viewModel.exerciseType == RQExerciseType_Test || self.viewModel.exerciseType == RQExerciseType_Exam)? self.countdownView : self.customPinView;
  84. }
  85. - (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView {
  86. //和categoryView的item数量一致
  87. return self.categoryView.titles.count;
  88. }
  89. - (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index {
  90. RQExerciseSubViewModel *exerciseSubViewModel = [[RQExerciseSubViewModel alloc] initWithServices:self.viewModel.services params:@{
  91. RQExerciseTypeKey : (self.viewModel.exerciseType == RQExerciseType_Test)? @(RQExerciseType_Test) : ((self.viewModel.exerciseType == RQExerciseType_Exam)? @(RQExerciseType_Exam) : @(index)),
  92. RQHomePageCarTypeKey : @(self.viewModel.homePageCarType),
  93. RQHomePageSubjectTypeKey : @(self.viewModel.homePageSubjectType),
  94. RQHomeSubPageTypeKey : @(self.viewModel.homeSubPageType),
  95. RQHomeSubEditListTypeKey : @(self.viewModel.homeSubEditListType),
  96. RQViewModelIDKey : self.viewModel.titleStr? : @"",
  97. RQViewCommonValueKey : @(self.viewModel.number),
  98. RQViewModelUtilKey : self.viewModel.questionArr? : @[],
  99. }];
  100. RQExerciseSubViewController *exerciseSubViewController = [[RQExerciseSubViewController alloc] initWithViewModel:exerciseSubViewModel];
  101. RAC(exerciseSubViewController, count) = RACObserve(self, count);
  102. return exerciseSubViewController;
  103. }
  104. #pragma mark - JXCategoryViewDelegate
  105. - (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
  106. [RQ_Exercise_Module cancleAutoReadQuestion];
  107. self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
  108. //根据选中的下标,实时更新currentListView
  109. RQExerciseSubViewController *list = (RQExerciseSubViewController *)self.pagerView.listContainerView.validListDict[@(index)];
  110. RQ_Exercise_Module.currrentExerciseModel = list.questionArr[list.number];
  111. }
  112. #pragma mark - JXPagerMainTableViewGestureDelegate
  113. - (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  114. //禁止categoryView左右滑动的时候,上下和左右都可以滚动
  115. if (otherGestureRecognizer == self.categoryView.collectionView.panGestureRecognizer) {
  116. return NO;
  117. }
  118. return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
  119. }
  120. #pragma mark - LazyLoad
  121. - (JXPagerView *)pagerView {
  122. if (!_pagerView) {
  123. _pagerView = [[JXPagerView alloc] initWithDelegate:self];
  124. _pagerView.mainTableView.gestureDelegate = self;
  125. _pagerView.isListHorizontalScrollEnabled = NO;
  126. _pagerView.mainTableView.bounces = NO;
  127. }
  128. return _pagerView;
  129. }
  130. - (JXCategoryTitleBackgroundView *)categoryView {
  131. if (!_categoryView) {
  132. CGFloat autoReadBtnWidth = (RQ_SCREEN_WIDTH) / 4.f + 8;
  133. _categoryView = [[JXCategoryTitleBackgroundView alloc] initWithFrame:CGRectMake(0, 0, RQ_SCREEN_WIDTH - autoReadBtnWidth, 55)];
  134. _categoryView.delegate = self;
  135. _categoryView.titles = self.titles;
  136. _categoryView.titleSelectedColor = UIColor.whiteColor;
  137. _categoryView.titleColor = UIColor.whiteColor;
  138. _categoryView.titleFont = RQRegularFont(13);
  139. _categoryView.titleSelectedFont = RQRegularFont(13);
  140. _categoryView.titleNumberOfLines = 0;
  141. _categoryView.contentScrollViewClickTransitionAnimationEnabled = NO;
  142. _categoryView.borderLineWidth = 0;
  143. _categoryView.selectedBackgroundColor = RQ_MAIN_COLOR;
  144. _categoryView.normalBackgroundColor = RQ_BACKGROUNDCOLOR_3;
  145. _categoryView.contentEdgeInsetLeft = 16;
  146. _categoryView.contentEdgeInsetRight = 16;
  147. _categoryView.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  148. _categoryView.cellSpacing = 8;
  149. _categoryView.cellWidth = autoReadBtnWidth - (3 * 8);
  150. _categoryView.backgroundHeight = 25;
  151. _categoryView.backgroundWidth = _categoryView.cellWidth;
  152. _categoryView.backgroundCornerRadius = 12.5;
  153. _categoryView.defaultSelectedIndex = 0;
  154. /// !!!: 将列表容器视图关联到 categoryView
  155. _categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagerView.listContainerView;
  156. self.autoReadBtn.frame = CGRectMake(_categoryView.width, 15, autoReadBtnWidth - 16, 25);
  157. [self.customPinView addSubview:_autoReadBtn];
  158. }
  159. return _categoryView;
  160. }
  161. - (NSArray<NSString *> *)titles {
  162. return (self.viewModel.exerciseType == RQExerciseType_Test || self.viewModel.exerciseType == RQExerciseType_Exam)? @[@""] : @[@"顺序练习", @"随机练习", @"背题模式"];
  163. }
  164. - (UIButton *)autoReadBtn {
  165. if (!_autoReadBtn) {
  166. _autoReadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  167. [_autoReadBtn setImage:RQImageNamed(@"autoRead") withTitle:@"自动读题" textColor:UIColor.whiteColor Font:13 fotState:UIControlStateNormal];
  168. [_autoReadBtn setImage:RQImageNamed(@"stopRead") withTitle:@"停止读题" textColor:UIColor.whiteColor Font:13 fotState:UIControlStateSelected];
  169. _autoReadBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
  170. [_autoReadBtn setBackgroundImage:[UIImage imageWithColor:RQ_MAIN_COLOR] forState:UIControlStateSelected];
  171. [_autoReadBtn setBackgroundImage:[UIImage imageWithColor:RQ_BACKGROUNDCOLOR_3] forState:UIControlStateNormal];
  172. [_autoReadBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleLeft imageTitleSpace:2];
  173. _autoReadBtn.layer.cornerRadius = 12.5;
  174. _autoReadBtn.clipsToBounds = YES;
  175. _autoReadBtn.userInteractionEnabled = YES;
  176. // @weakify(_autoReadBtn);
  177. [_autoReadBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  178. // @strongify(_autoReadBtn);
  179. if (RQ_Exercise_Module.isAutoRead) {
  180. [RQ_Exercise_Module cancleAutoReadQuestion];
  181. } else {
  182. RQ_Exercise_Module.isAutoRead = YES;
  183. }
  184. }];
  185. RAC(_autoReadBtn, selected) = RACObserve(RQ_Exercise_Module, isAutoRead);
  186. }
  187. return _autoReadBtn;
  188. }
  189. - (UIView *)customPinView {
  190. if (!_customPinView) {
  191. _customPinView = [[UIView alloc] init];
  192. [_customPinView addSubview:self.categoryView];
  193. UIImageView *lineImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:RQ_LIST_BACKGROUNDCOLOR size:CGSizeMake(RQ_SCREEN_WIDTH, 10)]];
  194. lineImageView.rq_y = 55;
  195. [_customPinView addSubview:lineImageView];
  196. }
  197. return _customPinView;
  198. }
  199. - (RQCountdownView *)countdownView {
  200. if (!_countdownView) {
  201. _countdownView = [RQCountdownView countdownView];
  202. }
  203. return _countdownView;
  204. }
  205. @end