/
ValueCellDataSource.swift
363 lines (287 loc) · 13.1 KB
/
ValueCellDataSource.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import Foundation
import UIKit
/**
A type-safe wrapper around a two-dimensional array of values that can be used to provide a data source for
`UICollectionView`s and `UITableView`s. There is no direct access to the two-dimensional array, and instead
values can be appended via public methods that make sure the value you are add to the data source matches
the type of value the table/collection cell can handle.
*/
open class ValueCellDataSource: NSObject, UICollectionViewDataSource, UITableViewDataSource {
private var values: [[(value: Any, reusableId: String)]] = []
/**
Override this method to destructure `cell` and `value` in order to call the `configureWith(value:)` method
on the cell with the value. This method is called by the internals of `ValueCellDataSource`, it does not
need to be called directly.
- parameter cell: A cell that is about to be displayed.
- parameter value: A value that is associated with the cell.
*/
open func configureCell(collectionCell cell: UICollectionViewCell, with value: Any, for indexPath: IndexPath) {
}
/**
Override this method to destructure `cell` and `value` in order to call the `configureWith(value:)` method
on the cell with the value. This method is called by the internals of `ValueCellDataSource`, it does not
need to be called directly.
- parameter cell: A cell that is about to be displayed.
- parameter value: A value that is associated with the cell.
*/
open func configureCell(tableCell cell: UITableViewCell, with value: Any, for indexPath: IndexPath) {
}
/**
Override this to perform any registrations of cell classes and nibs. Call this method from your controller
before the data source is set on the collection view. If you are using prototype cells you do not need
to call this.
- parameter collectionView: A collection view that needs to have cells registered.
*/
open func registerClasses(collectionView: UICollectionView?) {
}
/**
Override this to perform any registrations of cell classes and nibs. Call this method from your controller
before the data source is set on the table view. If you are using prototype cells you do not need
to call this.
- parameter tableView: A table view that needs to have cells registered.
*/
open func registerClasses(tableView: UITableView?) {
}
/**
Removes all values from the data source.
*/
public final func clearValues() {
self.values = [[]]
}
/**
Clears all the values stored in a particular section.
- parameter section: A section index.
*/
public final func clearValues(section: Int) {
self.padValuesForSection(section)
self.values[section] = []
}
/**
Inserts a single value at the beginning of the section specified.
- parameter value: A value to prepend.
- parameter cellClass: The type of cell associated with the value.
- parameter section: The section to append the value to.
- returns: The index path of the prepended row.
*/
@discardableResult
public final func prependRow <
Cell: ValueCell,
Value: Any>
(value: Value, cellClass: Cell.Type, toSection section: Int) -> IndexPath
where
Cell.Value == Value {
self.padValuesForSection(section)
self.values[section].insert((value, Cell.defaultReusableId), at: 0)
return IndexPath(row: 0, section: section)
}
/**
Adds a single value to the end of the section specified.
- parameter value: A value to append.
- parameter cellClass: The type of cell associated with the value.
- parameter section: The section to append the value to.
- returns: The index path of the appended row.
*/
@discardableResult
public final func appendRow <
Cell: ValueCell,
Value: Any>
(value: Value, cellClass: Cell.Type, toSection section: Int) -> IndexPath
where
Cell.Value == Value {
self.padValuesForSection(section)
self.values[section].append((value, Cell.defaultReusableId))
return IndexPath(row: self.values[section].count - 1, section: section)
}
/**
Inserts a single value at the index and section specified.
- parameter value: A value to append.
- parameter cellClass: The type of cell associated with the value.
- parameter index: The index to insert the value into
- parameter section: The section to insert the value into
- returns: The index path of the inserted row.
*/
@discardableResult
public final func insertRow<Cell: ValueCell, Value: Any>(value: Value,
cellClass: Cell.Type,
atIndex index: Int,
inSection section: Int) -> IndexPath
where Cell.Value == Value {
self.padValuesForSection(section)
self.values[section].insert((value, Cell.defaultReusableId), at: index)
return IndexPath(row: index, section: section)
}
public final func deleteRow<Cell: ValueCell, Value: Any>(value: Value,
cellClass: Cell.Type,
atIndex index: Int,
inSection section: Int) -> IndexPath
where Cell.Value == Value {
self.padValuesForSection(section)
self.values[section].remove(at: index)
return IndexPath(row: index, section: section)
}
/**
Adds a single row to the end of a section without specifying a value. This can be useful for
providing static rows.
- parameter cellIdentifier: The cell identifier of the static row in your table view.
- parameter section: The section to append the row to.
*/
public final func appendStaticRow(cellIdentifier: String, toSection section: Int) {
self.padValuesForSection(section)
self.values[section].append(((), cellIdentifier))
}
/**
Sets an entire section of static cells.
- parameter cellIdentifiers: A list of cell identifiers that represent the rows.
- parameter section: The section to replace.
*/
public final func set(cellIdentifiers: [String], inSection section: Int) {
self.padValuesForSection(section)
self.values[section] = cellIdentifiers.map { ((), $0) }
}
/**
Appends a section of values to the end of the data source.
- parameter values: An array of values that make up the section.
- parameter cellClass: The type of cell associated with all the values.
*/
public final func appendSection <
Cell: ValueCell,
Value: Any>
(values: [Value], cellClass: Cell.Type)
where
Cell.Value == Value {
self.values.append(values.map { ($0, Cell.defaultReusableId) })
}
/**
Replaces a section with values.
- parameter values: An array of values to replace the section with.
- parameter cellClass: The type of cell associated with the values.
- parameter section: The section to replace.
*/
public final func set <
Cell: ValueCell,
Value: Any>
(values: [Value], cellClass: Cell.Type, inSection section: Int)
where
Cell.Value == Value {
self.padValuesForSection(section)
self.values[section] = values.map { ($0, Cell.defaultReusableId) }
}
/**
Replaces a row with a value.
- parameter value: A value to replace the row with.
- parameter cellClass: The type of cell associated with the value.
- parameter section: The section for the row.
- parameter row: The row to replace.
*/
public final func set <
Cell: ValueCell,
Value: Any>
(value: Value, cellClass: Cell.Type, inSection section: Int, row: Int)
where
Cell.Value == Value {
self.values[section][row] = (value, Cell.defaultReusableId)
}
/**
- parameter indexPath: An index path to retrieve a value.
- returns: The value at the index path given.
*/
public final subscript(indexPath: IndexPath) -> Any {
return self.values[indexPath.section][indexPath.item].value
}
/**
- parameter section: The section to retrieve a value.
- parameter item: The item to retrieve a value.
- returns: The value at the section, item given.
*/
public final subscript(itemSection itemSection: (item: Int, section: Int)) -> Any {
return self.values[itemSection.section][itemSection.item].value
}
/**
- parameter section: The section to retrieve a value.
- returns: The array of values in the section.
*/
public final subscript(section section: Int) -> [Any] {
return self.values[section].map { $0.value }
}
/**
- returns: The total number of items in the data source.
*/
public final func numberOfItems() -> Int {
return self.values.reduce(0) { accum, section in accum + section.count }
}
/**
- returns: The total number of items in given section, in the data source.
*/
public final func numberOfItems(in section: Int) -> Int {
return self.values[section].count
}
/**
- parameter indexPath: An index path that we want to convert to a linear index.
- returns: A linear index representation of the index path.
*/
public final func itemIndexAt(_ indexPath: IndexPath) -> Int {
return self.values[0..<indexPath.section]
.reduce(indexPath.item) { accum, section in accum + section.count }
}
// MARK: UICollectionViewDataSource methods
public final func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.values.count
}
public final func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.values[section].count
}
public final func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let (value, reusableId) = self.values[indexPath.section][indexPath.item]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reusableId, for: indexPath)
self.configureCell(collectionCell: cell, with: value, for: indexPath)
return cell
}
// MARK: UITableViewDataSource methods
public final func numberOfSections(in tableView: UITableView) -> Int {
return self.values.count
}
public final func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.values[section].count
}
public final func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let (value, reusableId) = self.values[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: reusableId, for: indexPath)
self.configureCell(tableCell: cell, with: value, for: indexPath)
return cell
}
/**
- parameter item: An item index.
- parameter section: A section index.
- returns: The reusableId associated with an (item, section) pair. Marked as internal as it's
only useful for testing.
*/
internal final func reusableId(item: Int, section: Int) -> String? {
if !self.values.isEmpty && self.values.count >= section &&
!self.values[section].isEmpty && self.values[section].count >= item {
return self.values[section][item].reusableId
}
return nil
}
/**
Only useful for testing.
- parameter itemSection: A pair containing an item index and a section index.
- returns: The value of Any? type that is contained within the section at the item index.
*/
internal final subscript(testItemSection itemSection: (item: Int, section: Int)) -> Any? {
let (item, section) = itemSection
if !self.values.isEmpty && self.values.count >= section &&
!self.values[section].isEmpty && self.values[section].count >= item {
return self.values[itemSection.section][itemSection.item].value
}
return nil
}
private func padValuesForSection(_ section: Int) {
guard self.values.count <= section else { return }
(self.values.count...section).forEach { _ in
self.values.append([])
}
}
}