From d638ff513b1c096753057ff291dda082c14b84ee Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 26 Oct 2020 22:13:48 -0400 Subject: [PATCH] Fix crash when using show timeline Siri Shortcut --- .../Main/MainSplitViewController.swift | 14 +++++++ .../Main/TuskerRootViewController.swift | 1 + Tusker/Shortcuts/UserActivityManager.swift | 39 ++++++++++--------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Tusker/Screens/Main/MainSplitViewController.swift b/Tusker/Screens/Main/MainSplitViewController.swift index e30741ee..1aec0f08 100644 --- a/Tusker/Screens/Main/MainSplitViewController.swift +++ b/Tusker/Screens/Main/MainSplitViewController.swift @@ -334,4 +334,18 @@ extension MainSplitViewController: TuskerRootViewController { } } } + + func getTabController(tab: MainTabBarViewController.Tab) -> UIViewController? { + if traitCollection.horizontalSizeClass == .compact { + return tabBarViewController?.getTabController(tab: tab) + } else { + if tab == .compose { + return nil + } else if case .tab(tab) = sidebar.selectedItem { + return viewController(for: .secondary) + } else { + return nil + } + } + } } diff --git a/Tusker/Screens/Main/TuskerRootViewController.swift b/Tusker/Screens/Main/TuskerRootViewController.swift index 1eb795be..94f7b3f6 100644 --- a/Tusker/Screens/Main/TuskerRootViewController.swift +++ b/Tusker/Screens/Main/TuskerRootViewController.swift @@ -11,4 +11,5 @@ import UIKit protocol TuskerRootViewController: UIViewController { func presentCompose() func select(tab: MainTabBarViewController.Tab) + func getTabController(tab: MainTabBarViewController.Tab) -> UIViewController? } diff --git a/Tusker/Shortcuts/UserActivityManager.swift b/Tusker/Shortcuts/UserActivityManager.swift index 545f0f48..faf95ad5 100644 --- a/Tusker/Shortcuts/UserActivityManager.swift +++ b/Tusker/Shortcuts/UserActivityManager.swift @@ -21,14 +21,14 @@ class UserActivityManager { return scene.session.mastodonController! } - private static func getMainTabBarController() -> MainTabBarViewController { + private static func getMainViewController() -> TuskerRootViewController { let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first! let window = scene.windows.first { $0.isKeyWindow }! - return window.rootViewController as! MainTabBarViewController + return window.rootViewController as! TuskerRootViewController } private static func present(_ vc: UIViewController, animated: Bool = true) { - getMainTabBarController().present(vc, animated: animated) + getMainViewController().present(vc, animated: animated) } // MARK: - New Post @@ -66,9 +66,9 @@ class UserActivityManager { } static func handleCheckNotifications(activity: NSUserActivity) { - let tabBarController = getMainTabBarController() - tabBarController.select(tab: .notifications) - if let navigationController = tabBarController.getTabController(tab: .notifications) as? UINavigationController, + let mainViewController = getMainViewController() + mainViewController.select(tab: .notifications) + if let navigationController = mainViewController.getTabController(tab: .notifications) as? UINavigationController, let notificationsPageController = navigationController.viewControllers.first as? NotificationsPageViewController { navigationController.popToRootViewController(animated: false) notificationsPageController.loadViewIfNeeded() @@ -86,9 +86,9 @@ class UserActivityManager { } static func handleCheckMentions(activity: NSUserActivity) { - let tabBarController = getMainTabBarController() - tabBarController.select(tab: .notifications) - if let navController = tabBarController.getTabController(tab: .notifications) as? UINavigationController, + let mainViewController = getMainViewController() + mainViewController.select(tab: .notifications) + if let navController = mainViewController.getTabController(tab: .notifications) as? UINavigationController, let notificationsPageController = navController.viewControllers.first as? NotificationsPageViewController { navController.popToRootViewController(animated: false) notificationsPageController.loadViewIfNeeded() @@ -133,9 +133,12 @@ class UserActivityManager { return } - let tabBarController = getMainTabBarController() - tabBarController.select(tab: .timelines) - let navigationController = tabBarController.viewControllers![0] as! UINavigationController + let mainViewController = getMainViewController() + mainViewController.select(tab: .timelines) + guard let navigationController = mainViewController.getTabController(tab: .timelines) as? UINavigationController else { + return + } + switch timeline { case .home, .public(true), .public(false): navigationController.popToRootViewController(animated: false) @@ -170,9 +173,9 @@ class UserActivityManager { } static func handleSearch(activity: NSUserActivity) { - let tabBarController = getMainTabBarController() - tabBarController.select(tab: .explore) - if let navigationController = tabBarController.getTabController(tab: .explore) as? UINavigationController, + let mainViewController = getMainViewController() + mainViewController.select(tab: .explore) + if let navigationController = mainViewController.getTabController(tab: .explore) as? UINavigationController, let exploreController = navigationController.viewControllers.first as? ExploreViewController { navigationController.popToRootViewController(animated: false) exploreController.searchController.isActive = true @@ -189,9 +192,9 @@ class UserActivityManager { } static func handleBookmarks(activity: NSUserActivity) { - let tabBarController = getMainTabBarController() - tabBarController.select(tab: .explore) - if let navigationController = tabBarController.getTabController(tab: .explore) as? UINavigationController { + let mainViewController = getMainViewController() + mainViewController.select(tab: .explore) + if let navigationController = mainViewController.getTabController(tab: .explore) as? UINavigationController { navigationController.popToRootViewController(animated: false) navigationController.pushViewController(BookmarksTableViewController(mastodonController: mastodonController), animated: false) }