Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
61 lines (52 sloc)
2.03 KB
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
// | |
// UIImagePickerController+RxCreate.swift | |
// RxExample | |
// | |
// Created by Krunoslav Zaher on 1/10/16. | |
// Copyright © 2016 Krunoslav Zaher. All rights reserved. | |
// | |
import UIKit | |
import RxSwift | |
import RxCocoa | |
func dismissViewController(_ viewController: UIViewController, animated: Bool) { | |
if viewController.isBeingDismissed || viewController.isBeingPresented { | |
DispatchQueue.main.async { | |
dismissViewController(viewController, animated: animated) | |
} | |
return | |
} | |
if viewController.presentingViewController != nil { | |
viewController.dismiss(animated: animated, completion: nil) | |
} | |
} | |
extension Reactive where Base: UIImagePickerController { | |
static func createWithParent(_ parent: UIViewController?, animated: Bool = true, configureImagePicker: @escaping (UIImagePickerController) throws -> Void = { x in }) -> Observable<UIImagePickerController> { | |
return Observable.create { [weak parent] observer in | |
let imagePicker = UIImagePickerController() | |
let dismissDisposable = imagePicker.rx | |
.didCancel | |
.subscribe(onNext: { [weak imagePicker] _ in | |
guard let imagePicker = imagePicker else { | |
return | |
} | |
dismissViewController(imagePicker, animated: animated) | |
}) | |
do { | |
try configureImagePicker(imagePicker) | |
} | |
catch let error { | |
observer.on(.error(error)) | |
return Disposables.create() | |
} | |
guard let parent = parent else { | |
observer.on(.completed) | |
return Disposables.create() | |
} | |
parent.present(imagePicker, animated: animated, completion: nil) | |
observer.on(.next(imagePicker)) | |
return Disposables.create(dismissDisposable, Disposables.create { | |
dismissViewController(imagePicker, animated: animated) | |
}) | |
} | |
} | |
} |