RQPlaceListViewModel.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // RQPlaceListViewModel.m
  3. // JiaPei
  4. //
  5. // Created by 张嵘 on 2023/4/12.
  6. //
  7. #import "RQPlaceListViewModel.h"
  8. @interface RQPlaceListViewModel ()
  9. //@property (nonatomic, readwrite, strong) RQLocationModel *myLocationModel;
  10. @property (nonatomic, readwrite, copy) NSString *placeName;
  11. @end
  12. @implementation RQPlaceListViewModel
  13. #pragma mark - Public Method
  14. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params {
  15. if (self = [super initWithServices:services params:params]) {
  16. // self.myLocationModel = RQ_LOCATION_MANAGER.myLocationModel;
  17. }
  18. return self;
  19. }
  20. - (void)initialize {
  21. [super initialize];
  22. @weakify(self)
  23. /// 隐藏导航栏的细线
  24. self.title = @"科三真实考场";
  25. self.prefersNavigationBarBottomLineHidden = YES;
  26. self.shouldPullDownToRefresh = YES;
  27. self.shouldPullUpToLoadMore = YES;
  28. self.style = UITableViewStyleGrouped;
  29. self.emptyTex = @"暂无考场信息~";
  30. self.emptyImage = RQImageNamed(@"考场空数据占位图");
  31. // [QMUITips showLoading:@"定位中..." inView:DefaultTipsParentView];
  32. // [RQ_LOCATION_MANAGER updateLocationWithCompleteBlock:^(BOOL success, RQLocationModel * _Nullable locationModel) {
  33. // [QMUITips hideAllTips];
  34. // @strongify(self)
  35. // if (success) {
  36. // self.myLocationModel = locationModel;
  37. // }
  38. // [self.requestRemoteDataCommand execute:nil];
  39. // }];
  40. ///配置数据
  41. [self rq_configureData];
  42. }
  43. //- (void)updateMyLocationWithLocationModel:(RQLocationModel *)locationModel {
  44. // self.myLocationModel = locationModel;
  45. // [self.requestRemoteDataCommand execute:nil];
  46. //}
  47. - (void)updateMyPlaceNameWithPlaceName:(NSString *)placeName {
  48. self.placeName = placeName;
  49. [self.requestRemoteDataCommand execute:nil];
  50. }
  51. #pragma mark - PrivateMethod
  52. - (void)rq_configureData {
  53. @weakify(self);
  54. RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel];
  55. RAC(self, items) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
  56. RAC(group, itemViewModels) = [RACObserve(self, items) map:^(NSArray * items) {
  57. @strongify(self)
  58. return RQObjectIsNil(items)? @[] : [self dataSourceWithItems:items];
  59. }];
  60. RAC(self, dataSource) = [RACObserve(group, itemViewModels) map:^(NSArray * itemViewModels) {
  61. [group setCustomStyleWithRQCommonGroupModel:^(RQCommonGroupModel *groupModel) {
  62. groupModel.headerHeight = (itemViewModels.count == 0)? 0.f : 10.f;
  63. groupModel.footerHeight = (itemViewModels.count == 0)? 0.f : 10.f;
  64. groupModel.headerBgColor = [UIColor qmui_colorWithHexString:@"#EDF4FF"];
  65. groupModel.footerBgColor = [UIColor qmui_colorWithHexString:@"#EDF4FF"];
  66. }];
  67. return @[group];
  68. }];
  69. }
  70. /// 请求数据
  71. - (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page {
  72. [QMUITips showLoadingInView:DefaultTipsParentView];
  73. NSArray * (^itemsBlock)(NSArray *) = ^(NSArray *products) {
  74. [QMUITips hideAllTips];
  75. if (page == 1 || page == 0) {
  76. /// 下拉刷新
  77. } else {
  78. /// 上拉加载
  79. products = @[(self.items ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array;
  80. }
  81. return products;
  82. };
  83. ///
  84. return [[[RQ_HTTP_Service getPlaceListWithPageNum:self.page pageSize:self.perPage placeName:self.placeName] map:itemsBlock] doError:^(NSError * _Nonnull error) {
  85. [QMUITips hideAllTips];
  86. [QMUITips showError:[NSError rq_tipsFromError:error]];
  87. }];
  88. }
  89. #pragma mark - 辅助方法
  90. - (NSArray *)dataSourceWithItems:(NSArray *)items {
  91. // @weakify(self)
  92. NSArray *viewModels = [items.rac_sequence map:^(RQPlaceListModel *placeListModel) {
  93. // @strongify(self)
  94. RQPlaceListItemViewModel *placeListItemViewModel = [[RQPlaceListItemViewModel alloc] initWithPlaceListModel:placeListModel items:items];
  95. return placeListItemViewModel;
  96. }].array;
  97. return viewModels ?: @[];
  98. }
  99. @end