// // RQSettingViewModel.m // jiaPei // // Created by 张嵘 on 2022/5/5. // Copyright © 2022 JCZ. All rights reserved. // #import "RQSettingViewModel.h" @interface RQSettingItemModel () @property (nonatomic, readwrite, copy) NSString *title; @property (nonatomic, readwrite, copy) NSString *icon; @end @implementation RQSettingItemModel + (instancetype)itemModelWithTitle:(NSString *)title icon:(NSString *)icon { RQSettingItemModel *item = [[self alloc] init]; item.title = title; item.icon = icon; return item; } @end @interface RQSettingViewModel () @property (nonatomic, readwrite, copy) NSArray *itemsArr; /// 登出的命令 @property (nonatomic, readwrite, strong) RACCommand *logoutCommand; /// 登出回调 @property (nonatomic, readwrite, strong) RACSubject *logoutSubject; @end @implementation RQSettingViewModel - (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.logoutCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) { /// 删除账号 // [SAMKeychain deleteRawLogin]; /// 先退出用户 [RQ_USER_MANAGER logoutUser]; return [RACSignal createSignal:^RACDisposable *(id subscriber) { /// 延迟一段时间 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)}]; [subscriber sendNext:nil]; [subscriber sendCompleted]; }); return [RACDisposable disposableWithBlock:^{ }]; }]; }]; /// 配置数据 [self rq_configureData]; } #pragma mark - 配置数据 - (void)rq_configureData { @weakify(self) /// 第一组 RQCommonGroupViewModel *group0 = [RQCommonGroupViewModel groupViewModel]; group0.itemViewModels = [self.itemsArr.rac_sequence.signal map:^id _Nullable(RQSettingItemModel *settingItemModel) { RQCommonArrowItemViewModel *arrowItem = [RQCommonArrowItemViewModel itemViewModelWithTitle:settingItemModel.title icon:settingItemModel.icon]; if ([arrowItem.title isEqualToString:@"注销账号"]) { arrowItem.subtitle = @"删除所有数据,无法恢复"; } @weakify(arrowItem) arrowItem.operation = ^{ @strongify(self,arrowItem) if ([arrowItem.title isEqualToString:@"修改密码"]) { RQUpdatePasswordViewModel *model = [[RQUpdatePasswordViewModel alloc] initWithServices:self.services params:nil]; [self.services pushViewModel:model animated:YES]; } else if ([arrowItem.title isEqualToString:@"关于我们"]) { RQAboutViewModel *aboutViewModel = [[RQAboutViewModel alloc] initWithServices:self.services params:nil]; [self.services pushViewModel:aboutViewModel animated:YES]; } else if ([arrowItem.title isEqualToString:@"注销账号"]) { [[RACScheduler mainThreadScheduler] schedule:^{ [RQ_ALERTVIEW_MANAGER showAlertWithAlertType:RQAlertType_SignOut title:@"注销账号" message:RQ_COMMON_MANAGER.APP_SWITCH? @"注销账号将会永久删除当前用户所有数据,请谨慎操作!" : @"注销账号将会永久删除当前用户所有数据,VIP用户的VIP权益也将失效,请谨慎操作!" confirmTitle:@"确认" cancelTitle:@"取消" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { [MBProgressHUD rq_showProgressHUD:@"注销中"]; RACCommand *zxCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber * selected) { return [self zx]; return [RACSignal empty]; }]; [[zxCommand execute:@1] subscribeNext:^(id _Nullable x) { @strongify(self) [RQ_APPDELEGATE.services popToRootViewModelAnimated:YES]; [MBProgressHUD rq_hideHUD]; [self.logoutCommand execute:nil]; }]; } cancelAction:nil]; }]; } }; return arrowItem; }].toArray; /// 第二组 RQCommonGroupViewModel *group1 = [RQCommonGroupViewModel groupViewModel]; RQProfileLogouItemViewModel *profileLogouItemViewModel = [[RQProfileLogouItemViewModel alloc] init]; profileLogouItemViewModel.logoutCommand = self.logoutCommand; profileLogouItemViewModel.logoutSubject = self.logoutSubject; group1.itemViewModels = @[profileLogouItemViewModel]; self.dataSource = @[group0, group1]; } - (RACSignal *)zx { return [[RACSignal createSignal:^RACDisposable *(id subscriber) { [[RQ_HTTP_Service studentLoginWithUserId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(id x) { [subscriber sendNext:x]; } error:^(NSError * _Nullable error) { [subscriber sendError:error]; } completed:^{ [subscriber sendCompleted]; }]; return [RACDisposable disposableWithBlock:^{ /// 取消任务 }]; }] deliverOnMainThread]; } #pragma mark - LazyLoad - (NSArray *)itemsArr { return @[ [RQSettingItemModel itemModelWithTitle:@"修改密码" icon:@"隐私协议"], [RQSettingItemModel itemModelWithTitle:@"注销账号" icon:@"清除缓存"], [RQSettingItemModel itemModelWithTitle:@"关于我们" icon:@"关于我们"], ]; } @end