This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathDataSourceProvider.swift
More file actions
173 lines (144 loc) · 6.98 KB
/
DataSourceProvider.swift
File metadata and controls
173 lines (144 loc) · 6.98 KB
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
//
// Created by Jesse Squires
// https://www.jessesquires.com
//
//
// Documentation
// https://jessesquires.github.io/JSQDataSourcesKit
//
//
// GitHub
// https://github.com/jessesquires/JSQDataSourcesKit
//
//
// License
// Copyright © 2015-present Jesse Squires
// Released under an MIT license: https://opensource.org/licenses/MIT
//
import Foundation
import UIKit
/// A `DataSourceProvider` is responsible for providing a data source object for a table view or collection view.
public final class DataSourceProvider < DataSource: DataSourceProtocol,
CellConfig: ReusableViewConfigProtocol,
SupplementaryConfig: ReusableViewConfigProtocol>
where CellConfig.Item == DataSource.Item, SupplementaryConfig.Item == DataSource.Item {
// MARK: Properties
/// The data source.
public var dataSource: DataSource
/// The cell configuration.
public let cellConfig: CellConfig
/// The supplementary view configuration.
public let supplementaryConfig: SupplementaryConfig
private var bridgedDataSource: BridgedDataSource?
private var tableEditingController: TableEditingController<DataSource>?
// MARK: Initialization
/// Initializes a new data source provider.
///
/// - Parameters:
/// - dataSource: The data source.
/// - cellConfig: The cell configuration.
/// - supplementaryConfig: The supplementary view configuration.
///
/// - Warning: Table views do not have supplementary views, but this parameter is still required in order to satisfy
/// the generic constraints for Swift. You can simply pass the same `cellConfig` here. The parameter will be ignored.
/// The same applies to collection views that do not have supplementary views. Again, the parameter will be ignored.
public init(dataSource: DataSource,
cellConfig: CellConfig,
supplementaryConfig: SupplementaryConfig) {
self.dataSource = dataSource
self.cellConfig = cellConfig
self.supplementaryConfig = supplementaryConfig
}
}
extension DataSourceProvider where CellConfig.View: UITableViewCell {
/// Initializes a new data source provider.
///
/// - Parameters:
/// - dataSource: The data source.
/// - cellConfig: The cell configuration.
/// - supplementaryConfig: The supplementary view configuration.
/// - tableEditingController: The table editing controller.
///
/// - Warning: Table views do not have supplementary views, but this parameter is still required in order to satisfy
/// the generic constraints for Swift. You can simply pass the same `cellConfig` here. The parameter will be ignored.
/// The same applies to collection views that do not have supplementary views. Again, the parameter will be ignored.
public convenience init(dataSource: DataSource,
cellConfig: CellConfig,
supplementaryConfig: SupplementaryConfig,
tableEditingController: TableEditingController<DataSource>? = nil) {
self.init(dataSource: dataSource, cellConfig: cellConfig, supplementaryConfig: supplementaryConfig)
self.tableEditingController = tableEditingController
}
// MARK: UITableViewDataSource
/// Returns the `UITableViewDataSource` object.
public var tableViewDataSource: UITableViewDataSource {
if bridgedDataSource == nil {
bridgedDataSource = tableViewBridgedDataSource()
}
return bridgedDataSource!
}
private func tableViewBridgedDataSource() -> BridgedDataSource {
let dataSource = BridgedDataSource(
numberOfSections: { [unowned self] () -> Int in
self.dataSource.numberOfSections()
},
numberOfItemsInSection: { [unowned self] section -> Int in
self.dataSource.numberOfItems(inSection: section)
})
dataSource.tableCellForRowAtIndexPath = { [unowned self] tableView, indexPath -> UITableViewCell in
let item = self.dataSource.item(atIndexPath: indexPath)!
return self.cellConfig.tableCellFor(item: item, tableView: tableView, indexPath: indexPath)
}
dataSource.tableTitleForHeaderInSection = { [unowned self] section -> String? in
self.dataSource.headerTitle(inSection: section)
}
dataSource.tableTitleForFooterInSection = { [unowned self] section -> String? in
self.dataSource.footerTitle(inSection: section)
}
dataSource.tableCanEditRow = { [unowned self] tableView, indexPath -> Bool in
guard let controller = self.tableEditingController else { return false }
let item = self.dataSource.item(atIndexPath: indexPath)!
return controller.canEditRow(item, tableView, indexPath)
}
dataSource.tableCommitEditingStyleForRow = { [unowned self] tableView, editingStyle, indexPath in
self.tableEditingController?.commitEditing(&self.dataSource, tableView, editingStyle, indexPath)
}
return dataSource
}
}
extension DataSourceProvider where CellConfig.View: UICollectionViewCell, SupplementaryConfig.View: UICollectionReusableView {
// MARK: UICollectionViewDataSource
/// Returns the `UICollectionViewDataSource` object.
public var collectionViewDataSource: UICollectionViewDataSource {
if bridgedDataSource == nil {
bridgedDataSource = collectionViewBridgedDataSource()
}
return bridgedDataSource!
}
private func collectionViewBridgedDataSource() -> BridgedDataSource {
let dataSource = BridgedDataSource(
numberOfSections: { [unowned self] () -> Int in
self.dataSource.numberOfSections()
},
numberOfItemsInSection: { [unowned self] section -> Int in
self.dataSource.numberOfItems(inSection: section)
})
dataSource.collectionCellForItemAtIndexPath = { [unowned self] collectionView, indexPath -> UICollectionViewCell in
let item = self.dataSource.item(atIndexPath: indexPath)!
return self.cellConfig.collectionCellFor(item: item, collectionView: collectionView, indexPath: indexPath)
}
dataSource.collectionSupplementaryViewAtIndexPath = { [unowned self] collectionView, kind, indexPath -> UICollectionReusableView in
var item: SupplementaryConfig.Item?
if indexPath.section < self.dataSource.numberOfSections() {
if indexPath.item < self.dataSource.numberOfItems(inSection: indexPath.section) {
item = self.dataSource.item(atIndexPath: indexPath)
}
}
return self.supplementaryConfig.supplementaryViewFor(item: item,
kind: kind,
collectionView: collectionView,
indexPath: indexPath)
}
return dataSource
}
}