Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @objc public protocol MyCollectionViewProtocol {
- func scrollViewShouldScrollToTop()
- }
- public class MyCollectionView: UICollectionView {
- weak var cvDelegate : MyCollectionViewProtocol?
- ... //rest of the code isnt related to this question in particular
- open class RxMyCollectionViewDelegateProxy : DelegateProxy<MyCollectionView, MyCollectionViewProtocol>
- , DelegateProxyType
- , MyCollectionViewProtocol {
- public static func currentDelegate(for object: MyCollectionView) -> MyCollectionViewProtocol? {
- return object.cvDelegate
- }
- public static func setCurrentDelegate(_ delegate: MyCollectionViewProtocol?, to object: MyCollectionView) {
- object.cvDelegate = delegate
- }
- public weak private(set) var collectionView: MyCollectionView?
- internal lazy var shouldScrollPublishSubject: PublishSubject<Void> = {
- let localSubject = PublishSubject<Void>()
- return localSubject
- }()
- public init(collectionView: ParentObject) {
- self.collectionView = collectionView
- super.init(parentObject: collectionView, delegateProxy: RxMyCollectionViewDelegateProxy.self)
- }
- // Register known implementations
- public static func registerKnownImplementations() {
- self.register { RxMyCollectionViewDelegateProxy(collectionView: $0) }
- }
- //implementation of MyCollectionViewProtocol
- public func scrollViewShouldScrollToTop() {
- shouldScrollPublishSubject.onNext(())
- self._forwardToDelegate?.scrollViewShouldScrollToTop()
- }
- deinit {
- shouldScrollPublishSubject.onCompleted()
- }
- }
- extension Reactive where Base: MyCollectionView {
- public var delegate: DelegateProxy<MyCollectionView, MyCollectionViewProtocol> {
- return RxMyCollectionViewDelegateProxy.proxy(for: base)
- }
- public var shouldScrollToTop: ControlEvent<Void> {
- let source = RxMyCollectionViewDelegateProxy.proxy(for: base).shouldScrollPublishSubject
- return ControlEvent(events: source)
- }
- }
- collectionView.rx.shouldScrollToTop.debug().subscribe(onNext: { (state) in
- print("I should scroll to top")
- }, onError: { (error) in
- print("errored out")
- }, onCompleted: {
- print("completed")
- }, onDisposed: {
- print("Disposed")
- }).disposed(by: disposeBag)
Add Comment
Please, Sign In to add comment