RQCollectViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // RQCollectViewController.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/8/3.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "RQCollectViewController.h"
  9. @interface RQCollectViewController ()
  10. /// viewModel
  11. @property (nonatomic, readonly, strong) RQCollectViewModel *viewModel;
  12. @property (nonatomic, readwrite, strong) RQErrorAndCollectHeaderView *collectHeaderView;
  13. @property (nonatomic, readwrite, strong) QMUIButton *clearBtn;
  14. @end
  15. @implementation RQCollectViewController
  16. @dynamic viewModel;
  17. #pragma mark - SystemMethod
  18. - (void)viewDidLoad {
  19. @weakify(self)
  20. [super viewDidLoad];
  21. self.tableView.tableHeaderView = self.collectHeaderView;
  22. self.tableView.backgroundColor = UIColor.clearColor;
  23. [self.view addSubview:self.clearBtn];
  24. [[RACObserve(self.viewModel, dataSource) deliverOnMainThread] subscribeNext:^(NSArray *dataSource) {
  25. @strongify(self)
  26. NSUInteger count = dataSource.count;
  27. if (count == 0) {
  28. // [self showEmptyViewWithImage:RQImageNamed(@"自动收藏") text:@"您收藏的题目会自动记录到这里哟~" detailText:nil buttonTitle:nil buttonAction:nil];
  29. [self.emptyView setImage:RQImageNamed(@"自动收藏")];
  30. [self.emptyView setLoadingViewHidden:YES];
  31. [self.emptyView setTextLabelText:@"您收藏的题目会自动记录到这里哟~"];
  32. [self.tableView addSubview:self.emptyView];
  33. [self.tableView sendSubviewToBack:self.emptyView];
  34. CGFloat a = 8.f + RQ_FIT_HORIZONTAL(214.f) + 4.f + RQ_FIT_HORIZONTAL(40.f) + 20.f + 10.f;
  35. self.emptyView.frame = CGRectMake(0, a / 2.f, RQ_SCREEN_WIDTH, RQ_SCREEN_HEIGHT);
  36. [self.clearBtn setTitleNormal:@"去练习"];
  37. } else {
  38. [self hideEmptyView];
  39. [self.clearBtn setTitleNormal:@"清空我的收藏"];
  40. }
  41. }];
  42. [[[RQNotificationCenter rac_addObserverForName:RQCancelCollectNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * _Nullable x) {
  43. @strongify(self);
  44. self.collectHeaderView.allNumLabel.text = [NSString qmui_stringWithNSInteger:[RQ_YDTQuestion_Module getQuestionWithExerciseType:RQExerciseType_Collect].count];
  45. }];
  46. }
  47. - (void)viewDidLayoutSubviews {
  48. [super viewDidLayoutSubviews];
  49. self.tableView.frame = CGRectMake(0, 0, RQ_SCREEN_WIDTH, RQ_SCREEN_HEIGHT - RQ_APPLICATION_STATUS_BAR_HEIGHT - RQ_APPLICATION_NAV_BAR_HEIGHT - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT - RQ_FIT_HORIZONTAL(40.f) - 30.f - 16.f);
  50. self.collectHeaderView.size = CGSizeMake(RQ_SCREEN_WIDTH, 8.f + RQ_FIT_HORIZONTAL(214.f) + 4.f + RQ_FIT_HORIZONTAL(40.f) + 20.f + 10.f);
  51. self.clearBtn.frame = CGRectMake(16, RQ_SCREEN_HEIGHT - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT - RQ_APPLICATION_STATUS_BAR_HEIGHT - RQ_APPLICATION_NAV_BAR_HEIGHT - 30.f - RQ_FIT_HORIZONTAL(40.f), RQ_SCREEN_WIDTH - 32.f, RQ_FIT_HORIZONTAL(40.f));
  52. self.clearBtn.layer.cornerRadius = RQ_FIT_HORIZONTAL(40.f) / 2.f;
  53. }
  54. - (void)clearBtnAction {
  55. // @weakify(self)
  56. if (self.viewModel.dataSource.count == 0) {
  57. [self.viewModel.services popToRootViewModelAnimated:YES];
  58. } else {
  59. [MBProgressHUD rq_showProgressHUD:@"删除中..."];
  60. [[RQ_HTTP_Service deleteQuestionFavAllWithType:RQSaveFavQuestionType_Collect] subscribeNext:^(id _Nullable x) {
  61. // @strongify(self)
  62. [MBProgressHUD rq_hideHUD];
  63. [MBProgressHUD rq_showTips:@"删除成功!"];
  64. [RQ_YDT_USER_Question_Module deleteAllCollectRecordWithCarType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject];
  65. [RQNotificationCenter postNotificationName:RQCancelCollectNotification object:nil userInfo:@{}];
  66. } error:^(NSError * _Nullable error) {
  67. [MBProgressHUD rq_hideHUD];
  68. [MBProgressHUD rq_showErrorTips:error];
  69. }];
  70. }
  71. }
  72. #pragma mark - OverrideMethods
  73. - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
  74. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section];
  75. RQCommonCollectionItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row];
  76. return [NSClassFromString(itemViewModel.itemClassName) cellWithTableView:tableView];
  77. }
  78. - (void)configureCell:(RQCommonCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
  79. [cell bindViewModel:object];
  80. }
  81. #pragma mark - JXPagerSmoothViewListViewDelegate
  82. - (UIView *)listView {
  83. return self.view;
  84. }
  85. - (UIScrollView *)listScrollView {
  86. return self.tableView;
  87. }
  88. #pragma mark - LazyLoad
  89. - (RQErrorAndCollectHeaderView *)collectHeaderView {
  90. if (!_collectHeaderView) {
  91. _collectHeaderView = [RQErrorAndCollectHeaderView errorAndCollectHeaderView];
  92. _collectHeaderView.bottomView.hidden = YES;
  93. [_collectHeaderView.todayErrorBtn setTitleNormal:@"今日收藏"];
  94. [_collectHeaderView.allErrorBtn setTitleNormal:@"全部收藏"];
  95. _collectHeaderView.titleLabel.text = @"累计收藏";
  96. _collectHeaderView.allNumLabel.text = [NSString qmui_stringWithNSInteger:[RQ_YDTQuestion_Module getQuestionWithExerciseType:RQExerciseType_Collect].count];
  97. [_collectHeaderView.todayErrorBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  98. NSArray *todayArr = [RQ_YDTQuestion_Module getQuestionWithExerciseType:RQExerciseType_Collect];
  99. NSArray *arr = [todayArr.rac_sequence.signal map:^id _Nullable(RQYDTQuestionModel *ydtQuestionModel) {
  100. return [RQExerciseModel exerciseModelWithRQYDTQuestionModel:ydtQuestionModel];
  101. }].toArray;
  102. RQExerciseViewModel *exerciseViewModel = [[RQExerciseViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
  103. RQHomePageCarTypeKey : @(RQ_YDTQuestion_Module.carType),
  104. RQHomePageSubjectTypeKey : @(RQ_YDTQuestion_Module.subject),
  105. RQHomeSubPageTypeKey : @(RQHomeSubPageType_WrongTopicAndCollection),
  106. RQViewModelIDKey : @"今日收藏",
  107. RQExerciseTypeKey : @(RQExerciseType_Collect),
  108. RQViewModelUtilKey : arr,
  109. }];
  110. [RQ_APPDELEGATE.services pushViewModel:exerciseViewModel animated:YES];
  111. }];
  112. [_collectHeaderView.allErrorBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  113. NSArray *allArr = [RQ_YDTQuestion_Module getQuestionWithExerciseType:RQExerciseType_Collect];
  114. NSArray *arr = [allArr.rac_sequence.signal map:^id _Nullable(RQYDTQuestionModel *ydtQuestionModel) {
  115. return [RQExerciseModel exerciseModelWithRQYDTQuestionModel:ydtQuestionModel];
  116. }].toArray;
  117. RQExerciseViewModel *exerciseViewModel = [[RQExerciseViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
  118. RQHomePageCarTypeKey : @(RQ_YDTQuestion_Module.carType),
  119. RQHomePageSubjectTypeKey : @(RQ_YDTQuestion_Module.subject),
  120. RQHomeSubPageTypeKey : @(RQHomeSubPageType_WrongTopicAndCollection),
  121. RQViewModelIDKey : @"全部收藏",
  122. RQExerciseTypeKey : @(RQExerciseType_Collect),
  123. RQViewModelUtilKey : arr,
  124. }];
  125. [RQ_APPDELEGATE.services pushViewModel:exerciseViewModel animated:YES];
  126. }];
  127. }
  128. return _collectHeaderView;
  129. }
  130. - (QMUIButton *)clearBtn {
  131. if (!_clearBtn) {
  132. _clearBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  133. [_clearBtn addTarget:self action:@selector(clearBtnAction) forControlEvents:UIControlEventTouchUpInside];
  134. [_clearBtn setTitleNormal:@"清空我的收藏"];
  135. [_clearBtn setTitleColor:RQ_MAIN_BACKGROUNDCOLOR forState:UIControlStateNormal];
  136. [_clearBtn setBackgroundColor:RQ_MAIN_COLOR];
  137. _clearBtn.layer.masksToBounds = YES;
  138. }
  139. return _clearBtn;
  140. }
  141. @end