From c1bfc6d3d9a9aaea470c4eee63dab97148f93f01 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 22 Oct 2018 20:37:12 -0400 Subject: [PATCH] Fix presenting nested modal view controllers through AppRouter --- Tusker/AppRouter.swift | 45 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/Tusker/AppRouter.swift b/Tusker/AppRouter.swift index 4cd77cf0..8d9d9cf2 100644 --- a/Tusker/AppRouter.swift +++ b/Tusker/AppRouter.swift @@ -13,43 +13,44 @@ import SafariServices class AppRouter { let rootViewController: UIViewController - var currentViewController: UIViewController { - return getContentViewController(from: rootViewController) - } init(root: UIViewController) { self.rootViewController = root } - private func getContentViewController(from vc: UIViewController) -> UIViewController { - if let vc = vc as? UITabBarController, - let selected = vc.selectedViewController { - return getContentViewController(from: selected) - } else if let vc = vc as? UINavigationController, + private func getTopViewController(parent vc: UIViewController) -> UIViewController { + if let vc = vc as? UINavigationController, 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 { return vc } } - func present(_ vc: UIViewController, animated: Bool) { - if currentViewController.presentingViewController != nil { - currentViewController.dismiss(animated: animated) { - self.present(vc, animated: animated) - } + private func getNavController(parent vc: UIViewController) -> UINavigationController { + if let vc = vc as? UINavigationController { + return vc + } 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) { - if currentViewController.presentingViewController != nil { - currentViewController.dismiss(animated: animated) { - 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) + let nav = getNavController(parent: rootViewController) + nav.pushViewController(vc, animated: true) } func profile(for accountID: String) -> ProfileTableViewController {