I have authorization controller with 2 UITextField properties and 1 UIButton. I want to bind my View to ViewModel but don't know how to to do it. This is my AuthorizatioVC.swift:
class AuthorizationViewController: UIViewController {
let disposeBag = DisposeBag()
@IBOutlet weak var passwordTxtField: UITextField!
@IBOutlet weak var loginTxtField: UITextField!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
addBindsToViewModel()
}
func addBindsToViewModel(){
let authModel = AuthorizationViewModel(authClient: AuthClient())
authModel.login.asObservable().bindTo(passwordTxtField.rx_text).addDisposableTo(self.disposeBag)
authModel.password.asObservable().bindTo(loginTxtField.rx_text).addDisposableTo(self.disposeBag)
//HOW TO BIND button.rx_tap here?
}
}
And this is my AuthorizationViewModel.swift:
final class AuthorizationViewModel{
private let disposeBag = DisposeBag()
//input
//HOW TO DEFINE THE PROPERTY WHICH WILL BE BINDED TO RX_TAP FROM THE BUTTON IN VIEW???
let authEvent = ???
let login = Variable<String>("")
let password = Variable<String>("")
//output
private let authModel: Observable<Auth>
init(authClient: AuthClient){
let authModel = authEvent.asObservable()
.flatMap({ (v) -> Observable<Auth> in
return authClient.authObservable(String(self.login.value), mergedHash: String(self.password.value))
.map({ (authResponse) -> Auth in
return self.convertAuthResponseToAuthModel(authResponse)
})
})
}
func convertAuthResponseToAuthModel(authResponse: AuthResponse) -> Auth{
var authModel = Auth()
authModel.token = authResponse.token
return authModel
}
}
You can turn the taps on the UIButton into an Observable and hand it to the ViewModel along with the two Observables from the UITextFields.
This is a small working example for your scenario. (I used a small auth client mock class to simulate the response from the service):
The ViewController:
These are the two interesting parts:
// 1: We inject the three Observables into the ViewModel when we initialize it.
// 2: Then we subscribe to the ViewModel's output to receive the
Auth
model after the login was done.The ViewModel:
The ViewModel gets the 3 Observables in its init method and connects them to its output:
// 1: Combine the latest value of the login text field and the latest value of the password text field into one Observable.
// 2: When the user presses the button, use the latest value of the login text field and the latest value of the password text field and pass that to the auth service using
flatMap
. When the auth client returns aAuthResponse
, map that to theAuth
model. Set the result of this "chain" as theauthResponse
output of theViewModel
The problem here is that you are trying to make your "viewModel" a class. It should be a function.
Use set it up by doing this in your viewDidLoad:
If you have multiple outputs for your view model, then it might be worth it to make a class (rather than returning a tuple from a function.) If you want to do that, then
GithubSignupViewModel1
from the examples in the RxSwift repo is an excellent example of how to set it up.First approach use PublishSubject
The second approach use Action