Permalink
Browse files

DisposeBag added and format more friendly

DisposeBag is important: https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md#dispose-bags
This reformat aids to look better each stream
  • Loading branch information...
carlosypunto committed Nov 19, 2015
1 parent 72d886e commit 8c9c4ca92d6b98dd5cffc4c99154bd07ab527df4
Showing with 54 additions and 39 deletions.
  1. +54 −39 reactive-recipe-1/reactive-recipe-1/ViewController.swift
@@ -17,58 +17,73 @@ class ViewController: UIViewController {
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var loginButton: UIButton!
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
let validUsernameObservable = usernameTextField.rx_text.map {
$0.characters.count > 5
}.distinctUntilChanged()
let validUsernameObservable = usernameTextField.rx_text
.map {
$0.characters.count > 5
}
.distinctUntilChanged()
let validPasswordObservable = passwordTextField.rx_text.map {
$0.characters.count > 5
}.distinctUntilChanged()
let validPasswordObservable = passwordTextField.rx_text
.map {
$0.characters.count > 5
}
.distinctUntilChanged()
validUsernameObservable.subscribeNext { value in
print(value)
}
_ = validUsernameObservable
.subscribeNext { value in
print(value)
}
.addDisposableTo(disposeBag)
validUsernameObservable.map {
$0 ? UIColor.greenColor() : UIColor.whiteColor()
}
.subscribeNext {
self.usernameTextField.backgroundColor = $0
}
validUsernameObservable
.map {
$0 ? UIColor.greenColor() : UIColor.whiteColor()
}
.subscribeNext {
self.usernameTextField.backgroundColor = $0
}
.addDisposableTo(disposeBag)
validPasswordObservable.map {
$0 ? UIColor.greenColor() : UIColor.whiteColor()
}
.subscribeNext {
validPasswordObservable
.map {
$0 ? UIColor.greenColor() : UIColor.whiteColor()
}
.subscribeNext {
self.passwordTextField.backgroundColor = $0
}
}
.addDisposableTo(disposeBag)
combineLatest(validUsernameObservable, validPasswordObservable) {
$0 && $1
}.bindTo(loginButton.rx_enabled)
$0 && $1
}
.bindTo(loginButton.rx_enabled)
.addDisposableTo(disposeBag)
func parseLogin() -> Observable<String> {
return create { observer in
PFUser.logInWithUsernameInBackground(self.usernameTextField.text!, password: self.passwordTextField.text!) { user, error in
observer.onNext(user?.username ?? "Try again!")
observer.onCompleted()
loginButton.rx_tap
.flatMap { _ -> Observable<String> in
return create { observer in
PFUser.logInWithUsernameInBackground(self.usernameTextField.text!, password: self.passwordTextField.text!) {
user, error in
observer.onNext(user?.username ?? "Try again!")
observer.onCompleted()
}
return NopDisposable.instance
}
return NopDisposable.instance
}
}
loginButton.rx_tap.flatMap {
parseLogin()
}
.subscribeNext {
let alert = UIAlertController(title: $0, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
.subscribeNext {
let alert = UIAlertController(title: $0, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
.addDisposableTo(disposeBag)
}
}

0 comments on commit 8c9c4ca

Please sign in to comment.