Last active
July 1, 2020 03:47
-
-
Save alfian0/80c9898a502c23d0dca077f190bacc61 to your computer and use it in GitHub Desktop.
[Pure Swift Delegate] DelegateProxy<CLLocationManager, CLLocationManagerDelegate>
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
import Foundation | |
import CoreLocation | |
import RxSwift | |
import RxCocoa | |
class CLLocationManagerDelegateProxy: DelegateProxy<CLLocationManager, CLLocationManagerDelegate>, DelegateProxyType, CLLocationManagerDelegate { | |
public weak private(set) var locationManager: CLLocationManager? | |
public init(locationManager: CLLocationManager) { | |
self.locationManager = locationManager | |
super.init(parentObject: locationManager, delegateProxy: CLLocationManagerDelegateProxy.self) | |
} | |
static func registerKnownImplementations() { | |
self.register { CLLocationManagerDelegateProxy(locationManager: $0) } | |
} | |
static func currentDelegate(for object: CLLocationManager) -> CLLocationManagerDelegate? { | |
return object.delegate | |
} | |
static func setCurrentDelegate(_ delegate: CLLocationManagerDelegate?, to object: CLLocationManager) { | |
object.delegate = delegate | |
} | |
} | |
extension Reactive where Base: CLLocationManager { | |
fileprivate var locationManagerDelegateProxy: DelegateProxy<CLLocationManager, CLLocationManagerDelegate> { | |
return CLLocationManagerDelegateProxy.proxy(for: base) | |
} | |
var didUpdateLocations: Observable<CLLocationCoordinate2D> { | |
let selector = #selector(CLLocationManagerDelegate.locationManager(_:didUpdateLocations:)) | |
return locationManagerDelegateProxy.methodInvoked(selector) | |
.map({ (params) -> CLLocationCoordinate2D in | |
return (params[1] as! [CLLocation]).first!.coordinate | |
}) | |
} | |
} |
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
import Foundation | |
import GooglePlacePicker | |
import RxSwift | |
import RxCocoa | |
class GMSPlacePickerViewControllerProxy: DelegateProxy<GMSPlacePickerViewController, GMSPlacePickerViewControllerDelegate>, DelegateProxyType, GMSPlacePickerViewControllerDelegate { | |
public weak private(set) var placePickerViewController: GMSPlacePickerViewController? | |
fileprivate let gmsPlace = PublishSubject<GMSPlace>() | |
public init(placePickerViewController: GMSPlacePickerViewController) { | |
self.placePickerViewController = placePickerViewController | |
super.init(parentObject: placePickerViewController, delegateProxy: GMSPlacePickerViewControllerProxy.self) | |
} | |
static func registerKnownImplementations() { | |
self.register { GMSPlacePickerViewControllerProxy(placePickerViewController: $0) } | |
} | |
static func currentDelegate(for object: GMSPlacePickerViewController) -> GMSPlacePickerViewControllerDelegate? { | |
return object.delegate | |
} | |
static func setCurrentDelegate(_ delegate: GMSPlacePickerViewControllerDelegate?, to object: GMSPlacePickerViewController) { | |
object.delegate = delegate | |
} | |
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) { | |
self.gmsPlace.onNext(place) | |
self._forwardToDelegate?.placePicker(viewController, didPick: place) | |
} | |
deinit { | |
self.gmsPlace.onCompleted() | |
} | |
} | |
extension Reactive where Base: GMSPlacePickerViewController { | |
fileprivate var placePickerViewControllerProxy: DelegateProxy<GMSPlacePickerViewController, GMSPlacePickerViewControllerDelegate> { | |
return GMSPlacePickerViewControllerProxy.proxy(for: base) | |
} | |
var placePicker: Observable<GMSPlace> { | |
return (placePickerViewControllerProxy as! GMSPlacePickerViewControllerProxy).gmsPlace | |
} | |
var placePickerDidCancel: Observable<Void> { | |
let selector = #selector(GMSPlacePickerViewControllerDelegate.placePickerDidCancel(_:)) | |
return placePickerViewControllerProxy.methodInvoked(selector) | |
.map({ (params) -> Void in | |
return | |
}) | |
} | |
var didFailWithError: Observable<Error> { | |
let selector = #selector(GMSPlacePickerViewControllerDelegate.placePicker(_:didFailWithError:)) | |
return placePickerViewControllerProxy.methodInvoked(selector) | |
.map({ (params) -> Error in | |
return params[1] as! Error | |
}) | |
} | |
} |
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
import UIKit | |
import RxSwift | |
import RxCocoa | |
class UIImagePickerControllerDelegateProxy: DelegateProxy<UIImagePickerController, (UIImagePickerControllerDelegate & UINavigationControllerDelegate)>, DelegateProxyType, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
public weak private(set) var imagePickerController: UIImagePickerController? | |
public init(imagePickerController: UIImagePickerController) { | |
self.imagePickerController = imagePickerController | |
super.init(parentObject: imagePickerController, delegateProxy: UIImagePickerControllerDelegateProxy.self) | |
} | |
static func registerKnownImplementations() { | |
self.register { UIImagePickerControllerDelegateProxy(imagePickerController: $0) } | |
} | |
static func currentDelegate(for object: UIImagePickerController) -> (UIImagePickerControllerDelegate & UINavigationControllerDelegate)? { | |
return object.delegate | |
} | |
static func setCurrentDelegate(_ delegate: (UIImagePickerControllerDelegate & UINavigationControllerDelegate)?, to object: UIImagePickerController) { | |
object.delegate = delegate | |
} | |
} | |
extension Reactive where Base: UIImagePickerController { | |
fileprivate var imagePickerControllerDelegateProxy: DelegateProxy<UIImagePickerController, (UIImagePickerControllerDelegate & UINavigationControllerDelegate)> { | |
return UIImagePickerControllerDelegateProxy.proxy(for: base) | |
} | |
var imagePickerControllerDidCancel: Observable<Void> { | |
let selector = #selector(UIImagePickerControllerDelegate.imagePickerControllerDidCancel(_:)) | |
return imagePickerControllerDelegateProxy.methodInvoked(selector) | |
.map({ (params) -> Void in | |
return | |
}) | |
} | |
var didFinishPickingMediaWithInfo: Observable<UIImage?> { | |
let selector = #selector(UIImagePickerControllerDelegate.imagePickerController(_:didFinishPickingMediaWithInfo:)) | |
return imagePickerControllerDelegateProxy.methodInvoked(selector) | |
.map({ (params) -> UIImage? in | |
let info = params[1] as! [String : Any] | |
return info[UIImagePickerControllerOriginalImage] as? UIImage | |
}) | |
} | |
} |
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
import UIKit | |
import CoreLocation | |
import RxSwift | |
class ViewController: UIViewController { | |
@IBOutlet weak var photoLibrary: UIButton! | |
@IBOutlet weak var image: UIImageView! | |
private lazy var imagePickerController = UIImagePickerController() | |
private lazy var locationManger = CLLocationManager() | |
private let disposeBag = DisposeBag() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
locationManger.startUpdatingLocation() | |
locationManger.requestWhenInUseAuthorization() | |
locationManger.rx.didUpdateLocations | |
.subscribe(onNext: { (location) in | |
print(location.latitude, location.longitude) | |
}) | |
.disposed(by: disposeBag) | |
imagePickerController.rx.imagePickerControllerDidCancel | |
.asDriver(onErrorJustReturn: ()) | |
.drive(onNext: { [unowned self] (_) in | |
self.dismiss(animated: true, completion: nil) | |
}) | |
.disposed(by: disposeBag) | |
imagePickerController.rx.didFinishPickingMediaWithInfo | |
.asDriver(onErrorJustReturn: nil) | |
.drive(onNext: { [unowned self] (image) in | |
self.image.image = image | |
self.dismiss(animated: true, completion: nil) | |
}) | |
.disposed(by: disposeBag) | |
photoLibrary.rx.tap | |
.asDriver() | |
.drive(onNext: { [unowned self] (_) in | |
self.present(self.imagePickerController, animated: true, completion: nil) | |
}) | |
.disposed(by: disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment