RQChapterAndPointListViewModel.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // RQChapterAndPointListViewModel.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/7/26.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "RQChapterAndPointListViewModel.h"
  9. @interface RQChapterAndPointListViewModel ()
  10. /// 数组
  11. @property (nonatomic, readwrite, copy) NSArray *items;
  12. @end
  13. @implementation RQChapterAndPointListViewModel
  14. #pragma mark - Public Method
  15. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params {
  16. if (self = [super initWithServices:services params:params]) {
  17. self.title = params[RQViewModelTitleKey];
  18. }
  19. return self;
  20. }
  21. - (void)initialize {
  22. [super initialize];
  23. ///配置数据
  24. if ([self.title isEqualToString:@"章节练习"]) {
  25. [self rq_configureData_Chapter];
  26. } else {
  27. [self rq_configureData_Point];
  28. }
  29. }
  30. #pragma mark - PrivateMethod
  31. - (void)rq_configureData_Chapter {
  32. @weakify(self);
  33. /// 列表
  34. RAC(self, items) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
  35. /// 数据源
  36. RAC(self, dataSource) = [RACObserve(self, items) map:^(NSArray * items) {
  37. @strongify(self)
  38. return RQObjectIsNil(items)? @[] : [self dataSourceWithItems:items];
  39. }];
  40. }
  41. - (void)rq_configureData_Point {
  42. /// 数据源
  43. RAC(self, dataSource) = [RACObserve(self, items) map:^(NSArray * items) {
  44. return [items.rac_sequence.signal map:^id _Nullable(RQYDTPointModel *pointModel) {
  45. RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel];
  46. RQCommonItemViewModel *commonItemViewModel = [RQCommonItemViewModel itemViewModelWithTitle:pointModel.name];
  47. NSArray *pointArr = [RQ_YDTQuestion_Module getPointQuestionWithPointID:pointModel.id];
  48. commonItemViewModel.subtitle = [NSString qmui_stringWithNSInteger:pointArr.count];
  49. commonItemViewModel.rowHeight = RQ_FIT_HORIZONTAL(54.f);
  50. commonItemViewModel.operation = ^{
  51. NSArray *arr = [pointArr.rac_sequence.signal map:^id _Nullable(RQYDTQuestionModel *ydtQuestionModel) {
  52. return [RQExerciseModel exerciseModelWithRQYDTQuestionModel:ydtQuestionModel];
  53. }].toArray;
  54. RQExerciseViewModel *exerciseViewModel = [[RQExerciseViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
  55. RQHomePageCarTypeKey : @(RQ_YDTQuestion_Module.carType),
  56. RQHomePageSubjectTypeKey : @(RQ_YDTQuestion_Module.subject),
  57. RQHomeSubPageTypeKey : @(RQHomeSubPageType_SequentialPractice),
  58. RQViewModelIDKey : pointModel.name,
  59. RQExerciseTypeKey : @(RQExerciseType_Point),
  60. RQViewModelUtilKey : arr,
  61. }];
  62. RQ_Exercise_Module.otherStr = pointModel.name;
  63. [RQ_APPDELEGATE.services pushViewModel:exerciseViewModel animated:YES];
  64. };
  65. group.itemViewModels = @[commonItemViewModel];
  66. return group;
  67. }].toArray;
  68. }];
  69. self.items = [RQ_YDTQuestion_Module getPoint];
  70. }
  71. /// 请求数据
  72. - (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page {
  73. NSArray * (^itemsBlock)(NSArray *) = ^(NSArray *products) {
  74. if (page == 1 || page == 0) {
  75. /// 下拉刷新
  76. } else {
  77. /// 上拉加载
  78. products = @[(self.items ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array;
  79. }
  80. return products;
  81. };
  82. ///
  83. return [[[RQ_HTTP_Service getChapterList] map:itemsBlock] doError:^(NSError * _Nonnull error) {
  84. [MBProgressHUD rq_showErrorTips:error];
  85. }];
  86. }
  87. #pragma mark - 辅助方法
  88. - (NSArray *)dataSourceWithItems:(NSArray *)items {
  89. NSArray *viewModels = [items.rac_sequence map:^(RQDictInfoModel *dictInfoModel) {
  90. RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel];
  91. RQCommonItemViewModel *commonItemViewModel = [RQCommonItemViewModel itemViewModelWithTitle:dictInfoModel.dictValue];
  92. commonItemViewModel.subtitle = dictInfoModel.remark;
  93. commonItemViewModel.rowHeight = RQ_FIT_HORIZONTAL(54.f);
  94. NSArray *chapterArr = [RQ_YDTQuestion_Module getChapterQuestionWithChapterName:dictInfoModel.dictValue];
  95. commonItemViewModel.operation = ^{
  96. NSArray *arr = [chapterArr.rac_sequence.signal map:^id _Nullable(RQYDTQuestionModel *ydtQuestionModel) {
  97. return [RQExerciseModel exerciseModelWithRQYDTQuestionModel:ydtQuestionModel];
  98. }].toArray;
  99. RQExerciseViewModel *exerciseViewModel = [[RQExerciseViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
  100. RQHomePageCarTypeKey : @(RQ_YDTQuestion_Module.carType),
  101. RQHomePageSubjectTypeKey : @(RQ_YDTQuestion_Module.subject),
  102. RQHomeSubPageTypeKey : @(RQHomeSubPageType_SequentialPractice),
  103. RQViewModelIDKey : dictInfoModel.dictValue,
  104. RQExerciseTypeKey : @(RQExerciseType_Chapter),
  105. RQViewModelUtilKey : arr,
  106. }];
  107. RQ_Exercise_Module.otherStr = dictInfoModel.dictValue;
  108. [RQ_APPDELEGATE.services pushViewModel:exerciseViewModel animated:YES];
  109. };
  110. group.itemViewModels = @[commonItemViewModel];
  111. return group;
  112. }].array;
  113. return viewModels ?: @[];
  114. }
  115. @end