Guest User

Untitled

a guest
May 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. public protocol InitializableCell {
  2. associatedtype T
  3.  
  4. init(item: T)
  5. }
  6.  
  7. public typealias ACCollectionNodeCellAction = (_ collectionNode: ASCollectionNode, _ indexPath: IndexPath, _ object: Any) -> Void
  8.  
  9. open class ASCollectionNodeAdapter<Provider: CollectionDataProvider, Cell: ASCellNode>:
  10. NSObject,
  11. ASCollectionDataSource,
  12. ASCollectionDelegate
  13. where Cell: InitializableCell, Provider.T == Cell.T
  14. {
  15. // MARK: - Delegates
  16. public var didSelectCell: ACCollectionNodeCellAction?
  17.  
  18. // MARK: - Private Properties
  19. let provider: Provider
  20. let collectionNode: ASCollectionNode
  21.  
  22. // MARK: - Lifecycle
  23. init(collectionNode: ASCollectionNode, provider: Provider) {
  24. self.collectionNode = collectionNode
  25. self.provider = provider
  26. super.init()
  27. setUp()
  28. }
  29.  
  30. func setUp() {
  31. collectionNode.dataSource = self
  32. collectionNode.delegate = self
  33. }
  34.  
  35. // MARK: - ASCollectionDataSource
  36.  
  37. public func numberOfSections(in collectionNode: ASCollectionNode) -> Int {
  38. return provider.numberOfSections()
  39. }
  40.  
  41. public func collectionNode(_ collectionNode: ASCollectionNode, numberOfItemsInSection section: Int) -> Int {
  42. return provider.numberOfItems(in: section)
  43. }
  44.  
  45. public func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock {
  46. return { [weak self] () -> ASCellNode in
  47.  
  48. guard let `self` = self, let item = self.provider.item(at: indexPath) else {
  49. return ASCellNode()
  50. }
  51. return Cell.init(item: item)
  52.  
  53.  
  54. }
  55. }
  56.  
  57. // MARK: - ASCollectionDelegate
  58. }
Add Comment
Please, Sign In to add comment