-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSteppingUpViewController.swift
105 lines (90 loc) · 3.52 KB
/
SteppingUpViewController.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright © 2017 Stefan van den Oord. All rights reserved.
import Foundation
import UIKit
import ReSwift
import RxSwift
import ReRxSwift
class SteppingUpViewController: UIViewController {
let connection = Connection(store: store,
mapStateToProps: mapStateToProps,
mapDispatchToActions: mapDispatchToActions)
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var stepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
connection.bind(\Props.value, to: slider.rx.value)
connection.bind(\Props.value, to: textField.rx.text, mapping: { String($0) })
connection.bind(\Props.value, to: progressView.rx.progress)
connection.bind(\Props.value, to: stepper.rx.value, mapping: { Double($0) })
// Binding the stepper requires https://github.com/ReactiveX/RxSwift/pull/1389 to be merged.
// Applying workaround: subscribe instead of bind.
// connection.bind(\Props.stepSize, to: stepper.rx.stepValue, mapping: { Double($0) })
connection.subscribe(\Props.stepSize) { self.stepper.stepValue = Double($0) }
connection.bind(\Props.stepSize, to: segmentedControl.rx.selectedSegmentIndex, mapping: { self.segmentIndex(for: $0) })
}
func segmentIndex(for stepSize: Float) -> Int {
for index in 0 ..< segmentedControl.numberOfSegments {
guard let title = segmentedControl.titleForSegment(at: index),
let segment = Float(title) else {
continue
}
if stepSize == segment {
return index
}
}
return -1
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
connection.connect()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
connection.disconnect()
}
@IBAction func sliderChanged() {
actions.setValue(slider.value)
}
@IBAction func textChanged() {
guard let text = textField.text, let newValue = Float(text) else {
return
}
actions.setValue(newValue)
}
@IBAction func stepperChanged() {
actions.setValue(Float(stepper.value))
}
@IBAction func selectedSegmentChanged() {
guard segmentedControl.selectedSegmentIndex >= 0,
let title = segmentedControl.titleForSegment(at: segmentedControl.selectedSegmentIndex),
let newStepSize = Float(title) else {
return
}
actions.setStepSize(newStepSize)
}
}
extension SteppingUpViewController: Connectable {
struct Props {
let value: Float
let stepSize: Float
}
struct Actions {
let setValue: (Float) -> ()
let setStepSize: (Float) -> ()
}
}
private let mapStateToProps = { (appState: AppState) in
return SteppingUpViewController.Props(value: appState.steppingUp.value,
stepSize: appState.steppingUp.stepSize)
}
private let mapDispatchToActions = { (dispatch: @escaping DispatchFunction) in
return SteppingUpViewController.Actions(
setValue: { newValue in dispatch(SteppingUpSetValue(newValue: newValue)) },
setStepSize: { newStepSize in
dispatch(SteppingUpSetStepSize(newStepSize: newStepSize))
}
)
}