Fix presenting nested modal view controllers through AppRouter

This commit is contained in:
Shadowfacts 2018-10-22 20:37:12 -04:00
parent 54f142e3f9
commit bef53cda6e
1 changed files with 23 additions and 22 deletions

View File

@ -13,43 +13,44 @@ import SafariServices
class AppRouter { class AppRouter {
let rootViewController: UIViewController let rootViewController: UIViewController
var currentViewController: UIViewController {
return getContentViewController(from: rootViewController)
}
init(root: UIViewController) { init(root: UIViewController) {
self.rootViewController = root self.rootViewController = root
} }
private func getContentViewController(from vc: UIViewController) -> UIViewController { private func getTopViewController(parent vc: UIViewController) -> UIViewController {
if let vc = vc as? UITabBarController, if let vc = vc as? UINavigationController,
let selected = vc.selectedViewController {
return getContentViewController(from: selected)
} else if let vc = vc as? UINavigationController,
let top = vc.topViewController { let top = vc.topViewController {
return getContentViewController(from: top) return getTopViewController(parent: top)
} else if let vc = vc as? UITabBarController,
let selected = vc.selectedViewController {
return getTopViewController(parent: selected)
} else if let presented = vc.presentedViewController {
return getTopViewController(parent: presented)
} else { } else {
return vc return vc
} }
} }
func present(_ vc: UIViewController, animated: Bool) { private func getNavController(parent vc: UIViewController) -> UINavigationController {
if currentViewController.presentingViewController != nil { if let vc = vc as? UINavigationController {
currentViewController.dismiss(animated: animated) { return vc
self.present(vc, animated: animated) } else if let vc = vc as? UITabBarController,
} let selected = vc.selectedViewController {
return getNavController(parent: selected)
} else {
fatalError()
} }
currentViewController.present(vc, animated: animated) }
func present(_ vc: UIViewController, animated: Bool) {
let top = getTopViewController(parent: rootViewController)
top.present(vc, animated: animated)
} }
func push(_ vc: UIViewController, animated: Bool) { func push(_ vc: UIViewController, animated: Bool) {
if currentViewController.presentingViewController != nil { let nav = getNavController(parent: rootViewController)
currentViewController.dismiss(animated: animated) { nav.pushViewController(vc, animated: true)
self.push(vc, animated: animated)
}
}
guard let nav = currentViewController.navigationController else { fatalError("Can't push view controller while not in navigation controller") }
nav.pushViewController(vc, animated: animated)
} }
func profile(for accountID: String) -> ProfileTableViewController { func profile(for accountID: String) -> ProfileTableViewController {