// // RQLearningMaterialsViewModel.m // SDJK // // Created by 张嵘 on 2022/6/30. // #import "RQLearningMaterialsViewModel.h" @interface RQLearningMaterialsViewModel () @end @implementation RQLearningMaterialsViewModel - (instancetype)initWithServices:(id)services params:(NSDictionary *)params{ if (self = [super initWithServices:services params:params]) { } return self; } - (void)initialize{ [super initialize]; self.title = @"资料下载"; self.style = UITableViewStyleGrouped; /// 允许下拉刷新 self.shouldPullDownToRefresh = YES; ///配置数据 [self rq_configureData]; } #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 [self dataSourceWithItems:items]; }]; /// 选中cell 跳转的命令 // self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath * indexPath) { // return [RACSignal empty]; // }]; } /// 请求数据 - (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 getLearningMaterialsList] map:itemsBlock] doError:^(NSError * _Nonnull error) { [MBProgressHUD rq_showErrorTips:error]; }]; } #pragma mark - 辅助方法 - (NSArray *)dataSourceWithItems:(NSArray *)items { NSArray *viewModels = [items.rac_sequence map:^(RQLearningMaterialsModel *learningMaterialsModel) { RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel]; RQLearningMaterialsItemViewModel *learningMaterialsItemViewModel = [[RQLearningMaterialsItemViewModel alloc] initWithLearningMaterialsModel:learningMaterialsModel allModelArr:items]; group.itemViewModels = @[learningMaterialsItemViewModel]; return group; }].array; return viewModels ?: @[]; } @end