RQUpdatePasswordViewModel.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // RQUpdatePasswordViewModel.m
  3. // YueXueChe
  4. //
  5. // Created by 张嵘 on 2019/1/14.
  6. // Copyright © 2019 RONG. All rights reserved.
  7. //
  8. #import "RQUpdatePasswordViewModel.h"
  9. #import "RQHTTPService+RQUpdatePassword.h"
  10. @interface RQUpdatePasswordViewModel ()
  11. /// closeCommand
  12. @property (nonatomic, readwrite, strong) RACCommand *closeCommand;
  13. /// sureCommand
  14. @property (nonatomic, readwrite, strong) RACCommand *sureCommand;
  15. /// 确定按钮有效性
  16. @property (nonatomic, readwrite, strong) RACSignal *validSureSignal;
  17. /// phone
  18. @property (nonatomic, readwrite, copy) NSString *phone;
  19. /// code
  20. @property (nonatomic, readwrite, copy) NSString *code;
  21. @end
  22. @implementation RQUpdatePasswordViewModel
  23. - (void)initialize {
  24. [super initialize];
  25. /// 显示导航栏的细线
  26. self.prefersNavigationBarBottomLineHidden = NO;
  27. self.title = @"修改密码";
  28. @weakify(self);
  29. self.closeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
  30. @strongify(self);
  31. [self.services popToRootViewModelAnimated:YES];
  32. return [RACSignal empty];
  33. }];
  34. /// 按钮的有效性
  35. self.validSureSignal = [[RACSignal
  36. combineLatest:@[RACObserve(self, freshPassword)]
  37. reduce:^(NSString *freshPassword) {
  38. return @(RQStringIsNotEmpty(freshPassword));
  39. }]
  40. distinctUntilChanged];
  41. self.sureCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
  42. @strongify(self);
  43. [QMUITips showLoading:@"修改中..." inView:DefaultTipsParentView];
  44. return [self updatePassWorld];
  45. }];
  46. }
  47. - (RACSignal *)updatePassWorld {
  48. @weakify(self);
  49. return [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
  50. [[RQ_HTTP_Service updatePasswordWithLoginCode:RQ_USER_MANAGER.currentUser.loginCode password:self.freshPassword] subscribeNext:^(id _Nullable x) {
  51. [subscriber sendNext:x];
  52. } error:^(NSError * _Nullable error) {
  53. [subscriber sendError:error];
  54. } completed:^{
  55. [subscriber sendCompleted];
  56. }];
  57. return [RACDisposable disposableWithBlock:^{
  58. }];
  59. }] doNext:^(id _Nullable x) {
  60. @strongify(self);
  61. [QMUITips hideAllTips];
  62. [QMUITips showSucceed:@"修改成功"];
  63. [RQ_USER_MANAGER logoutUser];
  64. /// 延迟一段时间
  65. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  66. /// 这里切换 到账号登录的界面
  67. [RQNotificationCenter postNotificationName:RQSwitchRootViewControllerNotification object:nil userInfo:@{RQSwitchRootViewControllerUserInfoKey:@(RQSwitchRootViewControllerFromTypeLogout)}];
  68. });
  69. }] doError:^(NSError * _Nonnull error) {
  70. [QMUITips hideAllTips];
  71. [QMUITips showError:[NSError rq_tipsFromError:error]];
  72. }];
  73. }
  74. @end