Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres
Created March 13, 2019 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RuiAAPeres/2b8d195b8e2b43d374686c77722492dd to your computer and use it in GitHub Desktop.
Save RuiAAPeres/2b8d195b8e2b43d374686c77722492dd to your computer and use it in GitHub Desktop.
public extension UIViewController {
var modalFlow: Flow {
return ModalFlow(self)
}
var navigationFlow: Flow {
guard let navigationController = self.navigationController else { return modalFlow }
return NavigationFlow(navigationController)
}
}
public protocol Flow {
func present(_ viewController: UIViewController, animated: Bool)
func dismiss(_ animated: Bool)
}
private struct ModalFlow: Flow {
private let origin: UIViewController
init(_ viewController: UIViewController) {
self.origin = viewController
}
func present(_ viewController: UIViewController, animated: Bool) {
origin.present(viewController, animated: animated, completion: nil)
}
func dismiss(_ animated: Bool) {
origin.dismiss(animated: animated, completion: nil)
}
}
private struct NavigationFlow: Flow {
private let origin: UINavigationController
init(_ viewController: UINavigationController) {
self.origin = viewController
}
func present(_ viewController: UIViewController, animated: Bool) {
origin.pushViewController(viewController, animated: animated)
}
func dismiss(_ animated: Bool) {
origin.popViewController(animated: animated)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment