Skip to content

Instantly share code, notes, and snippets.

@morpheby
Last active February 13, 2019 14:21
Show Gist options
  • Save morpheby/1df592b04ec4b5cd8dfaa2931b351cdc to your computer and use it in GitHub Desktop.
Save morpheby/1df592b04ec4b5cd8dfaa2931b351cdc to your computer and use it in GitHub Desktop.
public protocol Router {
associatedtype ViewController: UIViewController
/// Returns view controller that is managing the view
/// presented by the router
var viewController: ViewController? { get }
}
extension Router {
func wireNextRouter<T: Router>(_ wireClosure: @escaping (T.ViewController) throws -> T, file: StaticString = #file, line: UInt = #line) -> (UIViewController) -> Void {
return { (targetViewController: UIViewController) -> Void in
guard let target = targetViewController as? T.ViewController else {
fatalError("Invalid target invoked: Should be \(T.ViewController.self), is \(type(of: targetViewController))", file: file, line: line)
}
do {
_ = try wireClosure(target)
} catch let error {
fatalError("Error while instantiating \(T.self) at \(file):\(line): \(error)", file: file, line: line)
}
}
}
func wireEmpty() -> (UIViewController) -> Void {
return { (targetViewController: UIViewController) -> Void in
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment