RQSettingViewModel.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // RQSettingViewModel.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/5/5.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "RQSettingViewModel.h"
  9. @interface RQSettingItemModel ()
  10. @property (nonatomic, readwrite, copy) NSString *title;
  11. @property (nonatomic, readwrite, copy) NSString *icon;
  12. @end
  13. @implementation RQSettingItemModel
  14. + (instancetype)itemModelWithTitle:(NSString *)title icon:(NSString *)icon {
  15. RQSettingItemModel *item = [[self alloc] init];
  16. item.title = title;
  17. item.icon = icon;
  18. return item;
  19. }
  20. @end
  21. @interface RQSettingViewModel ()
  22. @property (nonatomic, readwrite, copy) NSArray *itemsArr;
  23. /// 登出的命令
  24. @property (nonatomic, readwrite, strong) RACCommand *logoutCommand;
  25. /// 登出回调
  26. @property (nonatomic, readwrite, strong) RACSubject *logoutSubject;
  27. @end
  28. @implementation RQSettingViewModel
  29. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params{
  30. if (self = [super initWithServices:services params:params]) {
  31. }
  32. return self;
  33. }
  34. - (void)initialize {
  35. [super initialize];
  36. self.title = @"设置";
  37. /// 退出回调
  38. self.logoutSubject = [RACSubject subject];
  39. /// 退出登录的命令
  40. self.logoutCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
  41. /// 删除账号
  42. // [SAMKeychain deleteRawLogin];
  43. /// 先退出用户
  44. [RQ_USER_MANAGER logoutUser];
  45. return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
  46. /// 延迟一段时间
  47. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  48. /// 这里切换 到账号登录的界面
  49. [RQNotificationCenter postNotificationName:RQSwitchRootViewControllerNotification object:nil userInfo:@{RQSwitchRootViewControllerUserInfoKey:@(RQSwitchRootViewControllerFromTypeLogout)}];
  50. [subscriber sendNext:nil];
  51. [subscriber sendCompleted];
  52. });
  53. return [RACDisposable disposableWithBlock:^{
  54. }];
  55. }];
  56. }];
  57. /// 配置数据
  58. [self rq_configureData];
  59. }
  60. #pragma mark - 配置数据
  61. - (void)rq_configureData {
  62. @weakify(self)
  63. /// 第一组
  64. RQCommonGroupViewModel *group0 = [RQCommonGroupViewModel groupViewModel];
  65. group0.itemViewModels = [self.itemsArr.rac_sequence.signal map:^id _Nullable(RQSettingItemModel *settingItemModel) {
  66. RQCommonArrowItemViewModel *arrowItem = [RQCommonArrowItemViewModel itemViewModelWithTitle:settingItemModel.title icon:settingItemModel.icon];
  67. if ([arrowItem.title isEqualToString:@"注销账号"]) {
  68. arrowItem.subtitle = @"删除所有数据,无法恢复";
  69. }
  70. @weakify(arrowItem)
  71. arrowItem.operation = ^{
  72. @strongify(self,arrowItem)
  73. if ([arrowItem.title isEqualToString:@"修改密码"]) {
  74. RQUpdatePasswordViewModel *model = [[RQUpdatePasswordViewModel alloc] initWithServices:self.services params:nil];
  75. [self.services pushViewModel:model animated:YES];
  76. } else if ([arrowItem.title isEqualToString:@"关于我们"]) {
  77. RQAboutViewModel *aboutViewModel = [[RQAboutViewModel alloc] initWithServices:self.services params:nil];
  78. [self.services pushViewModel:aboutViewModel animated:YES];
  79. } else if ([arrowItem.title isEqualToString:@"注销账号"]) {
  80. [[RACScheduler mainThreadScheduler] schedule:^{
  81. [RQ_ALERTVIEW_MANAGER showAlertWithAlertType:RQAlertType_SignOut title:@"注销账号" message: @"注销账号将会永久删除当前用户所有数据,VIP用户的VIP权益也将失效,请谨慎操作!" confirmTitle:@"确认" cancelTitle:@"取消" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) {
  82. [MBProgressHUD rq_showProgressHUD:@"注销中"];
  83. RACCommand *zxCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSNumber * selected) {
  84. return [self zx];
  85. return [RACSignal empty];
  86. }];
  87. [[zxCommand execute:@1] subscribeNext:^(id _Nullable x) {
  88. @strongify(self)
  89. [RQ_APPDELEGATE.services popToRootViewModelAnimated:YES];
  90. [MBProgressHUD rq_hideHUD];
  91. [self.logoutCommand execute:nil];
  92. }];
  93. } cancelAction:nil];
  94. }];
  95. }
  96. };
  97. return arrowItem;
  98. }].toArray;
  99. /// 第二组
  100. RQCommonGroupViewModel *group1 = [RQCommonGroupViewModel groupViewModel];
  101. RQProfileLogouItemViewModel *profileLogouItemViewModel = [[RQProfileLogouItemViewModel alloc] init];
  102. profileLogouItemViewModel.logoutCommand = self.logoutCommand;
  103. profileLogouItemViewModel.logoutSubject = self.logoutSubject;
  104. group1.itemViewModels = @[profileLogouItemViewModel];
  105. self.dataSource = @[group0, group1];
  106. }
  107. - (RACSignal *)zx {
  108. return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
  109. [[RQ_HTTP_Service studentLoginWithUserId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(id x) {
  110. [subscriber sendNext:x];
  111. } error:^(NSError * _Nullable error) {
  112. [subscriber sendError:error];
  113. } completed:^{
  114. [subscriber sendCompleted];
  115. }];
  116. return [RACDisposable disposableWithBlock:^{
  117. /// 取消任务
  118. }];
  119. }] deliverOnMainThread];
  120. }
  121. #pragma mark - LazyLoad
  122. - (NSArray *)itemsArr {
  123. return @[
  124. [RQSettingItemModel itemModelWithTitle:@"修改密码" icon:@"隐私协议"],
  125. [RQSettingItemModel itemModelWithTitle:@"注销账号" icon:@"清除缓存"],
  126. [RQSettingItemModel itemModelWithTitle:@"关于我们" icon:@"关于我们"],
  127. ];
  128. }
  129. @end