// // RQHomePageViewModel.m // RQCommon // // Created by 张嵘 on 2018/11/21. // Copyright © 2018 张嵘. All rights reserved. // #import "RQHomePageViewModel.h" @interface RQHomePageViewModel () @property (nonatomic, readwrite, strong) RACCommand *requestReLoginCommand; /// The current `user`. @property (nonatomic, readwrite , strong) RQUserModel *user; @end @implementation RQHomePageViewModel #pragma mark - Public Method - (instancetype)initWithServices:(id)services params:(NSDictionary *)params{ if (self = [super initWithServices:services params:params]) { /// 获取user self.user = [RQ_USER_MANAGER currentUser]; } return self; } - (void)initialize { [super initialize]; self.title = @""; /// 隐藏导航栏的细线 self.prefersNavigationBarBottomLineHidden = YES; ///配置数据 [self rq_configureData]; } #pragma mark - PrivateMethod - (void)rq_configureData { @weakify(self); /// 获取网络数据+本地数据 RACSignal *fetchLocalDataSignal = [RACSignal return:[self fetchLocalData]]; RACSignal *requestRemoteDataSignal = self.requestRemoteDataCommand.executionSignals.switchToLatest; [[[fetchLocalDataSignal merge:requestRemoteDataSignal] deliverOnMainThread] subscribeNext:^(RQUserModel *user) { @strongify(self) [RQ_VIP_Module isVipWithSubject:0 complete:^(BOOL isVip) {}]; [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) {}]; [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectOne] subscribeNext:^(NSArray *examResultModelArr) { RQ_COMMON_MANAGER.myExamOneCount = examResultModelArr.count; RQ_COMMON_MANAGER.examResultOneListArr = examResultModelArr; }]; [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectFour] subscribeNext:^(NSArray *examResultModelArr) { RQ_COMMON_MANAGER.myExamFourCount = examResultModelArr.count; RQ_COMMON_MANAGER.examResultFourListArr = examResultModelArr; }]; [self willChangeValueForKey:@"user"]; /// user模型的数据 重置,但是user的 指针地址不变 [self.user mergeValuesForKeysFromModel:user]; [self didChangeValueForKey:@"user"]; }]; [self.requestReLoginCommand execute:nil]; } - (RACCommand *)requestReLoginCommand { @weakify(self) return [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) { @strongify(self) return [[self requestReLoginSignal] takeUntil:self.rac_willDeallocSignal]; }]; } /// 获取本地的用户数据 - (RQUserModel *)fetchLocalData{ return [RQ_USER_MANAGER currentUser]; } /// 获取网络的用户数据 用于比对 - (RACSignal *)requestReLoginSignal { return [[RACSignal createSignal:^RACDisposable * _Nullable(id _Nonnull subscriber) { if (RQStringIsNotEmpty([SAMKeychain rawLogin]) && RQStringIsNotEmpty([SAMKeychain rawLoginPassword]) && RQ_USER_MANAGER.currentUser) { RQ_USER_MANAGER.currentUser.channel = RQUserLoginChannelTypeAutoLogin; [[RQ_HTTP_Service studentLoginWithLoginCode:[SAMKeychain rawLogin] password:[SAMKeychain rawLoginPassword]] subscribeNext:^(RQUserModel *userModel) { [subscriber sendNext:userModel]; } error:^(NSError * _Nullable error) { [subscriber sendError:error]; } completed:^{ [subscriber sendCompleted]; }]; } return [RACDisposable disposableWithBlock:^{ /// 取消任务 }]; }] doError:^(NSError * _Nonnull error) { [RQ_USER_MANAGER logoutUser]; /// 延迟一段时间 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ /// 这里切换 到账号登录的界面 [RQNotificationCenter postNotificationName:RQSwitchRootViewControllerNotification object:nil userInfo:@{RQSwitchRootViewControllerUserInfoKey:@(RQSwitchRootViewControllerFromTypeLogout)}]; }); [QMUITips showError:[NSError rq_tipsFromError:error]]; }]; } @end