-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathASCollectionSectionedDataSource.swift
More file actions
251 lines (211 loc) · 10.7 KB
/
ASCollectionSectionedDataSource.swift
File metadata and controls
251 lines (211 loc) · 10.7 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
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
//
// ASCollectionSectionedDataSource.swift
// RxASDataSources
//
// Created by Dang Thai Son on 7/27/17.
// Copyright (c) 2017 RxSwiftCommunity. All rights reserved.
//
import Foundation
import AsyncDisplayKit
#if !RX_NO_MODULE
import RxCocoa
import Differentiator
#endif
open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCollectionDataSource, ASCommonCollectionDataSource, SectionedViewDataSourceType {
public typealias I = S.Item
public typealias Section = S
public typealias ConfigureCell = (ASCollectionSectionedDataSource<S>, ASCollectionNode, IndexPath, I) -> ASCellNode
public typealias ConfigureCellBlock = (ASCollectionSectionedDataSource<S>, ASCollectionNode, IndexPath, I) -> ASCellNodeBlock
public typealias ConfigureSupplementaryView = (ASCollectionSectionedDataSource<S>, ASCollectionNode, String, IndexPath) -> ASCellNode
public typealias ConfigureSupplementaryViewBlock = (ASCollectionSectionedDataSource<S>, ASCollectionNode, String, IndexPath) -> ASCellNodeBlock
public typealias MoveItem = (ASCollectionSectionedDataSource<S>, _ sourceIndexPath: IndexPath, _ destinationIndexPath:IndexPath) -> Void
public typealias CanMoveItemAtIndexPath = (ASCollectionSectionedDataSource<S>, IndexPath) -> Bool
fileprivate static func configureCellNotSet(dataSource: ASCollectionSectionedDataSource<S>, node: ASCollectionNode, indexPath: IndexPath, model: I) -> ASCellNode {
return ASCollectionDataSourceNotSet().collectionNode(node, nodeForItemAt: indexPath)
}
fileprivate static func configureCellBlockNotSet(dataSource: ASCollectionSectionedDataSource<S>, node: ASCollectionNode, indexPath: IndexPath, model: I) -> ASCellNodeBlock {
// Users expect collectionNode(_:nodeForItemAt:) will be executed in main thread
let cellNode = dataSource.collectionNode(node, nodeForItemAt: indexPath)
return { cellNode }
}
fileprivate static func configureSupplementaryViewBlockNotSet(dataSource: ASCollectionSectionedDataSource<S>, node: ASCollectionNode, nodeForSupplementaryElementOfKind kind: String, indexPath: IndexPath) -> ASCellNodeBlock {
// Users expect collectionNode(_nodeForSupplementaryElementOfKind:at:) will be executed in main thread according to api doc.
let cellNode = dataSource.collectionNode(node, nodeForSupplementaryElementOfKind: kind, at: indexPath)
return { cellNode }
}
public init(
configureCell: @escaping ConfigureCell,
configureSupplementaryView: ConfigureSupplementaryView? = nil,
moveItem: @escaping MoveItem = { _, _, _ in () },
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
) {
self.configureCell = configureCell
self.configureCellBlock = ASCollectionSectionedDataSource.configureCellBlockNotSet
self.configureSupplementaryView = configureSupplementaryView
self.configureSupplementaryViewBlock = ASCollectionSectionedDataSource.configureSupplementaryViewBlockNotSet
self.moveItem = moveItem
self.canMoveItemAtIndexPath = canMoveItemAtIndexPath
}
public init(
configureCellBlock: @escaping ConfigureCellBlock,
configureSupplementaryView: ConfigureSupplementaryView? = nil,
moveItem: @escaping MoveItem = { _, _, _ in () },
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
) {
self.configureCell = ASCollectionSectionedDataSource.configureCellNotSet
self.configureCellBlock = configureCellBlock
self.configureSupplementaryView = configureSupplementaryView
self.configureSupplementaryViewBlock = ASCollectionSectionedDataSource.configureSupplementaryViewBlockNotSet
self.moveItem = moveItem
self.canMoveItemAtIndexPath = canMoveItemAtIndexPath
}
public init(
configureCell: @escaping ConfigureCell,
configureSupplementaryViewBlock: ConfigureSupplementaryViewBlock? = nil,
moveItem: @escaping MoveItem = { _, _, _ in () },
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
) {
self.configureCell = configureCell
self.configureCellBlock = ASCollectionSectionedDataSource.configureCellBlockNotSet
self.configureSupplementaryView = nil
self.configureSupplementaryViewBlock = configureSupplementaryViewBlock
self.moveItem = moveItem
self.canMoveItemAtIndexPath = canMoveItemAtIndexPath
}
public init(
configureCellBlock: @escaping ConfigureCellBlock,
configureSupplementaryViewBlock: ConfigureSupplementaryViewBlock? = nil,
moveItem: @escaping MoveItem = { _, _, _ in () },
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
) {
self.configureCell = ASCollectionSectionedDataSource.configureCellNotSet
self.configureCellBlock = configureCellBlock
self.configureSupplementaryView = nil
self.configureSupplementaryViewBlock = configureSupplementaryViewBlock
self.moveItem = moveItem
self.canMoveItemAtIndexPath = canMoveItemAtIndexPath
}
#if DEBUG
// If data source has already been bound, then mutating it
// afterwards isn't something desired.
// This simulates immutability after binding
var _dataSourceBound: Bool = false
private func ensureNotMutatedAfterBinding() {
assert(!_dataSourceBound, "Data source is already bound. Please write this line before binding call (`bindTo`, `drive`). Data source must first be completely configured, and then bound after that, otherwise there could be runtime bugs, glitches, or partial malfunctions.")
}
#endif
// This structure exists because model can be mutable
// In that case current state value should be preserved.
// The state that needs to be preserved is ordering of items in section
// and their relationship with section.
// If particular item is mutable, that is irrelevant for this logic to function
// properly.
public typealias SectionModelSnapshot = SectionModel<S, I>
private var _sectionModels: [SectionModelSnapshot] = []
open var sectionModels: [S] {
return _sectionModels.map { Section(original: $0.model, items: $0.items) }
}
open subscript(section: Int) -> S {
let sectionModel = self._sectionModels[section]
return S(original: sectionModel.model, items: sectionModel.items)
}
open subscript(indexPath: IndexPath) -> I {
get {
return self._sectionModels[indexPath.section].items[indexPath.item]
}
set(item) {
var section = self._sectionModels[indexPath.section]
section.items[indexPath.item] = item
self._sectionModels[indexPath.section] = section
}
}
open func model(at indexPath: IndexPath) throws -> Any {
return self[indexPath]
}
open func setSections(_ sections: [S]) {
self._sectionModels = sections.map { SectionModelSnapshot(model: $0, items: $0.items) }
}
open var configureCell: ConfigureCell {
didSet {
#if DEBUG
ensureNotMutatedAfterBinding()
#endif
}
}
open var configureCellBlock: ConfigureCellBlock {
didSet {
#if DEBUG
ensureNotMutatedAfterBinding()
#endif
}
}
open var configureSupplementaryView: ConfigureSupplementaryView? {
didSet {
#if DEBUG
ensureNotMutatedAfterBinding()
if self.configureSupplementaryViewBlock != nil {
print("[WARNING][RxASDataSources] `configureSupplementaryView` is always over written by `configureSupplementaryViewBlock`.")
}
#endif
}
}
open var configureSupplementaryViewBlock: ConfigureSupplementaryViewBlock? {
didSet {
#if DEBUG
ensureNotMutatedAfterBinding()
if self.configureSupplementaryView != nil {
print("[WARNING][RxASDataSources] `configureSupplementaryViewBlock` always over write `configureSupplementaryView`.")
}
#endif
}
}
open var moveItem: MoveItem {
didSet {
#if DEBUG
ensureNotMutatedAfterBinding()
#endif
}
}
open var canMoveItemAtIndexPath: CanMoveItemAtIndexPath {
didSet {
#if DEBUG
ensureNotMutatedAfterBinding()
#endif
}
}
//MARK:- ASCollectionNodeDataSource
open func numberOfSections(in collectionNode: ASCollectionNode) -> Int {
return _sectionModels.count
}
open func collectionNode(_ collectionNode: ASCollectionNode, numberOfItemsInSection section: Int) -> Int {
return _sectionModels[section].items.count
}
open func collectionNode(_ collectionNode: ASCollectionNode, nodeForItemAt indexPath: IndexPath) -> ASCellNode {
precondition(indexPath.item < _sectionModels[indexPath.section].items.count)
return configureCell(self, collectionNode, indexPath, self[indexPath])
}
open func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock {
precondition(indexPath.item < _sectionModels[indexPath.section].items.count)
return configureCellBlock(self, collectionNode, indexPath, self[indexPath])
}
open func collectionNode(_ collectionNode: ASCollectionNode, nodeForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> ASCellNode {
guard let cell = configureSupplementaryView?(self, collectionNode, kind, indexPath) else {
fatalError("configureSupplementaryView was not set")
}
return cell
}
open func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> ASCellNodeBlock {
guard let cell = configureSupplementaryViewBlock?(self, collectionNode, kind, indexPath) else {
fatalError("configureSUpplementaryViewBlock was not set")
}
return cell
}
open func collectionNode(_ collectionNode: ASCollectionNode, canMoveItemWith node: ASCellNode) -> Bool {
guard let indexPath = node.indexPath else { return false }
return canMoveItemAtIndexPath(self, indexPath)
}
open func collectionNode(_ collectionNode: ASCollectionNode, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
self._sectionModels.moveFromSourceIndexPath(sourceIndexPath, destinationIndexPath: destinationIndexPath)
self.moveItem(self, sourceIndexPath, destinationIndexPath)
}
}