forked from venmo/Static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSource.swift
209 lines (162 loc) · 6.65 KB
/
DataSource.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
import UIKit
/// Table view data source.
///
/// You should always access this object from the main thread since it talks to UIKit.
public class DataSource: NSObject {
// MARK: - Properties
/// The table view that will use this object as its data source.
public weak var tableView: UITableView? {
willSet {
if let tableView = tableView {
tableView.dataSource = nil
tableView.delegate = nil
}
}
didSet {
assert(NSThread.isMainThread(), "You must access Static.DataSource from the main thread.")
updateTableView()
}
}
/// Sections to use in the table view.
public var sections: [Section] {
didSet {
assert(NSThread.isMainThread(), "You must access Static.DataSource from the main thread.")
refresh()
}
}
private var registeredCellIdentifiers = Set<String>()
// MARK: - Initializers
/// Initialize with optional `tableView` and `sections`.
public init(tableView: UITableView? = nil, sections: [Section]? = nil) {
self.tableView = tableView
self.sections = sections ?? []
super.init()
updateTableView()
}
deinit {
// nil out the table view to ensure the table view data source and delegate niled out
tableView = nil
}
// MARK: - Private
private func updateTableView() {
guard let tableView = tableView else { return }
tableView.dataSource = self
tableView.delegate = self
refresh()
}
private func refresh() {
refreshTableSections()
refreshRegisteredCells()
}
private func sectionForIndex(index: Int) -> Section? {
if sections.count <= index {
assert(false, "Invalid section index: \(index)")
return nil
}
return sections[index]
}
private func rowForIndexPath(indexPath: NSIndexPath) -> Row? {
if let section = sectionForIndex(indexPath.section) {
let rows = section.rows
if rows.count >= indexPath.row {
return rows[indexPath.row]
}
}
assert(false, "Invalid index path: \(indexPath)")
return nil
}
private func refreshTableSections(oldSections: [Section]? = nil) {
guard let tableView = tableView else { return }
guard let oldSections = oldSections else {
tableView.reloadData()
return
}
let oldCount = oldSections.count
let newCount = sections.count
let delta = newCount - oldCount
let animation: UITableViewRowAnimation = .Automatic
tableView.beginUpdates()
if delta == 0 {
tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, newCount)), withRowAnimation: animation)
} else {
if delta > 0 {
// Insert sections
tableView.insertSections(NSIndexSet(indexesInRange: NSMakeRange(oldCount - 1, delta)), withRowAnimation: animation)
} else {
// Remove sections
tableView.deleteSections(NSIndexSet(indexesInRange: NSMakeRange(oldCount - 1, -delta)), withRowAnimation: animation)
}
// Reload existing sections
let commonCount = min(oldCount, newCount)
tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, commonCount)), withRowAnimation: animation)
}
tableView.endUpdates()
}
private func refreshRegisteredCells() {
// Filter to only rows with unregistered cells
let rows = sections.map({ $0.rows }).reduce([], combine: +).filter() {
!self.registeredCellIdentifiers.contains($0.cellIdentifier)
}
for row in rows {
let identifier = row.cellIdentifier
// Check again in case there were duplicate new cell classes
if registeredCellIdentifiers.contains(identifier) {
continue
}
registeredCellIdentifiers.insert(identifier)
tableView?.registerClass(row.cellClass, forCellReuseIdentifier: identifier)
}
}
}
extension DataSource: UITableViewDataSource {
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionForIndex(section)?.rows.count ?? 0
}
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let row = rowForIndexPath(indexPath) {
let tableCell = tableView.dequeueReusableCellWithIdentifier(row.cellIdentifier, forIndexPath: indexPath)
if let cell = tableCell as? CellType {
cell.configure(row: row)
}
return tableCell
}
return UITableViewCell()
}
public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionForIndex(section)?.header?.title
}
public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return sectionForIndex(section)?.header?.view
}
public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return sectionForIndex(section)?.header?.viewHeight ?? UITableViewAutomaticDimension
}
public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return sectionForIndex(section)?.footer?.title
}
public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return sectionForIndex(section)?.footer?.view
}
public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return sectionForIndex(section)?.footer?.viewHeight ?? UITableViewAutomaticDimension
}
}
extension DataSource: UITableViewDelegate {
public func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return rowForIndexPath(indexPath)?.isSelectable ?? false
}
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
if let row = rowForIndexPath(indexPath) {
row.selection?()
}
}
public func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
if let row = rowForIndexPath(indexPath) {
row.accessory.selection?()
}
}
}