diff --git a/Tusker/Screens/Main/MainSplitViewController.swift b/Tusker/Screens/Main/MainSplitViewController.swift index 63a806fe..3e344ce7 100644 --- a/Tusker/Screens/Main/MainSplitViewController.swift +++ b/Tusker/Screens/Main/MainSplitViewController.swift @@ -7,6 +7,7 @@ // import UIKit +import Combine class MainSplitViewController: UISplitViewController { @@ -20,10 +21,13 @@ class MainSplitViewController: UISplitViewController { private var tabBarViewController: MainTabBarViewController! + private var navigationMode: Preferences.WidescreenNavigationMode! private var secondaryNavController: NavigationControllerProtocol! { viewController(for: .secondary) as? NavigationControllerProtocol } + private var cancellables = Set() + private var sidebarVisibile: Bool { get { (UserDefaults.standard.object(forKey: "MainSplitViewControllerSidebarVisible") as? Bool) ?? true @@ -60,11 +64,18 @@ class MainSplitViewController: UISplitViewController { hide(.primary) } - let multiColumnNav = MultiColumnNavigationController() - setViewController(multiColumnNav, for: .secondary) - -// let splitNav = SplitNavigationController() -// setViewController(splitNav, for: .secondary) + let nav: UIViewController + navigationMode = Preferences.shared.widescreenNavigationMode + switch navigationMode! { + case .stack: + nav = EnhancedNavigationViewController() + case .splitScreen: + nav = SplitNavigationController() + case .multiColumn: + nav = MultiColumnNavigationController() + } + setViewController(nav, for: .secondary) + // don't unnecesarily construct a content VC unless the we're in actually split mode // when we change from compact -> split for the first time, the VC will be transferred anyways if traitCollection.horizontalSizeClass != .compact { @@ -94,6 +105,37 @@ class MainSplitViewController: UISplitViewController { addKeyCommand(MenuController.composeCommand) MenuController.sidebarItemKeyCommands.forEach(addKeyCommand(_:)) + + Preferences.shared.$widescreenNavigationMode + .sink { [unowned self] in + self.updateNavigationMode($0) + } + .store(in: &cancellables) + } + + private func updateNavigationMode(_ mode: Preferences.WidescreenNavigationMode) { + guard mode != navigationMode else { + return + } + let viewControllers = secondaryNavController.viewControllers + secondaryNavController.viewControllers = [] + // Setting viewControllers = [] doesn't remove the VC views from their superviews immediately, + // so do that ourselves so we can re-parent the VCs to the new nav controller. + for viewController in viewControllers { + viewController.viewIfLoaded?.removeFromSuperview() + } + + let newNav: NavigationControllerProtocol + switch mode { + case .stack: + newNav = EnhancedNavigationViewController() + case .splitScreen: + newNav = SplitNavigationController() + case .multiColumn: + newNav = MultiColumnNavigationController() + } + newNav.viewControllers = viewControllers + self.setViewController(newNav, for: .secondary) } func select(item: MainSidebarViewController.Item) { diff --git a/Tusker/Screens/Utilities/SplitNavigationController.swift b/Tusker/Screens/Utilities/SplitNavigationController.swift index a0c09b91..f7ee5506 100644 --- a/Tusker/Screens/Utilities/SplitNavigationController.swift +++ b/Tusker/Screens/Utilities/SplitNavigationController.swift @@ -290,6 +290,9 @@ private class SplitSecondaryNavigationController: EnhancedNavigationViewControll override var viewControllers: [UIViewController] { didSet { + for vc in oldValue where vc.parent !== self { + removeSecondarySplitCloseButton(for: vc) + } if let first = viewControllers.first { configureSecondarySplitCloseButton(for: first) } @@ -324,6 +327,12 @@ private class SplitSecondaryNavigationController: EnhancedNavigationViewControll viewController.navigationItem.leftBarButtonItem = item } + private func removeSecondarySplitCloseButton(for viewController: UIViewController) { + if viewController.navigationItem.leftBarButtonItem?.tag == ViewTags.splitNavCloseSecondaryButton { + viewController.navigationItem.leftBarButtonItem = nil + } + } + @objc private func closeSecondary() { closeSecondaryImpl() }