Guest User

Untitled

a guest
Jan 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. @objc public protocol MyCollectionViewProtocol {
  2. func scrollViewShouldScrollToTop()
  3. }
  4.  
  5. public class MyCollectionView: UICollectionView {
  6. weak var cvDelegate : MyCollectionViewProtocol?
  7. ... //rest of the code isnt related to this question in particular
  8.  
  9. open class RxMyCollectionViewDelegateProxy : DelegateProxy<MyCollectionView, MyCollectionViewProtocol>
  10. , DelegateProxyType
  11. , MyCollectionViewProtocol {
  12.  
  13. public static func currentDelegate(for object: MyCollectionView) -> MyCollectionViewProtocol? {
  14. return object.cvDelegate
  15. }
  16.  
  17. public static func setCurrentDelegate(_ delegate: MyCollectionViewProtocol?, to object: MyCollectionView) {
  18. object.cvDelegate = delegate
  19. }
  20.  
  21.  
  22. public weak private(set) var collectionView: MyCollectionView?
  23.  
  24.  
  25. internal lazy var shouldScrollPublishSubject: PublishSubject<Void> = {
  26. let localSubject = PublishSubject<Void>()
  27. return localSubject
  28. }()
  29.  
  30. public init(collectionView: ParentObject) {
  31. self.collectionView = collectionView
  32. super.init(parentObject: collectionView, delegateProxy: RxMyCollectionViewDelegateProxy.self)
  33. }
  34.  
  35. // Register known implementations
  36. public static func registerKnownImplementations() {
  37. self.register { RxMyCollectionViewDelegateProxy(collectionView: $0) }
  38. }
  39.  
  40.  
  41. //implementation of MyCollectionViewProtocol
  42. public func scrollViewShouldScrollToTop() {
  43. shouldScrollPublishSubject.onNext(())
  44. self._forwardToDelegate?.scrollViewShouldScrollToTop()
  45. }
  46.  
  47. deinit {
  48. shouldScrollPublishSubject.onCompleted()
  49. }
  50. }
  51.  
  52. extension Reactive where Base: MyCollectionView {
  53. public var delegate: DelegateProxy<MyCollectionView, MyCollectionViewProtocol> {
  54. return RxMyCollectionViewDelegateProxy.proxy(for: base)
  55. }
  56.  
  57. public var shouldScrollToTop: ControlEvent<Void> {
  58. let source = RxMyCollectionViewDelegateProxy.proxy(for: base).shouldScrollPublishSubject
  59. return ControlEvent(events: source)
  60. }
  61. }
  62.  
  63. collectionView.rx.shouldScrollToTop.debug().subscribe(onNext: { (state) in
  64. print("I should scroll to top")
  65. }, onError: { (error) in
  66. print("errored out")
  67. }, onCompleted: {
  68. print("completed")
  69. }, onDisposed: {
  70. print("Disposed")
  71. }).disposed(by: disposeBag)
Add Comment
Please, Sign In to add comment