@@ -25,21 +25,20 @@ class ViewController: UIViewController {
2525 @IBOutlet weak var button2 : UIButton !
2626
2727 var disposableBag = DisposeBag ( )
28- let sharedAction = Action < SharedInput , String > { input in
28+ let sharedAction = Action < SharedInput , String > { input in
2929 switch input {
3030 case . barButton: return Observable . just ( " UIBarButtonItem with 3 seconds delay " ) . delaySubscription ( 3 , scheduler: MainScheduler . instance)
31- case . button ( let title) : return . just( " UIButton " + title)
31+ case . button( let title) : return . just( " UIButton " + title)
3232 }
3333 }
34+
3435 override func viewDidLoad( ) {
3536 super. viewDidLoad ( )
3637
3738 // Demo: add an action to a button in the view
3839 let action = CocoaAction {
3940 print ( " Button was pressed, showing an alert and keeping the activity indicator spinning while alert is displayed " )
40- return Observable . create {
41- [ weak self] observer -> Disposable in
42-
41+ return Observable . create { [ weak self] observer -> Disposable in
4342 // Demo: show an alert and complete the view's button action once the alert's OK button is pressed
4443 let alertController = UIAlertController ( title: " Hello world " , message: " This alert was triggered by a button action " , preferredStyle: . alert)
4544 var ok = UIAlertAction . Action ( " OK " , style: . default)
@@ -49,7 +48,7 @@ class ViewController: UIViewController {
4948 return . empty( )
5049 }
5150 alertController. addAction ( ok)
52- self ! . present ( alertController, animated: true , completion: nil )
51+ self ? . present ( alertController, animated: true , completion: nil )
5352
5453 return Disposables . create ( )
5554 }
@@ -58,7 +57,7 @@ class ViewController: UIViewController {
5857 button. rx. action = action
5958
6059 // Demo: add an action to a UIBarButtonItem in the navigation item
61- self . navigationItem. rightBarButtonItem! . rx. action = CocoaAction {
60+ self . navigationItem. rightBarButtonItem? . rx. action = CocoaAction {
6261 print ( " Bar button item was pressed, simulating a 2 second action " )
6362 return Observable . empty ( ) . delaySubscription ( 2 , scheduler: MainScheduler . instance)
6463 }
@@ -67,15 +66,13 @@ class ViewController: UIViewController {
6766 // while performing the work
6867 Observable . combineLatest (
6968 button. rx. action!. executing,
70- self . navigationItem. rightBarButtonItem!. rx. action!. executing) {
69+ self . navigationItem. rightBarButtonItem!. rx. action!. executing) { a , b in
7170 // we combine two boolean observable and output one boolean
72- a, b in
7371 return a || b
7472 }
7573 . distinctUntilChanged ( )
76- . subscribe ( onNext: {
74+ . subscribe ( onNext: { [ weak self ] executing in
7775 // every time the execution status changes, spin an activity indicator
78- [ weak self] executing in
7976 self ? . workingLabel. isHidden = !executing
8077 if ( executing) {
8178 self ? . activityIndicator. startAnimating ( )
@@ -84,29 +81,26 @@ class ViewController: UIViewController {
8481 self ? . activityIndicator. stopAnimating ( )
8582 }
8683 } )
87-
8884 . addDisposableTo ( self . disposableBag)
89-
90-
85+
9186 button1. rx. bindToAction ( sharedAction, input: . button( " Button 1 " ) )
92-
87+
9388 button2. rx. bindToAction ( sharedAction) { _ in
9489 return . button( " Button 2 " )
9590 }
9691 self . navigationItem. leftBarButtonItem? . rx. bindToAction ( sharedAction, input: . barButton)
97-
98- sharedAction. executing. debounce ( 0 , scheduler: MainScheduler . instance) . subscribe ( onNext: { [ weak self] executing in
92+
93+ sharedAction. executing. debounce ( 0 , scheduler: MainScheduler . instance) . subscribe ( onNext: { [ weak self] executing in
9994 if ( executing) {
10095 self ? . activityIndicator. startAnimating ( )
10196 }
10297 else {
10398 self ? . activityIndicator. stopAnimating ( )
10499 }
105100 } ) . addDisposableTo ( self . disposableBag)
101+
106102 sharedAction. elements. subscribe ( onNext: { string in
107- print ( string + " pressed " )
103+ print ( string + " pressed " )
108104 } ) . addDisposableTo ( self . disposableBag)
109-
110-
111105 }
112106}
0 commit comments