RQCollectViewModel.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // RQCollectViewModel.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/8/3.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "RQCollectViewModel.h"
  9. @interface RQCollectViewModel ()
  10. /// 数组
  11. @property (nonatomic, readwrite, copy) NSArray *items;
  12. @end
  13. @implementation RQCollectViewModel
  14. #pragma mark - Public Method
  15. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params {
  16. if (self = [super initWithServices:services params:params]) {
  17. }
  18. return self;
  19. }
  20. - (void)initialize {
  21. [super initialize];
  22. @weakify(self);
  23. self.title = @"";
  24. self.prefersNavigationBarBottomLineHidden = YES;
  25. ///配置数据
  26. [self rq_configureData];
  27. [[[RQNotificationCenter rac_addObserverForName:RQCancelCollectNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * _Nullable x) {
  28. @strongify(self);
  29. [self.requestRemoteDataCommand execute:nil];
  30. }];
  31. }
  32. #pragma mark - PrivateMethod
  33. - (void)rq_configureData {
  34. @weakify(self);
  35. /// 列表
  36. RAC(self, items) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
  37. /// 数据源
  38. RAC(self, dataSource) = [RACObserve(self, items) map:^(NSArray * items) {
  39. @strongify(self)
  40. return RQObjectIsNil(items)? @[] : [self dataSourceWithItems:items];
  41. }];
  42. }
  43. /// 请求数据
  44. - (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page {
  45. NSArray * (^itemsBlock)(NSArray *) = ^(NSArray *products) {
  46. if (page == 1 || page == 0) {
  47. /// 下拉刷新
  48. } else {
  49. /// 上拉加载
  50. products = @[(self.items ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array;
  51. }
  52. return products;
  53. };
  54. ///
  55. return [[[RQ_HTTP_Service getChapterList] map:itemsBlock] doError:^(NSError * _Nonnull error) {
  56. [MBProgressHUD rq_showErrorTips:error];
  57. }];
  58. }
  59. - (NSArray *)dataSourceWithItems:(NSArray *)items {
  60. __block NSInteger index = 1;
  61. NSArray *viewModels = [[items.rac_sequence filter:^BOOL(RQDictInfoModel *dictInfoModel) {
  62. return [RQ_YDTQuestion_Module getChapterQuestionWithChapterName:dictInfoModel.dictValue exerciseType:RQExerciseType_Collect].count > 0;
  63. }].array.rac_sequence map:^(RQDictInfoModel *dictInfoModel) {
  64. RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel];
  65. NSArray *chapterArr = [RQ_YDTQuestion_Module getChapterQuestionWithChapterName:dictInfoModel.dictValue exerciseType:RQExerciseType_Collect];
  66. RQErrorAndCollectItemViewModel *errorAndCollectItemViewModel = [[RQErrorAndCollectItemViewModel alloc] initWithNumStr:[NSString qmui_stringWithNSInteger:index] titleStr:dictInfoModel.dictValue countStr:[NSString qmui_stringWithNSInteger:chapterArr.count]];
  67. index ++;
  68. errorAndCollectItemViewModel.operation = ^{
  69. NSArray *arr = [chapterArr.rac_sequence.signal map:^id _Nullable(RQYDTQuestionModel *ydtQuestionModel) {
  70. return [RQExerciseModel exerciseModelWithRQYDTQuestionModel:ydtQuestionModel];
  71. }].toArray;
  72. RQExerciseViewModel *exerciseViewModel = [[RQExerciseViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
  73. RQHomePageCarTypeKey : @(RQ_YDTQuestion_Module.carType),
  74. RQHomePageSubjectTypeKey : @(RQ_YDTQuestion_Module.subject),
  75. RQHomeSubPageTypeKey : @(RQHomeSubPageType_WrongTopicAndCollection),
  76. RQViewModelIDKey : dictInfoModel.dictValue,
  77. RQExerciseTypeKey : @(RQExerciseType_Collect),
  78. RQViewModelUtilKey : arr,
  79. }];
  80. [RQ_APPDELEGATE.services pushViewModel:exerciseViewModel animated:YES];
  81. };
  82. group.itemViewModels = @[errorAndCollectItemViewModel];
  83. return group;
  84. }].array;
  85. return viewModels ?: @[];
  86. }
  87. @end