// // RQPlaceListViewModel.m // JiaPei // // Created by 张嵘 on 2023/4/12. // #import "RQPlaceListViewModel.h" @interface RQPlaceListViewModel () //@property (nonatomic, readwrite, strong) RQLocationModel *myLocationModel; @property (nonatomic, readwrite, copy) NSString *placeName; @end @implementation RQPlaceListViewModel #pragma mark - Public Method - (instancetype)initWithServices:(id)services params:(NSDictionary *)params { if (self = [super initWithServices:services params:params]) { // self.myLocationModel = RQ_LOCATION_MANAGER.myLocationModel; } return self; } - (void)initialize { [super initialize]; @weakify(self) /// 隐藏导航栏的细线 self.title = @"科三真实考场"; self.prefersNavigationBarBottomLineHidden = YES; self.shouldPullDownToRefresh = YES; self.shouldPullUpToLoadMore = YES; self.style = UITableViewStyleGrouped; self.emptyTex = @"暂无考场信息~"; self.emptyImage = RQImageNamed(@"考场空数据占位图"); // [QMUITips showLoading:@"定位中..." inView:DefaultTipsParentView]; // [RQ_LOCATION_MANAGER updateLocationWithCompleteBlock:^(BOOL success, RQLocationModel * _Nullable locationModel) { // [QMUITips hideAllTips]; // @strongify(self) // if (success) { // self.myLocationModel = locationModel; // } // [self.requestRemoteDataCommand execute:nil]; // }]; ///配置数据 [self rq_configureData]; } //- (void)updateMyLocationWithLocationModel:(RQLocationModel *)locationModel { // self.myLocationModel = locationModel; // [self.requestRemoteDataCommand execute:nil]; //} - (void)updateMyPlaceNameWithPlaceName:(NSString *)placeName { self.placeName = placeName; [self.requestRemoteDataCommand execute:nil]; } #pragma mark - PrivateMethod - (void)rq_configureData { @weakify(self); RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel]; RAC(self, items) = self.requestRemoteDataCommand.executionSignals.switchToLatest; RAC(group, itemViewModels) = [RACObserve(self, items) map:^(NSArray * items) { @strongify(self) return RQObjectIsNil(items)? @[] : [self dataSourceWithItems:items]; }]; RAC(self, dataSource) = [RACObserve(group, itemViewModels) map:^(NSArray * itemViewModels) { [group setCustomStyleWithRQCommonGroupModel:^(RQCommonGroupModel *groupModel) { groupModel.headerHeight = (itemViewModels.count == 0)? 0.f : 10.f; groupModel.footerHeight = (itemViewModels.count == 0)? 0.f : 10.f; groupModel.headerBgColor = [UIColor qmui_colorWithHexString:@"#EDF4FF"]; groupModel.footerBgColor = [UIColor qmui_colorWithHexString:@"#EDF4FF"]; }]; return @[group]; }]; } /// 请求数据 - (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page { [QMUITips showLoadingInView:DefaultTipsParentView]; NSArray * (^itemsBlock)(NSArray *) = ^(NSArray *products) { [QMUITips hideAllTips]; if (page == 1 || page == 0) { /// 下拉刷新 } else { /// 上拉加载 products = @[(self.items ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array; } return products; }; /// return [[[RQ_HTTP_Service getPlaceListWithPageNum:self.page pageSize:self.perPage placeName:self.placeName] map:itemsBlock] doError:^(NSError * _Nonnull error) { [QMUITips hideAllTips]; [QMUITips showError:[NSError rq_tipsFromError:error]]; }]; } #pragma mark - 辅助方法 - (NSArray *)dataSourceWithItems:(NSArray *)items { // @weakify(self) NSArray *viewModels = [items.rac_sequence map:^(RQPlaceListModel *placeListModel) { // @strongify(self) RQPlaceListItemViewModel *placeListItemViewModel = [[RQPlaceListItemViewModel alloc] initWithPlaceListModel:placeListModel items:items]; return placeListItemViewModel; }].array; return viewModels ?: @[]; } @end