Guest User

Untitled

a guest
May 24th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.86 KB | None | 0 0
  1. import UIKit
  2. import Foundation
  3. import ReactiveCocoa
  4. import Result
  5. import Dwifft
  6.  
  7. /**
  8. Encapsulates information about collection view nib/cell registration.
  9.  
  10. - Nib: UINib.
  11. - Class: UICollectionViewCell class.
  12. */
  13. private enum RegisteredType {
  14. case Nib(UINib)
  15. case Class(AnyClass)
  16. }
  17.  
  18. /// CollectionViewBinder is a ReactiveCocoa helper class for simplifying using `UICollectionView`s with ReactiveCocoa.
  19. /// Using the binder lets the view configuration for collection view cells move to the view and out of the view controller.
  20. final class CollectionViewBinder<T: Equatable>: NSObject, UICollectionViewDataSource, UICollectionViewDelegate {
  21.  
  22. /// `UICollectionViewDelegate` object.
  23. weak var delegate: UICollectionViewDelegate? {
  24. get { return self.collectionView?.delegate }
  25. set { self.collectionView?.delegate = newValue }
  26. }
  27.  
  28. /// `CollectionBinderDataSource` object.
  29. weak var dataSource: CollectionBinderDataSource? {
  30. willSet {
  31. if let _ = newValue {
  32. reloadData()
  33. }
  34. }
  35. }
  36.  
  37. /// `UICollectionView` associated with the binder.
  38. private(set) weak var collectionView: UICollectionView?
  39.  
  40. /// Signal that emits the data
  41. private var dataSignal: SignalProducer<[[T]], NoError>
  42. /// Signal that emits the supplementary data
  43. private var supplementarySignal: SignalProducer<[String: [T]], NoError>?
  44.  
  45. /// Mutable data property
  46. private let data = MutableProperty<[[T]]>([])
  47. /// Mutable supplementary data property
  48. private let supplementaryData: MutableProperty<[String: [T]]> = MutableProperty<[String: [T]]>([:])
  49.  
  50.  
  51. // MARK: - Registration Information
  52.  
  53. /// All the cells registered with the binder.
  54. private var cells: [String: RegisteredType] = [:]
  55. /// All the supplementary views registered with the binder.
  56. private var supplementaries: [String: (String, RegisteredType)] = [:]
  57. /// All the decoration views registered with the binder.z
  58. private var decorations: [String: RegisteredType] = [:]
  59.  
  60. /**
  61. Create a binding helper.
  62.  
  63. - parameter collectionView: Collection view object to bind data to.
  64. - parameter dataSignal: SignalProducer that emits changes to the underlying data.
  65. - parameter supplementarySignal: SignalProducer that emits changes to the data the represents headers, footers, etc.
  66. */
  67. init(collectionView: UICollectionView, dataSignal: SignalProducer<[[T]], NoError>, supplementarySignal: SignalProducer<[String: [T]], NoError>?) {
  68. self.collectionView = collectionView
  69. self.dataSignal = dataSignal
  70. self.supplementarySignal = supplementarySignal
  71.  
  72. super.init()
  73. self.data <~ self.dataSignal
  74. self.supplementaryData <~ self.supplementarySignal ?? SignalProducer.empty
  75. self.collectionView?.dataSource = self
  76.  
  77. self.data.producer.on(next: { print($0.count) }).on(next: { _ in self.reloadData() }).takeUntil(self.willDeallocSignal()).start()
  78. }
  79.  
  80. // MARK: - UICollectionViewDataSource
  81.  
  82. func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
  83. guard let _ = self.dataSource where !self.cells.isEmpty else {
  84. return 0
  85. }
  86. return self.data.value.count
  87. }
  88.  
  89.  
  90. func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  91. guard let _ = self.dataSource where !self.cells.isEmpty && section < self.data.value.count else {
  92. return 0
  93. }
  94. return self.data.value[section].count
  95. }
  96.  
  97. func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  98. guard let dataSource = self.dataSource else {
  99. return UICollectionViewCell()
  100. }
  101. let model = self.data.value[indexPath.section][indexPath.item]
  102. if let reuseIdentifier = dataSource.collectionView(collectionView, reuseIdentifierForItemAtIndexPath: indexPath) {
  103. let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
  104. if cell is ReactiveView {
  105. (cell as! ReactiveView).bindViewModel(model)
  106. }
  107. return cell
  108. }
  109. return UICollectionViewCell()
  110. }
  111.  
  112. func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
  113.  
  114. if let model = self.supplementaryData.value[kind]?[indexPath.section],
  115. reuseIdentifier = self.dataSource?.collectionView(collectionView, reuseIdentifierForSupplementaryViewOfKind: kind, atIndexPath: indexPath) {
  116. let view = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: reuseIdentifier, forIndexPath: indexPath)
  117. if view is ReactiveView {
  118. (view as! ReactiveView).bindViewModel(model)
  119. }
  120. return view
  121. }
  122. return UICollectionReusableView()
  123. }
  124. }
  125.  
  126. // MARK: - Registering Views
  127. extension CollectionViewBinder {
  128.  
  129. /**
  130. Register a nib for a cell reuse identifier.
  131.  
  132. - parameter nib: Nib object.
  133. - parameter identifier: Reuse identifier to register the specified nib with.
  134. */
  135. func registerNib(nib: UINib, forCellReuseIdentifier identifier: String) {
  136. self.cells[identifier] = .Nib(nib)
  137. self.collectionView?.registerNib(nib, forCellWithReuseIdentifier: identifier)
  138. }
  139.  
  140. /**
  141. Register a class for a cell reuse identifier.
  142.  
  143. - parameter aClass: Class object.
  144. - parameter identifier: Reuse identifier to register the specified class with.
  145. */
  146. func registerClass(aClass: AnyClass, forCellReuseIdentifier identifier: String) {
  147. self.cells[identifier] = .Class(aClass)
  148. self.collectionView?.registerClass(aClass, forCellWithReuseIdentifier: identifier)
  149. }
  150.  
  151. /**
  152. Register a nib for a supplementary reuse identifier.
  153.  
  154. - parameter nib: Nib object.
  155. - parameter kind: Supplementary element kind.
  156. - parameter identifier: Reuse identifier to register the specified nib with.
  157. */
  158. func registerNib(nib: UINib, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String) {
  159. self.supplementaries[kind] = (identifier, .Nib(nib))
  160. self.collectionView?.registerNib(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier)
  161. }
  162.  
  163. /**
  164. Register a class for a supplementary reuse identifier.
  165.  
  166. - parameter aClass: Class object.
  167. - parameter kind: Supplementary element kind.
  168. - parameter identifier: Reuse identifier to register the specified class with.
  169. */
  170. func registerClass(aClass: AnyClass, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String) {
  171. self.supplementaries[kind] = (identifier, .Class(aClass))
  172. self.collectionView?.registerClass(aClass, forSupplementaryViewOfKind: kind, withReuseIdentifier: identifier)
  173. }
  174.  
  175. /**
  176. Register a nib for a decoration kind.
  177.  
  178. - parameter nib: Nib object.
  179. - parameter elementKind: Decoration identifier.
  180. */
  181. func registerNib(nib: UINib, forDecorationViewOfKind elementKind: String) {
  182. self.decorations[elementKind] = .Nib(nib)
  183. self.collectionView?.collectionViewLayout.registerNib(nib, forDecorationViewOfKind: elementKind)
  184. }
  185.  
  186. /**
  187. Register a class for a decoration kind.
  188.  
  189. - parameter aClass: Class object.
  190. - parameter elementKind: Decoration identifier.
  191. */
  192. func registerClass(aClass: AnyClass, forDecorationViewOfKind elementKind: String) {
  193. self.decorations[elementKind] = .Class(aClass)
  194. self.collectionView?.collectionViewLayout.registerClass(aClass, forDecorationViewOfKind: elementKind)
  195. }
  196.  
  197. }
  198.  
  199. // MARK: - Reloading
  200. extension CollectionViewBinder {
  201. /**
  202. Reload the binder.
  203. */
  204. func reloadData() {
  205. dispatch_async(dispatch_get_main_queue(), { [weak self] in
  206. self?.collectionView?.reloadData()
  207. })
  208. }
  209.  
  210. /**
  211. Reload the specified sections.
  212.  
  213. - parameter indexes: Index set of sections to reload.
  214. */
  215. func reloadSections(indexes: NSIndexSet) {
  216. dispatch_async(dispatch_get_main_queue(), { [weak self] in
  217. self?.collectionView?.reloadSections(indexes)
  218. })
  219. }
  220.  
  221. /**
  222. Reload all the items at the specified index paths.
  223.  
  224. - parameter indexPaths: List of index paths to reload.
  225. */
  226. func reloadItemsAtIndexPaths(indexPaths: [NSIndexPath]) {
  227. dispatch_async(dispatch_get_main_queue(), { [weak self] in
  228. self?.collectionView?.reloadItemsAtIndexPaths(indexPaths)
  229. })
  230. }
  231.  
  232. /**
  233. Reload the input views.
  234. */
  235. func reloadInputViews() {
  236. dispatch_async(dispatch_get_main_queue(), { [weak self] in
  237. self?.collectionView?.reloadInputViews()
  238. })
  239. }
  240. }
Add Comment
Please, Sign In to add comment