Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// 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