美文网首页
RAC MVVM demo

RAC MVVM demo

作者: 曾柏超 | 来源:发表于2018-11-02 19:19 被阅读11次

    https://www.jianshu.com/p/e34a86642d62

    Screen Shot 2018-11-02 at 7.11.35 PM.png

    VM中 获得btn是否能enable Signal

      self.loginButtonEnableSignal = [RACSignal combineLatest:@[RACObserve(self, username), RACObserve(self, password)] reduce:^id(NSString *username, NSString *password){
            return @(username.length && password.length);
        }];
    

    VC

    //根据signal设置btn 的enable
    - (void)loginButtonEnable {
        RAC(self.loginViewModel, username) = _loginView.usernameTextField.rac_textSignal;
        RAC(self.loginViewModel, password) = _loginView.passwordTextField.rac_textSignal;
        RAC(self.loginView.loginButton, enabled) = _loginViewModel.loginButtonEnableSignal;
        
    
    }
    
    //监听按钮点击 在vm 中处理网络请求
    - (void)loginButtonAction {
        [[_loginView.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self.loginViewModel.loginCommad execute:nil];
        }];
    }
    

    VM 处理网络请求

     self.loginCommad = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
    //            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login"]];
    //            request.HTTPMethod = @"POST";
    //            NSString *paramString = [NSString stringWithFormat:@"username=%@&pwd=%@&type=JSON", self.username, self.password];
    //            NSData *paramData = [paramString dataUsingEncoding:NSUTF8StringEncoding];
    //            request.HTTPBody = paramData;
    //            [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    //                NSDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
    //                [subscriber sendNext:resultDictionary];
    //                /****************** -------- 发送完成这一步很重要, 不然后面的无法信号无法执行 -------- ******************/
    //                [subscriber sendCompleted];
    //            }] resume];
                
                
                NSDictionary *resultDictionary = @{
                                                   @"value":@"zack",
                                                   @"book":@"quck"
                                                   
                                                   };
                [subscriber sendNext:resultDictionary];
                /****************** -------- 发送完成这一步很重要, 不然后面的无法信号无法执行 -------- ******************/
                [subscriber sendCompleted];
                
                return nil;
            }];
        }];
    
    
    //根据服务器返回结果做出相应的处理
     [self.loginCommad.executionSignals.switchToLatest subscribeNext:^(NSDictionary *x) {
    //        if ([x.allKeys.lastObject isEqualToString:@"success"]) {
                [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                [SVProgressHUD dismissWithDelay:1 completion:^{
                    YFFirstPageViewController *firstPageVC = [[YFFirstPageViewController alloc] init];
                    [UIView animateWithDuration:1 animations:^{
                        [UIApplication sharedApplication].keyWindow.rootViewController = firstPageVC;
                    }];
                }];
    //        }else {
    //            [SVProgressHUD showErrorWithStatus:@"登录失败"];
    //            [SVProgressHUD dismissWithDelay:1];
    //        }
        }];
        
        [[self.loginCommad.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if ([x boolValue]) {
                [SVProgressHUD showWithStatus:@"正在登录中"];
            }
        }];
    

    相关文章

      网友评论

          本文标题:RAC MVVM demo

          本文链接:https://www.haomeiwen.com/subject/xdocxqtx.html