Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,76 @@ open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCol
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 {
return { dataSource.collectionNode(node, nodeForItemAt: indexPath) }
}

fileprivate static func configureSupplementaryViewBlockNotSet(dataSource: ASCollectionSectionedDataSource<S>, node: ASCollectionNode, nodeForSupplementaryElementOfKind kind: String, indexPath: IndexPath) -> ASCellNodeBlock {
return { dataSource.collectionNode(node, nodeForSupplementaryElementOfKind: kind, at: indexPath) }
}

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
}
Expand Down Expand Up @@ -92,10 +150,34 @@ open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCol
}
}

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
}
}
Expand All @@ -107,6 +189,7 @@ open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCol
#endif
}
}

open var canMoveItemAtIndexPath: CanMoveItemAtIndexPath {
didSet {
#if DEBUG
Expand All @@ -131,13 +214,27 @@ open class ASCollectionSectionedDataSource<S: SectionModelType>: NSObject, ASCol
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, canMoveItemAt indexPath: IndexPath) -> Bool {
return canMoveItemAtIndexPath(self, indexPath)
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/DataSources/RxProxies/RxASCollectionDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ public extension Reactive where Base: ASCollectionNode {

return ControlEvent(events: source)
}

/// Reactive wrapper for `delegate` message `collectionNode(_:willBeginBatchFetchWith:)`
var willBeginBatchFetch: ControlEvent<ASBatchContext> {
let source: Observable<ASBatchContext> = self.delegate.methodInvoked(#selector(ASCollectionDelegate.collectionNode(_:willBeginBatchFetchWith:)))
.map { a in
return try castOrThrow(ASBatchContext.self, a[1])
}

return ControlEvent(events: source)
}

/// Reactive wrapper for `delegate` message `collectionNode(_:didSelectItemAtIndexPath:)`.
///
Expand Down
12 changes: 12 additions & 0 deletions Sources/DataSources/RxProxies/RxASTableDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ public extension Reactive where Base: ASTableNode {

return ControlEvent(events: source)
}

/**
Reactive wrapper for `delegate` message `tableNode:willBeginBatchFetchWith`
*/
var willBeginBatchFetch: ControlEvent<ASBatchContext> {
let source: Observable<ASBatchContext> = self.delegate.methodInvoked(#selector(ASTableDelegate.tableNode(_:willBeginBatchFetchWith:)))
.map { a in
return try castOrThrow(ASBatchContext.self, a[1])
}

return ControlEvent(events: source)
}

/**
Reactive wrapper for `delegate` message `tableNode:didSelectRowAtIndexPath:`.
Expand Down