/
TaskListViewController.swift
214 lines (184 loc) · 6.95 KB
/
TaskListViewController.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
import PersistDB
import ReactiveSwift
import Schemata
import UIKit
/// The view controller responsible for showing the list of tasks.
final class TaskListViewController: UIViewController {
/// The different segments/tabs within the task list.
enum Segment: Int {
/// All tasks--whether incomplete or complete
case all
/// The incomplete tasks
case active
/// The completed tasks
case completed
/// The title of the segment.
var title: String {
switch self {
case .all:
return "All"
case .active:
return "Active"
case .completed:
return "Completed"
}
}
/// The query matching the tasks for the segment.
var query: Query<None, Task> {
switch self {
case .all:
return Task.all
case .active:
return Task.active
case .completed:
return Task.completed
}
}
}
/// The store backing the application.
let store: Store<ReadWrite>
/// The currently viewed segment.
///
/// `MutableProperty` comes from ReactiveSwift. It allows this property to be observed,
/// making updating the query and store observation easy.
var segment = MutableProperty<Segment>(.all)
/// The table model backing the table view.
var table: Table<None, Task> = .init() {
didSet {
let diff = table.diff(from: oldValue)
tableView.performBatchUpdates(
{ // swiftlint:disable:this opening_brace
tableView.insertSections(diff.insertedGroups, with: .fade)
tableView.deleteSections(diff.deletedGroups, with: .fade)
diff.movedGroups.forEach(tableView.moveSection)
tableView.insertRows(at: diff.insertedValues, with: .fade)
tableView.deleteRows(at: diff.deletedValues, with: .fade)
diff.movedValues.forEach(tableView.moveRow)
}
)
for indexPath in diff.updatedValues {
let cell = tableView.cellForRow(at: indexPath) as? TaskCell
if let cell = cell {
cell.task = table[indexPath]
}
}
}
}
/// The control to switch between the different segments.
let segmentedControl = UISegmentedControl(
items: [
Segment.all.title,
Segment.active.title,
Segment.completed.title,
]
)
/// The text field for adding tasks.
let inputField = UITextField()
/// The table of tasks.
let tableView = UITableView()
init(store: Store<ReadWrite>) {
self.store = store
super.init(nibName: nil, bundle: nil)
}
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
segmentedControl.selectedSegmentIndex = Segment.all.rawValue
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(segmentedControl)
segmentedControl.addTarget(self, action: #selector(segmentDidChange), for: .valueChanged)
inputField.placeholder = "What needs to be done?"
inputField.returnKeyType = .done
inputField.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(inputField)
inputField.delegate = self
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(tableView)
tableView.dataSource = self
tableView.delegate = self
tableView.register(
TaskCell.self,
forCellReuseIdentifier: String(describing: Task.self)
)
let constraints = [
segmentedControl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
segmentedControl.centerXAnchor.constraint(equalTo: view.centerXAnchor),
inputField.topAnchor.constraint(
equalToSystemSpacingBelow: segmentedControl.bottomAnchor,
multiplier: 2
),
inputField.leadingAnchor.constraint(
equalToSystemSpacingAfter: view.leadingAnchor,
multiplier: 1
),
view.trailingAnchor.constraint(
equalToSystemSpacingAfter: inputField.trailingAnchor,
multiplier: 1
),
tableView.topAnchor.constraint(equalTo: inputField.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
]
constraints.forEach(view.addConstraint)
// Observe the tasks matching the query for the currently selected segment.
segment
.producer
.map { $0.query }
.flatMap(.latest) { [store = self.store] query in
return store.observe(query)
}
.startWithValues { [weak self] resultSet in
self?.table.resultSet = resultSet
}
}
@objc func segmentDidChange() {
segment.value = Segment(rawValue: segmentedControl.selectedSegmentIndex)!
}
}
extension TaskListViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
store.insert(Task.newTask(text: textField.text ?? ""))
textField.text = nil
textField.resignFirstResponder()
return true
}
}
extension TaskListViewController: UITableViewDataSource {
func numberOfSections(in _: UITableView) -> Int {
return table.sectionCount
}
func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
return table.rowCount(inSection: section)
}
func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView
.dequeueReusableCell(withIdentifier: String(describing: Task.self), for: indexPath)
as! TaskCell // swiftlint:disable:this force_cast
cell.task = table[indexPath]
return cell
}
func tableView(
_: UITableView,
commit editingStyle: UITableViewCell.EditingStyle,
forRowAt indexPath: IndexPath
) {
if editingStyle == .delete {
store.delete(Delete(\Task.id == table[indexPath].id))
}
}
}
extension TaskListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
let task = table[indexPath]
store.update(
Update(
predicate: \Task.id == task.id,
valueSet: task.isCompleted ? Task.incomplete : Task.complete
)
)
}
}