Created
March 13, 2019 19:10
-
-
Save RuiAAPeres/2b8d195b8e2b43d374686c77722492dd to your computer and use it in GitHub Desktop.
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
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