// // RQCollectViewModel.m // jiaPei // // Created by 张嵘 on 2022/8/3. // Copyright © 2022 JCZ. All rights reserved. // #import "RQCollectViewModel.h" @interface RQCollectViewModel () /// 数组 @property (nonatomic, readwrite, copy) NSArray *items; @end @implementation RQCollectViewModel #pragma mark - Public Method - (instancetype)initWithServices:(id)services params:(NSDictionary *)params { if (self = [super initWithServices:services params:params]) { } return self; } - (void)initialize { [super initialize]; @weakify(self); self.title = @""; self.prefersNavigationBarBottomLineHidden = YES; ///配置数据 [self rq_configureData]; [[[RQNotificationCenter rac_addObserverForName:RQCancelCollectNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * _Nullable x) { @strongify(self); [self.requestRemoteDataCommand execute:nil]; }]; } #pragma mark - PrivateMethod - (void)rq_configureData { @weakify(self); /// 列表 RAC(self, items) = self.requestRemoteDataCommand.executionSignals.switchToLatest; /// 数据源 RAC(self, dataSource) = [RACObserve(self, items) map:^(NSArray * items) { @strongify(self) return RQObjectIsNil(items)? @[] : [self dataSourceWithItems:items]; }]; } /// 请求数据 - (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page { NSArray * (^itemsBlock)(NSArray *) = ^(NSArray *products) { if (page == 1 || page == 0) { /// 下拉刷新 } else { /// 上拉加载 products = @[(self.items ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array; } return products; }; /// return [[[RQ_HTTP_Service getChapterList] map:itemsBlock] doError:^(NSError * _Nonnull error) { [MBProgressHUD rq_showErrorTips:error]; }]; } - (NSArray *)dataSourceWithItems:(NSArray *)items { __block NSInteger index = 1; NSArray *viewModels = [[items.rac_sequence filter:^BOOL(RQDictInfoModel *dictInfoModel) { return [RQ_YDTQuestion_Module getChapterQuestionWithChapterName:dictInfoModel.dictValue exerciseType:RQExerciseType_Collect].count > 0; }].array.rac_sequence map:^(RQDictInfoModel *dictInfoModel) { RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel]; NSArray *chapterArr = [RQ_YDTQuestion_Module getChapterQuestionWithChapterName:dictInfoModel.dictValue exerciseType:RQExerciseType_Collect]; RQErrorAndCollectItemViewModel *errorAndCollectItemViewModel = [[RQErrorAndCollectItemViewModel alloc] initWithNumStr:[NSString qmui_stringWithNSInteger:index] titleStr:dictInfoModel.dictValue countStr:[NSString qmui_stringWithNSInteger:chapterArr.count]]; index ++; errorAndCollectItemViewModel.operation = ^{ NSArray *arr = [chapterArr.rac_sequence.signal map:^id _Nullable(RQYDTQuestionModel *ydtQuestionModel) { return [RQExerciseModel exerciseModelWithRQYDTQuestionModel:ydtQuestionModel]; }].toArray; RQExerciseViewModel *exerciseViewModel = [[RQExerciseViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{ RQHomePageCarTypeKey : @(RQ_YDTQuestion_Module.carType), RQHomePageSubjectTypeKey : @(RQ_YDTQuestion_Module.subject), RQHomeSubPageTypeKey : @(RQHomeSubPageType_WrongTopicAndCollection), RQViewModelIDKey : dictInfoModel.dictValue, RQExerciseTypeKey : @(RQExerciseType_Collect), RQViewModelUtilKey : arr, }]; [RQ_APPDELEGATE.services pushViewModel:exerciseViewModel animated:YES]; }; group.itemViewModels = @[errorAndCollectItemViewModel]; return group; }].array; return viewModels ?: @[]; } @end