Last active
June 10, 2020 02:24
-
-
Save markcadag/99c2a1b8a037b8e39738980a8c46348e to your computer and use it in GitHub Desktop.
Migrate MXParallaxHeader Delegates to RxSwift observables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Reactive where Base: MXParallaxHeader { | |
public var delegate: DelegateProxy<MXParallaxHeader, MXParallaxHeaderDelegate> { | |
return MXParallaxHeaderDelegateProxy.proxy(for: base) | |
} | |
var parallaxHeaderDidScroll: ControlEvent<MXParallaxHeader> { | |
let source: Observable<MXParallaxHeader> = self.delegate.methodInvoked(#selector(MXParallaxHeaderDelegate.parallaxHeaderDidScroll(_:))).map { (parameters) in | |
(parameters[0] as? MXParallaxHeader ?? MXParallaxHeader()) | |
} | |
return ControlEvent.init(events: source) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scrollView.parallaxHeader.rx | |
.parallaxHeaderDidScroll | |
.subscribe(onNext: { i in | |
print("progress \(i.progress)") | |
}) | |
.disposed(by: disposeBag) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MXParallaxHeaderDelegateProxy: DelegateProxy<MXParallaxHeader, MXParallaxHeaderDelegate>, DelegateProxyType, MXParallaxHeaderDelegate { | |
public weak private(set) var mxParallaxHeader: MXParallaxHeader? | |
public init(mxParallaxHeader: ParentObject) { | |
self.mxParallaxHeader = mxParallaxHeader | |
super.init(parentObject: mxParallaxHeader, delegateProxy: MXParallaxHeaderDelegateProxy.self) | |
} | |
static func registerKnownImplementations() { | |
self.register { MXParallaxHeaderDelegateProxy(mxParallaxHeader: $0) } | |
} | |
static func currentDelegate(for object: MXParallaxHeader) -> MXParallaxHeaderDelegate? { | |
return object.delegate | |
} | |
static func setCurrentDelegate(_ delegate: MXParallaxHeaderDelegate?, to object: MXParallaxHeader) { | |
object.delegate = delegate | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment