Last active
September 15, 2020 18:54
-
-
Save danielt1263/ac1cbb673ce76409cafdd5d6009d6164 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
// | |
// Presenter.swift | |
// | |
// Created by Daniel Tartaglia on 8/24/20. | |
// Copyright © 2020 Daniel Tartaglia. MIT License. | |
// | |
import UIKit | |
import RxSwift | |
extension UIViewController { | |
@discardableResult | |
func present<T>(_ create: @autoclosure () -> (UIViewController, Observable<T>), animated: Bool) -> Observable<T> { | |
let (controller, action) = create() | |
present(controller, animated: animated) | |
let dismissed = PublishSubject<Void>() | |
let sharedAction = action.share(replay: 1) | |
_ = sharedAction | |
.observeOn(MainScheduler.instance) | |
.subscribe(onCompleted: { [weak self, weak controller] in | |
if self?.presentedViewController == controller { | |
self?.dismiss(animated: true, completion: { | |
dismissed.onCompleted() | |
}) | |
} | |
else { | |
dismissed.onCompleted() | |
} | |
}) | |
return Observable.combineLatest(sharedAction, dismissed.startWith(())).map { $0.0 } | |
} | |
} | |
func scene<Action, VC: UIViewController>(name: String, type: VC.Type, binder: (VC) -> Observable<Action>) -> (UIViewController, Observable<Action>) { | |
let storyboard = UIStoryboard(name: name, bundle: nil) | |
let controller = storyboard.instantiateInitialViewController() as! VC | |
controller.loadViewIfNeeded() | |
return (controller, binder(controller)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment