-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContents.swift
222 lines (174 loc) · 6.65 KB
/
Contents.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*:
# UIKit Sample
**Please build Ricemill framework with iOS simulator before you run this playground.**
#### Classes
- CounterViewController
- ViewModel
*/
//: You can try [SwiftUI Sample](@previous).
import Combine
import PlaygroundSupport
import Ricemill
import UIKit
final class CounterViewController: UIViewController {
let incrementButton: Button = {
let button = Button(type: .system)
button.setTitle("🔼", for: [])
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
let decrementButton: Button = {
let button = Button(type: .system)
button.setTitle("🔽", for: [])
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
let countLabel: UILabel = {
let label = UILabel(frame: .zero)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let counterToggle: Toggle = {
let toggle = Toggle()
toggle.translatesAutoresizingMaskIntoConstraints = false
return toggle
}()
let counterStateLabel: UILabel = {
let label = UILabel(frame: .zero)
label.textAlignment = .left
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let viewModel = ViewModel()
private var cancellables: [AnyCancellable] = []
override func viewDidLoad() {
super.viewDidLoad()
do {
view.backgroundColor = .white
let verticalStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [
incrementButton,
countLabel,
decrementButton,
])
stackView.axis = .vertical
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
view.addSubview(verticalStackView)
NSLayoutConstraint.activate([
view.centerXAnchor.constraint(equalTo: verticalStackView.centerXAnchor),
view.centerYAnchor.constraint(equalTo: verticalStackView.centerYAnchor),
])
let horizontalStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [
counterStateLabel,
counterToggle,
])
stackView.spacing = 8
stackView.axis = .horizontal
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
verticalStackView.addArrangedSubview(horizontalStackView)
}
do {
let output = viewModel.output
output.count
.assign(to: \.text, on: countLabel)
.store(in: &cancellables)
output.isIncrementEnabled
.assign(to: \.isEnabled, on: incrementButton)
.store(in: &cancellables)
output.isDecrementEnabled
.assign(to: \.isEnabled, on: decrementButton)
.store(in: &cancellables)
output.isIncrementEnabled
.map { $0 ? 1 : 0.5 }
.assign(to: \.alpha, on: incrementButton)
.store(in: &cancellables)
output.isDecrementEnabled
.map { $0 ? 1 : 0.5 }
.assign(to: \.alpha, on: decrementButton)
.store(in: &cancellables)
output.toggleText
.assign(to: \.text, on: counterStateLabel)
.store(in: &cancellables)
}
do {
let input = viewModel.input
incrementButton.publisher(for: .touchUpInside)
.map { _ in () }
.subscribe(input.increment)
.store(in: &cancellables)
decrementButton.publisher(for: .touchUpInside)
.map { _ in () }
.subscribe(input.decrement)
.store(in: &cancellables)
counterToggle.publisher()
.subscribe(input.isOn)
.store(in: &cancellables)
}
}
}
final class ViewModel: Machine<ViewModel> {
convenience init() {
self.init(input: Input(), store: Store(), extra: Extra())
}
struct Input: InputType {
let increment = PassthroughSubject<Void, Never>()
let decrement = PassthroughSubject<Void, Never>()
let isOn = PassthroughSubject<Bool, Never>()
}
struct Output: OutputType {
let count: AnyPublisher<String?, Never>
let isIncrementEnabled: AnyPublisher<Bool, Never>
let isDecrementEnabled: AnyPublisher<Bool, Never>
let toggleText: AnyPublisher<String?, Never>
}
final class Store: StoreType {
@Published var count: Int = 0
@Published var isToggleEnabled = false
}
struct Extra: ExtraType {}
static func polish(input: Publishing<Input>,
store: Store,
extra: Extra) -> Polished<Output> {
var cancellables: [AnyCancellable] = []
do {
let increment = input.increment
.flatMap { _ in Just(store.count) }
.map { $0 + 1 }
let decrement = input.decrement
.flatMap { _ in Just(store.count) }
.map { $0 > 0 ? $0 - 1 : $0 }
increment.merge(with: decrement)
.assign(to: \.count, on: store)
.store(in: &cancellables)
}
input.isOn
.assign(to: \.isToggleEnabled, on: store)
.store(in: &cancellables)
let count = store.$count
.map(String.init)
.map(Optional.some)
.eraseToAnyPublisher()
let incrementEnabled = store.$isToggleEnabled
.eraseToAnyPublisher()
let isDecrementEnabled = store.$isToggleEnabled
.combineLatest(store.$count)
.map { $0 && $1 > 0 }
.eraseToAnyPublisher()
let toggleText = store.$isToggleEnabled
.map { $0 ? "Enabled" : "Disabled" }
.map(Optional.some)
.eraseToAnyPublisher()
return Polished(output: Output(count: count,
isIncrementEnabled: incrementEnabled,
isDecrementEnabled: isDecrementEnabled,
toggleText: toggleText),
cancellables: cancellables)
}
}
PlaygroundPage.current.liveView = CounterViewController()