Skip to content

Commit 47338d3

Browse files
fumito-itodangthaison91
authored andcommitted
accept nodeBlock* delegates for ASCollectionNode (#23)
* add reactive properties for ASBatchContext * add constructor and properties for ASCollectionNode's ASCellBlock delegate * add warning
1 parent afd0a51 commit 47338d3

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

Sources/DataSources/ASCollectionNode+Rx/ASCollectionSectionedDataSource.swift

+97
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,76 @@ open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCol
1818
public typealias Section = S
1919

2020
public typealias ConfigureCell = (ASCollectionSectionedDataSource<S>, ASCollectionNode, IndexPath, I) -> ASCellNode
21+
public typealias ConfigureCellBlock = (ASCollectionSectionedDataSource<S>, ASCollectionNode, IndexPath, I) -> ASCellNodeBlock
2122
public typealias ConfigureSupplementaryView = (ASCollectionSectionedDataSource<S>, ASCollectionNode, String, IndexPath) -> ASCellNode
23+
public typealias ConfigureSupplementaryViewBlock = (ASCollectionSectionedDataSource<S>, ASCollectionNode, String, IndexPath) -> ASCellNodeBlock
2224
public typealias MoveItem = (ASCollectionSectionedDataSource<S>, _ sourceIndexPath: IndexPath, _ destinationIndexPath:IndexPath) -> Void
2325
public typealias CanMoveItemAtIndexPath = (ASCollectionSectionedDataSource<S>, IndexPath) -> Bool
2426

27+
fileprivate static func configureCellNotSet(dataSource: ASCollectionSectionedDataSource<S>, node: ASCollectionNode, indexPath: IndexPath, model: I) -> ASCellNode {
28+
return ASCollectionDataSourceNotSet().collectionNode(node, nodeForItemAt: indexPath)
29+
}
30+
31+
fileprivate static func configureCellBlockNotSet(dataSource: ASCollectionSectionedDataSource<S>, node: ASCollectionNode, indexPath: IndexPath, model: I) -> ASCellNodeBlock {
32+
return { dataSource.collectionNode(node, nodeForItemAt: indexPath) }
33+
}
34+
35+
fileprivate static func configureSupplementaryViewBlockNotSet(dataSource: ASCollectionSectionedDataSource<S>, node: ASCollectionNode, nodeForSupplementaryElementOfKind kind: String, indexPath: IndexPath) -> ASCellNodeBlock {
36+
return { dataSource.collectionNode(node, nodeForSupplementaryElementOfKind: kind, at: indexPath) }
37+
}
38+
2539
public init(
2640
configureCell: @escaping ConfigureCell,
2741
configureSupplementaryView: ConfigureSupplementaryView? = nil,
2842
moveItem: @escaping MoveItem = { _, _, _ in () },
2943
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
3044
) {
3145
self.configureCell = configureCell
46+
self.configureCellBlock = ASCollectionSectionedDataSource.configureCellBlockNotSet
47+
self.configureSupplementaryView = configureSupplementaryView
48+
self.configureSupplementaryViewBlock = ASCollectionSectionedDataSource.configureSupplementaryViewBlockNotSet
49+
self.moveItem = moveItem
50+
self.canMoveItemAtIndexPath = canMoveItemAtIndexPath
51+
}
52+
53+
public init(
54+
configureCellBlock: @escaping ConfigureCellBlock,
55+
configureSupplementaryView: ConfigureSupplementaryView? = nil,
56+
moveItem: @escaping MoveItem = { _, _, _ in () },
57+
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
58+
) {
59+
self.configureCell = ASCollectionSectionedDataSource.configureCellNotSet
60+
self.configureCellBlock = configureCellBlock
3261
self.configureSupplementaryView = configureSupplementaryView
62+
self.configureSupplementaryViewBlock = ASCollectionSectionedDataSource.configureSupplementaryViewBlockNotSet
63+
self.moveItem = moveItem
64+
self.canMoveItemAtIndexPath = canMoveItemAtIndexPath
65+
}
66+
67+
public init(
68+
configureCell: @escaping ConfigureCell,
69+
configureSupplementaryViewBlock: ConfigureSupplementaryViewBlock? = nil,
70+
moveItem: @escaping MoveItem = { _, _, _ in () },
71+
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
72+
) {
73+
self.configureCell = configureCell
74+
self.configureCellBlock = ASCollectionSectionedDataSource.configureCellBlockNotSet
75+
self.configureSupplementaryView = nil
76+
self.configureSupplementaryViewBlock = configureSupplementaryViewBlock
77+
self.moveItem = moveItem
78+
self.canMoveItemAtIndexPath = canMoveItemAtIndexPath
79+
}
80+
81+
public init(
82+
configureCellBlock: @escaping ConfigureCellBlock,
83+
configureSupplementaryViewBlock: ConfigureSupplementaryViewBlock? = nil,
84+
moveItem: @escaping MoveItem = { _, _, _ in () },
85+
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
86+
) {
87+
self.configureCell = ASCollectionSectionedDataSource.configureCellNotSet
88+
self.configureCellBlock = configureCellBlock
89+
self.configureSupplementaryView = nil
90+
self.configureSupplementaryViewBlock = configureSupplementaryViewBlock
3391
self.moveItem = moveItem
3492
self.canMoveItemAtIndexPath = canMoveItemAtIndexPath
3593
}
@@ -92,10 +150,34 @@ open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCol
92150
}
93151
}
94152

153+
open var configureCellBlock: ConfigureCellBlock {
154+
didSet {
155+
#if DEBUG
156+
ensureNotMutatedAfterBinding()
157+
#endif
158+
}
159+
}
160+
95161
open var configureSupplementaryView: ConfigureSupplementaryView? {
96162
didSet {
97163
#if DEBUG
98164
ensureNotMutatedAfterBinding()
165+
166+
if self.configureSupplementaryViewBlock != nil {
167+
print("[WARNING][RxASDataSources] `configureSupplementaryView` is always over written by `configureSupplementaryViewBlock`.")
168+
}
169+
#endif
170+
}
171+
}
172+
173+
open var configureSupplementaryViewBlock: ConfigureSupplementaryViewBlock? {
174+
didSet {
175+
#if DEBUG
176+
ensureNotMutatedAfterBinding()
177+
178+
if self.configureSupplementaryView != nil {
179+
print("[WARNING][RxASDataSources] `configureSupplementaryViewBlock` always over write `configureSupplementaryView`.")
180+
}
99181
#endif
100182
}
101183
}
@@ -107,6 +189,7 @@ open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCol
107189
#endif
108190
}
109191
}
192+
110193
open var canMoveItemAtIndexPath: CanMoveItemAtIndexPath {
111194
didSet {
112195
#if DEBUG
@@ -131,13 +214,27 @@ open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCol
131214
return configureCell(self, collectionNode, indexPath, self[indexPath])
132215
}
133216

217+
open func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock {
218+
precondition(indexPath.item < _sectionModels[indexPath.section].items.count)
219+
220+
return configureCellBlock(self, collectionNode, indexPath, self[indexPath])
221+
}
222+
134223
open func collectionNode(_ collectionNode: ASCollectionNode, nodeForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> ASCellNode {
135224
guard let cell = configureSupplementaryView?(self, collectionNode, kind, indexPath) else {
136225
fatalError("configureSupplementaryView was not set")
137226
}
138227
return cell
139228
}
140229

230+
open func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> ASCellNodeBlock {
231+
guard let cell = configureSupplementaryViewBlock?(self, collectionNode, kind, indexPath) else {
232+
fatalError("configureSUpplementaryViewBlock was not set")
233+
}
234+
235+
return cell
236+
}
237+
141238
open func collectionNode(_ collectionNode: ASCollectionNode, canMoveItemAt indexPath: IndexPath) -> Bool {
142239
return canMoveItemAtIndexPath(self, indexPath)
143240
}

Sources/DataSources/RxProxies/RxASCollectionDelegateProxy.swift

+10
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ public extension Reactive where Base: ASCollectionNode {
121121

122122
return ControlEvent(events: source)
123123
}
124+
125+
/// Reactive wrapper for `delegate` message `collectionNode(_:willBeginBatchFetchWith:)`
126+
var willBeginBatchFetch: ControlEvent<ASBatchContext> {
127+
let source: Observable<ASBatchContext> = self.delegate.methodInvoked(#selector(ASCollectionDelegate.collectionNode(_:willBeginBatchFetchWith:)))
128+
.map { a in
129+
return try castOrThrow(ASBatchContext.self, a[1])
130+
}
131+
132+
return ControlEvent(events: source)
133+
}
124134

125135
/// Reactive wrapper for `delegate` message `collectionNode(_:didSelectItemAtIndexPath:)`.
126136
///

Sources/DataSources/RxProxies/RxASTableDelegateProxy.swift

+12
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ public extension Reactive where Base: ASTableNode {
128128

129129
return ControlEvent(events: source)
130130
}
131+
132+
/**
133+
Reactive wrapper for `delegate` message `tableNode:willBeginBatchFetchWith`
134+
*/
135+
var willBeginBatchFetch: ControlEvent<ASBatchContext> {
136+
let source: Observable<ASBatchContext> = self.delegate.methodInvoked(#selector(ASTableDelegate.tableNode(_:willBeginBatchFetchWith:)))
137+
.map { a in
138+
return try castOrThrow(ASBatchContext.self, a[1])
139+
}
140+
141+
return ControlEvent(events: source)
142+
}
131143

132144
/**
133145
Reactive wrapper for `delegate` message `tableNode:didSelectRowAtIndexPath:`.

0 commit comments

Comments
 (0)