RQExerciseViewController.m 16 KB

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