Created
December 27, 2016 08:46
-
-
Save hungdv136/2565248ed928e088101b9c0541371236 to your computer and use it in GitHub Desktop.
RxDelegateProxy for UIImagePickerController
This file contains 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
// | |
// RxImagePickerControllerDelegateProxy.swift | |
// RxCocoa | |
// | |
// Created by Hung Dinh Van on 12/27/2016. | |
// Copyright © 2016 ChuCuoi. All rights reserved. | |
// | |
import Foundation | |
import RxSwift | |
import RxCocoa | |
import UIKit | |
/// For more information take a look at `DelegateProxyType`. | |
class RxImagePickerControllerDelegateProxy | |
: DelegateProxy | |
, UIImagePickerControllerDelegate | |
, UINavigationControllerDelegate | |
, DelegateProxyType { | |
fileprivate var _didFinishPickingSubject: PublishSubject<[String : Any]>? | |
fileprivate var _didCancelPickingSubject: PublishSubject<Void>? | |
weak fileprivate(set) var imagePickerController: UIImagePickerController? | |
var didFinishPickingSubject: Observable<[String : Any]> { | |
if _didFinishPickingSubject == nil { | |
_didFinishPickingSubject = PublishSubject<[String : Any]>() | |
} | |
return _didFinishPickingSubject! | |
} | |
var didCancelPickingSubject: Observable<Void> { | |
if _didCancelPickingSubject == nil { | |
_didCancelPickingSubject = PublishSubject<Void>() | |
} | |
return _didCancelPickingSubject! | |
} | |
/// Initializes `RxImageViewControllerDelegateProxy` | |
/// - parameter parentObject: Parent object for delegate proxy. | |
required init(parentObject: AnyObject) { | |
self.imagePickerController = (parentObject as! UIImagePickerController) | |
super.init(parentObject: parentObject) | |
} | |
// MARK: delegate methods | |
/// For more information take a look at `DelegateProxyType`. | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
if let subject = _didFinishPickingSubject { | |
subject.on(.next(info)) | |
} | |
self._forwardToDelegate?.imagePickerController?(picker, didFinishPickingMediaWithInfo: info) | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
if let subject = _didCancelPickingSubject { | |
subject.on(.next()) | |
} | |
self._forwardToDelegate?.imagePickerControllerDidCancel?(picker) | |
} | |
// MARK: delegate proxy | |
/// For more information take a look at `DelegateProxyType`. | |
override class func createProxyForObject(_ object: AnyObject) -> AnyObject { | |
let imagePickerController = (object as! UIImagePickerController) | |
return castOrFatalError(imagePickerController.createRxDelegateProxy()) | |
} | |
/// For more information take a look at `DelegateProxyType`. | |
class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) { | |
let imagePickerController: UIImagePickerController = castOrFatalError(object) | |
imagePickerController.delegate = castOptionalOrFatalError(delegate) | |
} | |
/// For more information take a look at `DelegateProxyType`. | |
class func currentDelegateFor(_ object: AnyObject) -> AnyObject? { | |
let imagePickerController: UIImagePickerController = castOrFatalError(object) | |
return imagePickerController.delegate | |
} | |
deinit { | |
if let finishSubject = _didFinishPickingSubject { | |
finishSubject.on(.completed) | |
} | |
if let cancelSubject = _didCancelPickingSubject { | |
cancelSubject.on(.completed) | |
} | |
} | |
} | |
func castOrFatalError<T>(_ value: Any!) -> T { | |
let maybeResult: T? = value as? T | |
guard let result = maybeResult else { | |
fatalError("Failure converting from \(value) to \(T.self)") | |
} | |
return result | |
} | |
func castOptionalOrFatalError<T>(_ value: Any?) -> T? { | |
if value == nil { | |
return nil | |
} | |
let v: T = castOrFatalError(value) | |
return v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment