// // RQProfileViewModel.m // RQCommon // // Created by 张嵘 on 2018/11/21. // Copyright © 2018 张嵘. All rights reserved. // #import "RQProfileViewModel.h" @interface RQProfileViewModel () @property (nonatomic, readwrite, copy) NSArray *group1DicArr; @property (nonatomic, readwrite, copy) NSArray *group2DicArr; /// 登出的命令 @property (nonatomic, readwrite, strong) RACCommand *logoutCommand; /// 登出回调 @property (nonatomic, readwrite, strong) RACSubject *logoutSubject; @end @implementation RQProfileViewModel #pragma mark - Public Method - (instancetype)initWithServices:(id)services params:(NSDictionary *)params { if (self = [super initWithServices:services params:params]) { } return self; } - (void)initialize { [super initialize]; self.title = @"用户中心"; /// 退出回调 self.logoutSubject = [RACSubject subject]; ///配置数据 [self rq_configureData]; } #pragma mark - PrivateMethod - (void)rq_configureData { @weakify(self); /// 第零组 RQCommonGroupViewModel *group0 = [RQCommonGroupViewModel groupViewModel]; group0.footerHeight = 10; RQProfileUserInfoItemViewModel *profileUserInfoItemViewModel = [[RQProfileUserInfoItemViewModel alloc] init]; RAC(profileUserInfoItemViewModel, headImageName) = RACObserve(RQ_USER_MANAGER, headImage); RAC(profileUserInfoItemViewModel, nickname) = RACObserve(RQ_USER_MANAGER, nickName); // profileUserInfoItemViewModel.telePhone = @"未绑定手机号"; profileUserInfoItemViewModel.rowHeight = RQ_FIT_HORIZONTAL(74.f); profileUserInfoItemViewModel.operation = ^{ @strongify(self); RQUserInfoViewModel *viewModel = [[RQUserInfoViewModel alloc] initWithServices:self.services params:nil]; [self.services pushViewModel:viewModel animated:YES]; }; group0.itemViewModels = @[profileUserInfoItemViewModel]; /// 第一组 RQCommonGroupViewModel *group1 = [RQCommonGroupViewModel groupViewModel]; group1.headerHeight = CGFLOAT_MIN; group1.footerHeight = 10; NSMutableArray *group1Arr = @[].mutableCopy; [self.group1DicArr.rac_sequence.signal subscribeNext:^(NSDictionary *dic) { RQCommonArrowItemViewModel *item = [[RQCommonArrowItemViewModel alloc] init]; item.title = dic[@"title"]; item.icon = dic[@"icon"]; item.rowHeight = RQ_FIT_HORIZONTAL(54.f); if ([dic[@"title"] isEqualToString:@"激活码"]) { RAC(item , subtitle) = [RACObserve(RQ_USER_MANAGER, isVip) map:^id _Nullable(id _Nullable value) { return (RQ_USER_MANAGER.isVip > 0)? @"已激活" : @""; }]; RAC(item, operation) = [RACObserve(RQ_USER_MANAGER, isVip) map:^id _Nullable(id _Nullable value) { return ^{ if (RQ_USER_MANAGER.isVip > 0) { [MBProgressHUD rq_showTips:@"用户已激活VIP会员!"]; } else { RQActivateVIPViewModel *activateVIPViewModel = [[RQActivateVIPViewModel alloc] initWithServices:self.services params:nil]; [self.services pushViewModel:activateVIPViewModel animated:YES]; } }; }]; } else if ([dic[@"title"] isEqualToString:@"免费体验"]) { item.operation = ^{ RQFreeTryViewModel *freeTryViewModel = [[RQFreeTryViewModel alloc] initWithServices:self.services params:nil]; [self.services pushViewModel:freeTryViewModel animated:YES]; }; } else if ([dic[@"title"] isEqualToString:@"资料下载"]) { item.operation = ^{ RQLearningMaterialsViewModel *learningMaterialsViewModel = [[RQLearningMaterialsViewModel alloc] initWithServices:self.services params:nil]; [self.services pushViewModel:learningMaterialsViewModel animated:YES]; }; } [group1Arr addObject:item]; } completed:^{ [RACObserve(RQ_USER_MANAGER, isLogin) subscribeNext:^(id _Nullable x) { if (RQ_USER_MANAGER.isLogin) { [RQ_COMMON_MANAGER checkVersionWithComplete:^(BOOL isOpen) { if (isOpen) { group1.itemViewModels = @[]; group1.footerHeight = CGFLOAT_MIN; } else { [RACObserve(RQ_HTTP_Service, networkReachabilityStatus) subscribeNext:^(id _Nullable x) { if (RQ_HTTP_Service.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { group1.footerHeight = 0; group1.itemViewModels = @[]; } else { group1.footerHeight = 10; group1.itemViewModels = group1Arr.copy; } }]; } }]; } else { group1.itemViewModels = @[]; group1.footerHeight = CGFLOAT_MIN; } }]; }]; /// 第二组 RQCommonGroupViewModel *group2 = [RQCommonGroupViewModel groupViewModel]; NSMutableArray *group2Arr = @[].mutableCopy; [self.group2DicArr.rac_sequence.signal subscribeNext:^(NSDictionary *dic) { RQCommonArrowItemViewModel *item = [[RQCommonArrowItemViewModel alloc] init]; item.title = dic[@"title"]; item.icon = dic[@"icon"]; item.rowHeight = RQ_FIT_HORIZONTAL(54.f); if ([item.title isEqualToString:@"版本更新"]) { item.operation = ^{ RQVersionUpdateViewModel *versionUpdateViewModel = [[RQVersionUpdateViewModel alloc] initWithServices:self.services params:nil]; [self.services pushViewModel:versionUpdateViewModel animated:YES]; }; } else if ([item.title isEqualToString:@"反馈帮助"]) { item.operation = ^{ RQFeedbackAndHelpViewModel *feedbackAndHelpViewModel = [[RQFeedbackAndHelpViewModel alloc] initWithServices:self.services params:nil]; [self.services pushViewModel:feedbackAndHelpViewModel animated:YES]; }; } else if ([item.title isEqualToString:@"隐私政策"]) { item.operation = ^{ NSString *urlStr = @"https://ys.zzxcx.net/xy_sdjk.html"; RQWebViewModel *webViewModel = [[RQWebViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelRequestKey:urlStr}]; [RQ_APPDELEGATE.services pushViewModel:webViewModel animated:YES]; }; } else if ([item.title isEqualToString:@"联系客服"]) { item.operation = ^{ [RQ_WECHAT_MANAGER lianXiKeFu]; }; } [group2Arr addObject:item]; } completed:^{ /// 调试模式 #if defined(DEBUG)||defined(_DEBUG) RQCommonArrowItemViewModel *debug = [RQCommonArrowItemViewModel itemViewModelWithTitle:@"打开/关闭调试器" icon:@"rq_profile_debug_25x25"]; debug.rowHeight = RQ_FIT_HORIZONTAL(54.f); debug.operation = ^{ [[RQDebugTouchView sharedInstance] setHide:![RQDebugTouchView sharedInstance].isHide]; [RQSharedAppDelegate.window bringSubviewToFront:[RQDebugTouchView sharedInstance]]; }; [group2Arr addObject:debug]; #else /// 发布模式 #endif group2.itemViewModels = group2Arr.copy; }]; /// 第三组 RQCommonGroupViewModel *group3 = [RQCommonGroupViewModel groupViewModel]; RQProfileLogouItemViewModel *profileLogouItemViewModel = [[RQProfileLogouItemViewModel alloc] initWithTitle:@"退出登录"]; self.logoutCommand = profileLogouItemViewModel.logoutCommand; group3.itemViewModels = @[profileLogouItemViewModel]; [[RACSignal combineLatest:@[RACObserve(group0, itemViewModels), RACObserve(group1, itemViewModels), RACObserve(group2, itemViewModels), RACObserve(group3, itemViewModels), RACObserve(RQ_USER_MANAGER, isVip)] reduce:^(NSArray *group0ItemViewModels, NSArray *group1ItemViewModels, NSArray *group2ItemViewModels, NSArray *group3ItemViewModels, NSNumber *isVip){ RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel]; return @[group0, (RQ_USER_MANAGER.isVip > 2)? group : group1, group2, group3]; }] subscribeNext:^(NSArray *allArr) { self.dataSource = allArr; }] ; } #pragma mark - LazyLoad - (NSArray *)group1DicArr { return @[ @{@"title" : @"激活码", @"icon" : @"ActivationCode"}, @{@"title" : @"免费体验", @"icon" : @"免费体验"}, @{@"title" : @"资料下载", @"icon" : @"资料下载"}, ]; } - (NSArray *)group2DicArr { return @[ @{@"title" : @"版本更新", @"icon" : @"VersionUpdate"}, @{@"title" : @"反馈帮助", @"icon" : @"FeedbackHelp"}, @{@"title" : @"隐私政策", @"icon" : @"PrivacyPolicy"}, @{@"title" : @"联系客服", @"icon" : @"kf"}, ]; } @end