Created
January 16, 2018 10:39
-
-
Save phillippbertram/8a4551c9b1750a79ac432a2af1fbb16d to your computer and use it in GitHub Desktop.
RxSwift DelegateProxy for AVCaptureMetadataOutputObjectsDelegate
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 AVFoundation | |
import RxSwift | |
import RxCocoa | |
extension Reactive where Base: AVCaptureMetadataOutput { | |
/// Reactive wrapper for `delegate`. | |
/// | |
/// For more information take a look at `DelegateProxyType` protocol documentation. | |
public var delegate: DelegateProxy<AVCaptureMetadataOutput, AVCaptureMetadataOutputObjectsDelegate> { | |
return RxCaptureMetadataOutputObjectsDelegateProxy.proxy(for: base) | |
} | |
public var metadata: Observable<[AVMetadataObject]> { | |
return delegate | |
.methodInvoked(#selector(AVCaptureMetadataOutputObjectsDelegate.metadataOutput(_:didOutput:from:))) | |
.map { params in | |
params[1] as! [AVMetadataObject] // swiftlint:disable:this force_cast | |
} | |
} | |
} | |
extension AVCaptureMetadataOutput: HasDelegate { | |
public typealias Delegate = AVCaptureMetadataOutputObjectsDelegate | |
// MARK: HasDelegate | |
public var delegate: AVCaptureMetadataOutputObjectsDelegate? { | |
get { | |
return metadataObjectsDelegate | |
} | |
set(newValue) { | |
setMetadataObjectsDelegate(newValue, queue: DispatchQueue.main) | |
} | |
} | |
} | |
open class RxCaptureMetadataOutputObjectsDelegateProxy: | |
DelegateProxy < AVCaptureMetadataOutput, | |
AVCaptureMetadataOutputObjectsDelegate>, | |
DelegateProxyType, | |
AVCaptureMetadataOutputObjectsDelegate { | |
/// Typed parent object. | |
public weak private(set) var output: AVCaptureMetadataOutput? | |
/// - parameter tabBar: Parent object for delegate proxy. | |
public init(metadataOutput: ParentObject) { | |
self.output = metadataOutput | |
super.init(parentObject: metadataOutput, delegateProxy: RxCaptureMetadataOutputObjectsDelegateProxy.self) | |
} | |
// Register known implementations | |
public static func registerKnownImplementations() { | |
self.register { | |
RxCaptureMetadataOutputObjectsDelegateProxy(metadataOutput: $0) | |
} | |
} | |
/// For more information take a look at `DelegateProxyType`. | |
open class func currentDelegate(for object: ParentObject) -> AVCaptureMetadataOutputObjectsDelegate? { | |
return object.delegate | |
} | |
/// For more information take a look at `DelegateProxyType`. | |
open class func setCurrentDelegate(_ delegate: AVCaptureMetadataOutputObjectsDelegate?, to object: ParentObject) { | |
object.delegate = delegate | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment