Skip to content

Instantly share code, notes, and snippets.

@basilche
Created July 9, 2019 04:48
Show Gist options
  • Save basilche/d5e8a81ebe8ae0dbbb9dc53aa124aac6 to your computer and use it in GitHub Desktop.
Save basilche/d5e8a81ebe8ae0dbbb9dc53aa124aac6 to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
@IBOutlet weak var loginTextField: UITextField?
@IBOutlet weak var passwordTextField: UITextField?
@IBOutlet weak var loginButton: UIButton?
private let disposeBag = DisposeBag()
private let viewModel = ViewModel()
override func viewDidLoad() {
super.viewDidLoad()
loginButton?.isEnabled = false
guard let loginTextField = loginTextField,
let passwordTextField = passwordTextField,
let loginButton = loginButton else {
return
}
//send the entered login value to the model
loginTextField.rx.text.orEmpty
.distinctUntilChanged()
.bind(to: viewModel.loginInputPublisher)
.disposed(by: disposeBag)
//send the entered password value to the model
passwordTextField.rx.text.orEmpty
.distinctUntilChanged()
.bind(to: viewModel.passworInputdPublisher)
.disposed(by: disposeBag)
//subscribe to loginEnabledDriver and set the isEnabled of the button
viewModel.loginEnabledDriver
.drive(loginButton.rx.isEnabled)
.disposed(by: disposeBag)
}
class ViewModel {
let loginInputPublisher: PublishSubject<String> = PublishSubject()
let passworInputdPublisher: PublishSubject<String> = PublishSubject()
let loginEnabledDriver: Driver<Bool>
init() {
loginEnabledDriver = Observable.combineLatest(loginInputPublisher, passworInputdPublisher) { (login, password) in
return login.isLoginValid && password.isPasswordValid
}
.asDriver(onErrorJustReturn: false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment