RQLearningMaterialsViewModel.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // RQLearningMaterialsViewModel.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2022/6/30.
  6. //
  7. #import "RQLearningMaterialsViewModel.h"
  8. @interface RQLearningMaterialsViewModel ()
  9. @end
  10. @implementation RQLearningMaterialsViewModel
  11. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params{
  12. if (self = [super initWithServices:services params:params]) {
  13. }
  14. return self;
  15. }
  16. - (void)initialize{
  17. [super initialize];
  18. self.title = @"资料下载";
  19. self.style = UITableViewStyleGrouped;
  20. /// 允许下拉刷新
  21. self.shouldPullDownToRefresh = YES;
  22. ///配置数据
  23. [self rq_configureData];
  24. }
  25. #pragma mark - PrivateMethod
  26. - (void)rq_configureData {
  27. @weakify(self);
  28. /// 列表
  29. RAC(self, items) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
  30. /// 数据源
  31. RAC(self, dataSource) = [RACObserve(self, items) map:^(NSArray * items) {
  32. @strongify(self)
  33. return [self dataSourceWithItems:items];
  34. }];
  35. /// 选中cell 跳转的命令
  36. // self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath * indexPath) {
  37. // return [RACSignal empty];
  38. // }];
  39. }
  40. /// 请求数据
  41. - (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page {
  42. NSArray * (^itemsBlock)(NSArray *) = ^(NSArray *products) {
  43. if (page == 1 || page == 0) {
  44. /// 下拉刷新
  45. } else {
  46. /// 上拉加载
  47. products = @[(self.items ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array;
  48. }
  49. return products;
  50. };
  51. ///
  52. return [[[RQ_HTTP_Service getLearningMaterialsList] map:itemsBlock] doError:^(NSError * _Nonnull error) {
  53. [MBProgressHUD rq_showErrorTips:error];
  54. }];
  55. }
  56. #pragma mark - 辅助方法
  57. - (NSArray *)dataSourceWithItems:(NSArray *)items {
  58. NSArray *viewModels = [items.rac_sequence map:^(RQLearningMaterialsModel *learningMaterialsModel) {
  59. RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel];
  60. RQLearningMaterialsItemViewModel *learningMaterialsItemViewModel = [[RQLearningMaterialsItemViewModel alloc] initWithLearningMaterialsModel:learningMaterialsModel allModelArr:items];
  61. group.itemViewModels = @[learningMaterialsItemViewModel];
  62. return group;
  63. }].array;
  64. return viewModels ?: @[];
  65. }
  66. @end