Created
February 27, 2019 20:37
-
-
Save etruta/2aa7dbf44f6616005a67192c8b49ec3b to your computer and use it in GitHub Desktop.
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
| import UIKit | |
| import RxSwift | |
| import RxCocoa | |
| import SJFluidSegmentedControl | |
| func castOrThrow<T>(_ resultType: T.Type, _ object: Any) throws -> T { | |
| guard let returnValue = object as? T else { | |
| throw RxCocoaError.castingError(object: object, targetType: resultType) | |
| } | |
| return returnValue | |
| } | |
| private class RxSJFluidSegmentedControlDelegateProxy: DelegateProxy<SJFluidSegmentedControl, SJFluidSegmentedControlDelegate>, | |
| SJFluidSegmentedControlDelegate, DelegateProxyType { | |
| public weak private(set) var segmentControl: SJFluidSegmentedControl? | |
| public init(segmentControl: SJFluidSegmentedControl) { | |
| self.segmentControl = segmentControl | |
| super.init(parentObject: segmentControl, delegateProxy: RxSJFluidSegmentedControlDelegateProxy.self) | |
| } | |
| static func registerKnownImplementations() { | |
| self.register { RxSJFluidSegmentedControlDelegateProxy(segmentControl: $0) } | |
| } | |
| static func currentDelegate(for object: SJFluidSegmentedControl) -> SJFluidSegmentedControlDelegate? { | |
| return object.delegate as? SJFluidSegmentedControlDelegate | |
| } | |
| static func setCurrentDelegate(_ delegate: SJFluidSegmentedControlDelegate?, to object: SJFluidSegmentedControl) { | |
| let segmentControl: SJFluidSegmentedControl = object | |
| segmentControl.delegate = delegate | |
| } | |
| } | |
| extension Reactive where Base: SJFluidSegmentedControl { | |
| public var delegate: DelegateProxy<SJFluidSegmentedControl, SJFluidSegmentedControlDelegate> { | |
| return RxSJFluidSegmentedControlDelegateProxy.proxy(for: base) | |
| } | |
| public var toSegmentAtIndex: ControlEvent<Int> { | |
| let source = delegate | |
| .methodInvoked( | |
| #selector( | |
| SJFluidSegmentedControlDelegate.segmentedControl(_:didChangeFromSegmentAtIndex:toSegmentAtIndex:) | |
| ) | |
| ) | |
| .map { segment in | |
| return try castOrThrow(Int.self, segment[2]) | |
| } | |
| return ControlEvent(events: source) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment