diff --git a/Mastodon/Scene/Root/ContentSplitViewController.swift b/Mastodon/Scene/Root/ContentSplitViewController.swift index 577f8d6a4..850b1429f 100644 --- a/Mastodon/Scene/Root/ContentSplitViewController.swift +++ b/Mastodon/Scene/Root/ContentSplitViewController.swift @@ -10,6 +10,10 @@ import UIKit import Combine import CoreDataStack +protocol ContentSplitViewControllerDelegate: AnyObject { + func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab) +} + final class ContentSplitViewController: UIViewController, NeedsDependency { var disposeBag = Set() @@ -19,6 +23,8 @@ final class ContentSplitViewController: UIViewController, NeedsDependency { weak var context: AppContext! { willSet { precondition(!isViewLoaded) } } weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } } + weak var delegate: ContentSplitViewControllerDelegate? + private(set) lazy var sidebarViewController: SidebarViewController = { let sidebarViewController = SidebarViewController() sidebarViewController.context = context @@ -87,18 +93,7 @@ extension ContentSplitViewController { extension ContentSplitViewController: SidebarViewControllerDelegate { func sidebarViewController(_ sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab) { - guard let _ = MainTabBarController.Tab.allCases.firstIndex(of: tab) else { - assertionFailure() - return - } - let previousTab = currentSupplementaryTab - currentSupplementaryTab = tab - - if previousTab == tab, - let navigationController = mainTabBarController.selectedViewController as? UINavigationController - { - navigationController.popToRootViewController(animated: true) - } + delegate?.contentSplitViewController(self, sidebarViewController: sidebarViewController, didSelectTab: tab) } func sidebarViewController(_ sidebarViewController: SidebarViewController, didLongPressItem item: SidebarViewModel.Item, sourceView: UIView) { diff --git a/Mastodon/Scene/Root/RootSplitViewController.swift b/Mastodon/Scene/Root/RootSplitViewController.swift index 22354751d..7c03287f1 100644 --- a/Mastodon/Scene/Root/RootSplitViewController.swift +++ b/Mastodon/Scene/Root/RootSplitViewController.swift @@ -23,6 +23,7 @@ final class RootSplitViewController: UISplitViewController, NeedsDependency { let contentSplitViewController = ContentSplitViewController() contentSplitViewController.context = context contentSplitViewController.coordinator = coordinator + contentSplitViewController.delegate = self return contentSplitViewController }() @@ -131,6 +132,36 @@ extension RootSplitViewController { } +// MARK: - ContentSplitViewControllerDelegate +extension RootSplitViewController: ContentSplitViewControllerDelegate { + func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab) { + guard let _ = MainTabBarController.Tab.allCases.firstIndex(of: tab) else { + assertionFailure() + return + } + switch tab { + case .search: + guard let navigationController = searchViewController.navigationController else { return } + if navigationController.viewControllers.count == 1 { + searchViewController.searchBarTapPublisher.send() + } else { + navigationController.popToRootViewController(animated: true) + } + + default: + let previousTab = contentSplitViewController.currentSupplementaryTab + contentSplitViewController.currentSupplementaryTab = tab + + if previousTab == tab, + let navigationController = contentSplitViewController.mainTabBarController.selectedViewController as? UINavigationController + { + navigationController.popToRootViewController(animated: true) + } + + } + } +} + // MARK: - UISplitViewControllerDelegate extension RootSplitViewController: UISplitViewControllerDelegate { @@ -180,7 +211,13 @@ extension RootSplitViewController: UISplitViewControllerDelegate { } RootSplitViewController.transform(from: compactMainTabBarViewController, to: contentSplitViewController.mainTabBarController) - contentSplitViewController.currentSupplementaryTab = compactMainTabBarViewController.currentTab.value + + let tab = compactMainTabBarViewController.currentTab.value + if tab == .search { + contentSplitViewController.currentSupplementaryTab = .home + } else { + contentSplitViewController.currentSupplementaryTab = compactMainTabBarViewController.currentTab.value + } return proposedDisplayMode }