RQExerciseViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. //
  2. // RQExerciseViewController.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2021/8/12.
  6. //
  7. #import "RQExerciseViewController.h"
  8. @interface RQExerciseViewController () <JXCategoryViewDelegate, JXPagerViewDelegate, JXPagerMainTableViewGestureDelegate>
  9. /// viewModel
  10. @property (nonatomic, readonly, strong) RQExerciseViewModel *viewModel;
  11. @property (nonatomic, readwrite, strong) JXPagerView *pagerView;
  12. @property (nonatomic, readwrite, strong) JXCategoryTitleView *categoryView;
  13. @property (nonatomic, readwrite, copy) NSArray <NSString *> *titles;
  14. @property (nonatomic, readwrite, copy) NSArray *beitiQuestionsArr;
  15. @property (nonatomic, readwrite, strong) RQCountdownView *countdownView;
  16. @property (nonatomic, readwrite, strong) UIView *myAdView;
  17. @property (nonatomic, readwrite, strong) UIView *myTitleView;
  18. @property (nonatomic, readwrite, assign) RQExerciseType historyExerciseType;
  19. //保存 vc
  20. @property (nonatomic, readwrite, strong) RQExerciseSubViewController *exerciseSubViewController;
  21. @end
  22. @implementation RQExerciseViewController
  23. @dynamic viewModel;
  24. #pragma mark - SystemMethod
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. /// 初始化
  28. [self rq_setup];
  29. [self rq_noti];
  30. }
  31. - (void)viewDidLayoutSubviews {
  32. [super viewDidLayoutSubviews];
  33. [self updateFrame];
  34. }
  35. - (void)updateFrame {
  36. self.myAdView.frame = CGRectMake(0, RQ_APPLICATION_NAV_BAR_HEIGHT + RQ_APPLICATION_STATUS_BAR_HEIGHT, RQ_SCREEN_WIDTH, (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Exam)? 0.f : (self.myAdView.hidden? 0.f : RQ_SCREEN_WIDTH / (640/100.0)));
  37. self.pagerView.frame = CGRectMake(0, CGRectGetMaxY(self.myAdView.frame), RQ_SCREEN_WIDTH, RQ_SCREEN_HEIGHT - (RQ_APPLICATION_NAV_BAR_HEIGHT + RQ_APPLICATION_STATUS_BAR_HEIGHT) - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT - ((RQ_Exercise_Module.currentExerciseType == RQExerciseType_Exam)? 0.f : (self.myAdView.hidden? 0.f : RQ_SCREEN_WIDTH / (640/100.0))));
  38. }
  39. - (void)viewWillAppear:(BOOL)animated {
  40. [super viewWillAppear:animated];
  41. if (RQ_Exercise_Module.timer) {
  42. [RQ_Exercise_Module.timer setFireDate:[NSDate distantPast]];
  43. }
  44. }
  45. - (void)viewWillDisappear:(BOOL)animated {
  46. [super viewWillDisappear:animated];
  47. if(!RQ_Exercise_Module.isShow_CatalogueView){
  48. if (RQ_Exercise_Module.timer) {
  49. [RQ_Exercise_Module.timer setFireDate:[NSDate distantFuture]];
  50. }
  51. }
  52. }
  53. - (void)viewDidDisappear:(BOOL)animated {
  54. [super viewDidDisappear:animated];
  55. if(!RQ_Exercise_Module.isShow_CatalogueView){
  56. self.categoryView.delegate = nil;
  57. // [self.myTitleView removeAllSubviews];
  58. // self.myTitleView = nil;
  59. self.pagerView = nil;
  60. [RQ_Exercise_Module.timer invalidate];
  61. RQ_Exercise_Module.timer = nil;
  62. RQ_Exercise_Module.count = 0;
  63. RQ_Exercise_Module.beginDate = nil;
  64. }
  65. }
  66. - (void)dealloc {
  67. }
  68. #pragma mark - PrivateMethods
  69. /// 初始化
  70. - (void)rq_setup {
  71. @weakify(self)
  72. /// set up ...
  73. if (RQ_Exercise_Module.currentExerciseType != RQExerciseType_Exam) {
  74. [self.view addSubview:self.myAdView];
  75. self.myAdView.hidden = YES;
  76. [RQ_AD_MANAGER loadAdWithAdType:RQADType_Banner customView:self.myAdView controller:self cycleSecound:RQ_COMMON_MANAGER.JSJP_APP_AD_CYCLE];
  77. [RQ_AD_MANAGER initCloseBlock:^(RQADDoType adDoType) {
  78. @strongify(self)
  79. if (adDoType == RQADDoType_Close || adDoType == RQADDoType_Faild) {
  80. self.myAdView.hidden = YES;
  81. } else if (adDoType == RQADDoType_Success) {
  82. self.myAdView.hidden = NO;
  83. }
  84. [self updateFrame];
  85. }];
  86. RAC(RQ_AD_MANAGER, bannerIsShow) = [[RACObserve(self.myAdView, hidden) map:^id _Nullable(id _Nullable value) {
  87. @strongify(self)
  88. return @(!self.myAdView.hidden);
  89. }] takeUntil:self.rac_willDeallocSignal];
  90. }
  91. [self.view addSubview:self.pagerView];
  92. self.navigationItem.rightBarButtonItem = [UIBarButtonItem rq_customItemWithTitle:@"" font:RQMediumFont(13) titleColor:RQ_MAIN_TEXT_COLOR_1 imageName:@"设置-黑" target:self selector:@selector(setAction) contentHorizontalAlignment:UIControlContentHorizontalAlignmentRight EdgeInsetsStyle:RQButtonEdgeInsetsStyleLeft space:4.f];
  93. RQ_Exercise_Module.count = (self.viewModel.homePageCarType == RQHomePageCarType_Motorcycle)? 30 * 60 : 45 * 60;
  94. [self.navigationItem setLeftBarButtonItems:@[[UIBarButtonItem rq_backItemWithTitle:@"" imageName:@"backIcon" target:self action:@selector(rq_back)]]];
  95. [[RACScheduler mainThreadScheduler] schedule:^{
  96. @strongify(self)
  97. if (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Exam) {
  98. self.navigationItem.titleView = self.countdownView;
  99. self.countdownView.countDownBtn.selected = YES;
  100. RAC(self.countdownView.countDownBtn, selected) = [RACObserve(RQ_Exercise_Module, timer) map:^id _Nullable(id _Nullable value) {
  101. if (RQ_Exercise_Module.timer) {
  102. return @(RQ_Exercise_Module.timer.isValid);
  103. } else {
  104. return @(NO);
  105. }
  106. }];
  107. [RQ_Exercise_Module startTimer];
  108. } else {
  109. self.navigationItem.titleView = self.myTitleView;
  110. [self.myTitleView addSubview:self.categoryView];
  111. }
  112. }];
  113. self.historyExerciseType = RQ_Exercise_Module.currentExerciseType;
  114. }
  115. - (void)rq_noti {
  116. // @weakify(self)
  117. }
  118. - (void)rq_back {
  119. if (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Exam) {
  120. [self.viewModel.services popToRootViewModelAnimated:YES];
  121. } else {
  122. [self.viewModel.services popViewModelAnimated:YES];
  123. }
  124. [NY_VOICE_MANAGER stopPayVoiceAction];//停止读题
  125. [NY_VOICE_MANAGER stopPayVoiceActionUrl];
  126. }
  127. - (void)setAction {
  128. RQ_Exercise_Module.isShow_CatalogueView = YES;
  129. RQExerciseSettingViewModel *exerciseSettingViewModel = [[RQExerciseSettingViewModel alloc] initWithServices:self.viewModel.services params:nil];
  130. RQExerciseSettingViewController *exerciseSettingViewController = [[RQExerciseSettingViewController alloc] initWithViewModel:exerciseSettingViewModel];
  131. [self presentPanModal:exerciseSettingViewController];
  132. }
  133. #pragma mark - JXPagerViewDelegate
  134. - (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView {
  135. return [UIView new];
  136. }
  137. - (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView {
  138. return 0.f;
  139. }
  140. - (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
  141. return 0.f;
  142. }
  143. - (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
  144. return [UIView new];
  145. }
  146. - (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView {
  147. //和categoryView的item数量一致
  148. return self.categoryView.titles.count;
  149. }
  150. - (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index {
  151. NSLog(@"index=%zd",index);
  152. // RQExerciseSubViewModel *exerciseSubViewModel = [[RQExerciseSubViewModel alloc] initWithServices:self.viewModel.services params:@{
  153. //// RQExerciseTypeKey : (self.viewModel.exerciseType == RQExerciseType_Exam)? @(RQExerciseType_Exam) : @(index),
  154. // RQHomePageCarTypeKey : @(self.viewModel.homePageCarType),
  155. // RQHomePageSubjectTypeKey : @(self.viewModel.homePageSubjectType),
  156. // RQHomeSubPageTypeKey : @(self.viewModel.homeSubPageType),
  157. // RQHomeSubEditListTypeKey : @(self.viewModel.homeSubEditListType),
  158. // RQViewModelIDKey : self.viewModel.titleStr? : @"",
  159. // RQViewCommonValueKey : @(self.viewModel.number),
  160. // RQViewModelUtilKey : self.viewModel.questionArr? : @[],
  161. // }];
  162. // RQExerciseSubViewController *exerciseSubViewController = [[RQExerciseSubViewController alloc] initWithViewModel:exerciseSubViewModel];
  163. // RAC(exerciseSubViewController, count) = RACObserve(self, count);
  164. RQExerciseSubViewController *exerciseSubViewController = self.exerciseSubViewController;
  165. return exerciseSubViewController;
  166. }
  167. #pragma mark - JXCategoryViewDelegate
  168. - (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
  169. self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
  170. if (index == 0) {
  171. if (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation) {
  172. RQ_Exercise_Module.currentExerciseType = self.historyExerciseType;
  173. } else {
  174. self.historyExerciseType = RQ_Exercise_Module.currentExerciseType;
  175. RQ_Exercise_Module.currentExerciseType = RQExerciseType_Recitation;
  176. }
  177. } else {
  178. RQ_Exercise_Module.currentExerciseType = RQExerciseType_Recitation;
  179. }
  180. //根据选中的下标,实时更新currentListView
  181. // RQExerciseSubViewController *list = (RQExerciseSubViewController *)self.pagerView.listContainerView.validListDict[@(index)];
  182. // RQ_Exercise_Module.currrentExerciseModel = list.questionArr[list.number];
  183. }
  184. #pragma mark - JXPagerMainTableViewGestureDelegate
  185. - (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  186. if ([self checkIsNestContentScrollView:(UIScrollView *)gestureRecognizer.view] || [self checkIsNestContentScrollView:(UIScrollView *)otherGestureRecognizer.view]) {
  187. //如果交互的是嵌套的contentScrollView,证明在左右滑动,就不允许同时响应
  188. return NO;
  189. }
  190. return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
  191. }
  192. - (BOOL)checkIsNestContentScrollView:(UIScrollView *)scrollView {
  193. for (RQExerciseSubViewController *list in self.pagerView.validListDict.allValues) {
  194. if (list.contentScrollView == scrollView) {
  195. return YES;
  196. }
  197. }
  198. return NO;
  199. }
  200. #pragma mark - LazyLoad
  201. - (JXPagerView *)pagerView {
  202. if (!_pagerView) {
  203. _pagerView = [[JXPagerView alloc] initWithDelegate:self listContainerType:JXPagerListContainerType_CollectionView];
  204. _pagerView.mainTableView.gestureDelegate = self;
  205. _pagerView.isListHorizontalScrollEnabled = NO;
  206. _pagerView.mainTableView.bounces = NO;
  207. }
  208. return _pagerView;
  209. }
  210. - (UIView *)myTitleView {
  211. if (!_myTitleView) {
  212. CGFloat autoReadBtnWidth = RQ_FIT_HORIZONTAL(80.f);
  213. CGFloat autoReadBtnHeight = RQ_FIT_HORIZONTAL(28.f);
  214. _myTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, autoReadBtnWidth * 2, autoReadBtnHeight)];
  215. }
  216. return _myTitleView;
  217. }
  218. - (JXCategoryTitleView *)categoryView {
  219. if (!_categoryView) {
  220. CGFloat autoReadBtnWidth = RQ_FIT_HORIZONTAL(80.f);
  221. CGFloat autoReadBtnHeight = RQ_FIT_HORIZONTAL(28.f);
  222. _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, autoReadBtnWidth * 2, autoReadBtnHeight)];
  223. _categoryView.delegate = self;
  224. _categoryView.titles = self.titles;
  225. _categoryView.titleSelectedColor = UIColor.whiteColor;
  226. _categoryView.titleColor = RQ_MAIN_TEXT_COLOR_3;
  227. _categoryView.titleFont = RQRegularFont(13);
  228. _categoryView.titleSelectedFont = RQRegularFont(13);
  229. _categoryView.titleNumberOfLines = 0;
  230. _categoryView.layer.cornerRadius = autoReadBtnHeight / 2.f;
  231. _categoryView.layer.borderWidth = 1;
  232. _categoryView.layer.borderColor = RQColorFromHexString(@"#B8C0CC").CGColor;
  233. _categoryView.contentScrollViewClickTransitionAnimationEnabled = YES;
  234. _categoryView.backgroundColor = UIColor.whiteColor;
  235. _categoryView.contentEdgeInsetLeft = 0;
  236. _categoryView.contentEdgeInsetRight = 0;
  237. _categoryView.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  238. _categoryView.cellSpacing = 0;
  239. _categoryView.cellWidth = autoReadBtnWidth;
  240. JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
  241. backgroundView.indicatorHeight = autoReadBtnHeight;
  242. backgroundView.indicatorWidth = autoReadBtnWidth;
  243. backgroundView.indicatorWidthIncrement = 0;
  244. backgroundView.indicatorColor = RQ_MAIN_COLOR;
  245. _categoryView.indicators = @[backgroundView];
  246. _categoryView.defaultSelectedIndex = 0;
  247. /// !!!: 将列表容器视图关联到 categoryView
  248. _categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagerView.listContainerView;
  249. }
  250. return _categoryView;
  251. }
  252. - (NSArray<NSString *> *)titles {
  253. return (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Exam)? @[@""] : @[@"答题模式", @"背题模式"];
  254. }
  255. - (RQExerciseSubViewController *)exerciseSubViewController{
  256. if(!_exerciseSubViewController){
  257. _exerciseSubViewController = [self createSubVc:0];
  258. }
  259. return _exerciseSubViewController;
  260. }
  261. - (RQExerciseSubViewController*)createSubVc:(NSInteger)index {
  262. RQExerciseSubViewModel *exerciseSubViewModel = [[RQExerciseSubViewModel alloc] initWithServices:self.viewModel.services params:@{
  263. // RQExerciseTypeKey : (self.viewModel.exerciseType == RQExerciseType_Exam)? @(RQExerciseType_Exam) : @(index),
  264. RQHomePageCarTypeKey : @(self.viewModel.homePageCarType),
  265. RQHomePageSubjectTypeKey : @(self.viewModel.homePageSubjectType),
  266. RQHomeSubPageTypeKey : @(self.viewModel.homeSubPageType),
  267. RQHomeSubEditListTypeKey : @(self.viewModel.homeSubEditListType),
  268. RQViewModelIDKey : self.viewModel.titleStr? : @"",
  269. RQViewCommonValueKey : @(self.viewModel.number),
  270. RQViewModelUtilKey : self.viewModel.questionArr? : @[],
  271. }];
  272. RQExerciseSubViewController *exerciseSubViewController = [[RQExerciseSubViewController alloc] initWithViewModel:exerciseSubViewModel];
  273. return exerciseSubViewController;
  274. }
  275. - (RQCountdownView *)countdownView {
  276. if (!_countdownView) {
  277. @weakify(self)
  278. _countdownView = [RQCountdownView countdownView];
  279. _countdownView.frame = CGRectMake(0, 0, RQ_SCREEN_WIDTH / 2.f, RQ_APPLICATION_NAV_BAR_HEIGHT);
  280. [_countdownView setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  281. @strongify(self)
  282. if (RQ_Exercise_Module.timer) {
  283. [RQ_Exercise_Module.timer setFireDate:[NSDate distantFuture]];
  284. NSDictionary *dic = @{
  285. @"questionArr" : self.viewModel.questionArr,
  286. };
  287. [RQ_Exercise_Module showAlertWithRQExerciseAlertType:RQExerciseAlertType_Exam_Suspend valueDic:dic confirmAction:^(__kindof QMUIDialogViewController *dialogViewController) {
  288. // @strongify(self);
  289. if (RQ_Exercise_Module.timer) {
  290. [RQ_Exercise_Module.timer setFireDate:[NSDate distantPast]];
  291. }
  292. } cancelAction:nil];
  293. }
  294. }];
  295. RAC(_countdownView.timeLabel, text) = [RACObserve(RQ_Exercise_Module, count) map:^id _Nullable(id _Nullable value) {
  296. NSUInteger sec = RQ_Exercise_Module.count%60;
  297. NSUInteger min = RQ_Exercise_Module.count/60;
  298. NSString* sec0,*min0;
  299. sec0 = sec<10 ? @"0" : @"";
  300. min0 = min<10 ? @"0" : @"";
  301. return [NSString stringWithFormat:@"%@%lu:%@%lu",min0,(unsigned long)min,sec0,(unsigned long)sec];
  302. }];
  303. }
  304. return _countdownView;
  305. }
  306. - (UIView *)myAdView {
  307. if (!_myAdView) {
  308. _myAdView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RQ_SCREEN_WIDTH, RQ_SCREEN_WIDTH / (640/100.0))];
  309. }
  310. return _myAdView;
  311. }
  312. @end