RQExerciseViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. exerciseSettingViewModel.homeSubPageType = self.viewModel.homeSubPageType;
  131. RQExerciseSettingViewController *exerciseSettingViewController = [[RQExerciseSettingViewController alloc] initWithViewModel:exerciseSettingViewModel];
  132. [self presentPanModal:exerciseSettingViewController];
  133. }
  134. #pragma mark - JXPagerViewDelegate
  135. - (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView {
  136. return [UIView new];
  137. }
  138. - (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView {
  139. return 0.f;
  140. }
  141. - (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
  142. return 0.f;
  143. }
  144. - (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
  145. return [UIView new];
  146. }
  147. - (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView {
  148. //和categoryView的item数量一致
  149. return self.categoryView.titles.count;
  150. }
  151. - (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index {
  152. NSLog(@"index=%zd",index);
  153. // RQExerciseSubViewModel *exerciseSubViewModel = [[RQExerciseSubViewModel alloc] initWithServices:self.viewModel.services params:@{
  154. //// RQExerciseTypeKey : (self.viewModel.exerciseType == RQExerciseType_Exam)? @(RQExerciseType_Exam) : @(index),
  155. // RQHomePageCarTypeKey : @(self.viewModel.homePageCarType),
  156. // RQHomePageSubjectTypeKey : @(self.viewModel.homePageSubjectType),
  157. // RQHomeSubPageTypeKey : @(self.viewModel.homeSubPageType),
  158. // RQHomeSubEditListTypeKey : @(self.viewModel.homeSubEditListType),
  159. // RQViewModelIDKey : self.viewModel.titleStr? : @"",
  160. // RQViewCommonValueKey : @(self.viewModel.number),
  161. // RQViewModelUtilKey : self.viewModel.questionArr? : @[],
  162. // }];
  163. // RQExerciseSubViewController *exerciseSubViewController = [[RQExerciseSubViewController alloc] initWithViewModel:exerciseSubViewModel];
  164. // RAC(exerciseSubViewController, count) = RACObserve(self, count);
  165. RQExerciseSubViewController *exerciseSubViewController = self.exerciseSubViewController;
  166. return exerciseSubViewController;
  167. }
  168. #pragma mark - JXCategoryViewDelegate
  169. - (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
  170. self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
  171. if (index == 0) {
  172. if (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Recitation) {
  173. RQ_Exercise_Module.currentExerciseType = self.historyExerciseType;
  174. } else {
  175. self.historyExerciseType = RQ_Exercise_Module.currentExerciseType;
  176. RQ_Exercise_Module.currentExerciseType = RQExerciseType_Recitation;
  177. }
  178. } else {
  179. RQ_Exercise_Module.currentExerciseType = RQExerciseType_Recitation;
  180. }
  181. //根据选中的下标,实时更新currentListView
  182. // RQExerciseSubViewController *list = (RQExerciseSubViewController *)self.pagerView.listContainerView.validListDict[@(index)];
  183. // RQ_Exercise_Module.currrentExerciseModel = list.questionArr[list.number];
  184. }
  185. #pragma mark - JXPagerMainTableViewGestureDelegate
  186. - (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  187. if ([self checkIsNestContentScrollView:(UIScrollView *)gestureRecognizer.view] || [self checkIsNestContentScrollView:(UIScrollView *)otherGestureRecognizer.view]) {
  188. //如果交互的是嵌套的contentScrollView,证明在左右滑动,就不允许同时响应
  189. return NO;
  190. }
  191. return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
  192. }
  193. - (BOOL)checkIsNestContentScrollView:(UIScrollView *)scrollView {
  194. for (RQExerciseSubViewController *list in self.pagerView.validListDict.allValues) {
  195. if (list.contentScrollView == scrollView) {
  196. return YES;
  197. }
  198. }
  199. return NO;
  200. }
  201. #pragma mark - LazyLoad
  202. - (JXPagerView *)pagerView {
  203. if (!_pagerView) {
  204. _pagerView = [[JXPagerView alloc] initWithDelegate:self listContainerType:JXPagerListContainerType_CollectionView];
  205. _pagerView.mainTableView.gestureDelegate = self;
  206. _pagerView.isListHorizontalScrollEnabled = NO;
  207. _pagerView.mainTableView.bounces = NO;
  208. }
  209. return _pagerView;
  210. }
  211. - (UIView *)myTitleView {
  212. if (!_myTitleView) {
  213. CGFloat autoReadBtnWidth = RQ_FIT_HORIZONTAL(80.f);
  214. CGFloat autoReadBtnHeight = RQ_FIT_HORIZONTAL(28.f);
  215. _myTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, autoReadBtnWidth * 2, autoReadBtnHeight)];
  216. }
  217. return _myTitleView;
  218. }
  219. - (JXCategoryTitleView *)categoryView {
  220. if (!_categoryView) {
  221. CGFloat autoReadBtnWidth = RQ_FIT_HORIZONTAL(80.f);
  222. CGFloat autoReadBtnHeight = RQ_FIT_HORIZONTAL(28.f);
  223. _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, autoReadBtnWidth * 2, autoReadBtnHeight)];
  224. _categoryView.delegate = self;
  225. _categoryView.titles = self.titles;
  226. _categoryView.titleSelectedColor = UIColor.whiteColor;
  227. _categoryView.titleColor = RQ_MAIN_TEXT_COLOR_3;
  228. _categoryView.titleFont = RQRegularFont(13);
  229. _categoryView.titleSelectedFont = RQRegularFont(13);
  230. _categoryView.titleNumberOfLines = 0;
  231. _categoryView.layer.cornerRadius = autoReadBtnHeight / 2.f;
  232. _categoryView.layer.borderWidth = 1;
  233. _categoryView.layer.borderColor = RQColorFromHexString(@"#B8C0CC").CGColor;
  234. _categoryView.contentScrollViewClickTransitionAnimationEnabled = YES;
  235. _categoryView.backgroundColor = UIColor.whiteColor;
  236. _categoryView.contentEdgeInsetLeft = 0;
  237. _categoryView.contentEdgeInsetRight = 0;
  238. _categoryView.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  239. _categoryView.cellSpacing = 0;
  240. _categoryView.cellWidth = autoReadBtnWidth;
  241. JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
  242. backgroundView.indicatorHeight = autoReadBtnHeight;
  243. backgroundView.indicatorWidth = autoReadBtnWidth;
  244. backgroundView.indicatorWidthIncrement = 0;
  245. backgroundView.indicatorColor = RQ_MAIN_COLOR;
  246. _categoryView.indicators = @[backgroundView];
  247. _categoryView.defaultSelectedIndex = 0;
  248. /// !!!: 将列表容器视图关联到 categoryView
  249. _categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagerView.listContainerView;
  250. }
  251. return _categoryView;
  252. }
  253. - (NSArray<NSString *> *)titles {
  254. return (RQ_Exercise_Module.currentExerciseType == RQExerciseType_Exam)? @[@""] : @[@"答题模式", @"背题模式"];
  255. }
  256. - (RQExerciseSubViewController *)exerciseSubViewController{
  257. if(!_exerciseSubViewController){
  258. _exerciseSubViewController = [self createSubVc:0];
  259. }
  260. return _exerciseSubViewController;
  261. }
  262. - (RQExerciseSubViewController*)createSubVc:(NSInteger)index {
  263. RQExerciseSubViewModel *exerciseSubViewModel = [[RQExerciseSubViewModel alloc] initWithServices:self.viewModel.services params:@{
  264. // RQExerciseTypeKey : (self.viewModel.exerciseType == RQExerciseType_Exam)? @(RQExerciseType_Exam) : @(index),
  265. RQHomePageCarTypeKey : @(self.viewModel.homePageCarType),
  266. RQHomePageSubjectTypeKey : @(self.viewModel.homePageSubjectType),
  267. RQHomeSubPageTypeKey : @(self.viewModel.homeSubPageType),
  268. RQHomeSubEditListTypeKey : @(self.viewModel.homeSubEditListType),
  269. RQViewModelIDKey : self.viewModel.titleStr? : @"",
  270. RQViewCommonValueKey : @(self.viewModel.number),
  271. RQViewModelUtilKey : self.viewModel.questionArr? : @[],
  272. }];
  273. RQExerciseSubViewController *exerciseSubViewController = [[RQExerciseSubViewController alloc] initWithViewModel:exerciseSubViewModel];
  274. return exerciseSubViewController;
  275. }
  276. - (RQCountdownView *)countdownView {
  277. if (!_countdownView) {
  278. @weakify(self)
  279. _countdownView = [RQCountdownView countdownView];
  280. _countdownView.frame = CGRectMake(0, 0, RQ_SCREEN_WIDTH / 2.f, RQ_APPLICATION_NAV_BAR_HEIGHT);
  281. [_countdownView setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  282. @strongify(self)
  283. if (RQ_Exercise_Module.timer) {
  284. [RQ_Exercise_Module.timer setFireDate:[NSDate distantFuture]];
  285. NSDictionary *dic = @{
  286. @"questionArr" : self.viewModel.questionArr,
  287. };
  288. [RQ_Exercise_Module showAlertWithRQExerciseAlertType:RQExerciseAlertType_Exam_Suspend valueDic:dic confirmAction:^(__kindof QMUIDialogViewController *dialogViewController) {
  289. // @strongify(self);
  290. if (RQ_Exercise_Module.timer) {
  291. [RQ_Exercise_Module.timer setFireDate:[NSDate distantPast]];
  292. }
  293. } cancelAction:nil];
  294. }
  295. }];
  296. RAC(_countdownView.timeLabel, text) = [RACObserve(RQ_Exercise_Module, count) map:^id _Nullable(id _Nullable value) {
  297. NSUInteger sec = RQ_Exercise_Module.count%60;
  298. NSUInteger min = RQ_Exercise_Module.count/60;
  299. NSString* sec0,*min0;
  300. sec0 = sec<10 ? @"0" : @"";
  301. min0 = min<10 ? @"0" : @"";
  302. return [NSString stringWithFormat:@"%@%lu:%@%lu",min0,(unsigned long)min,sec0,(unsigned long)sec];
  303. }];
  304. }
  305. return _countdownView;
  306. }
  307. - (UIView *)myAdView {
  308. if (!_myAdView) {
  309. _myAdView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RQ_SCREEN_WIDTH, RQ_SCREEN_WIDTH / (640/100.0))];
  310. }
  311. return _myAdView;
  312. }
  313. @end