-
Notifications
You must be signed in to change notification settings - Fork 68
/
Counter.swift
63 lines (52 loc) · 1.59 KB
/
Counter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Counter.swift
// RxFeedback
//
// Created by Krunoslav Zaher on 4/30/17.
// Copyright © 2017 Krunoslav Zaher. All rights reserved.
//
import UIKit
import RxSwift
import RxCocoa
import RxFeedback
class CounterViewController: UIViewController {
@IBOutlet weak var label: UILabel?
@IBOutlet weak var minus: UIButton?
@IBOutlet weak var plus: UIButton?
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
typealias State = Int
enum Event {
case increment
case decrement
}
Observable.system(
initialState: 0,
reduce: { (state, event) -> State in
switch event {
case .increment:
return state 1
case .decrement:
return state - 1
}
},
scheduler: MainScheduler.instance,
feedback:
// UI is user feedback
bind(self) { me, state -> Bindings<Event> in
let subscriptions = [
state.map(String.init).bind(to: me.label!.rx.text)
]
let events = [
me.plus!.rx.tap.map { Event.increment },
me.minus!.rx.tap.map { Event.decrement }
]
return Bindings(subscriptions: subscriptions,
events: events)
}
)
.subscribe()
.disposed(by: disposeBag)
}
}