Enable switching between navigation modes

This commit is contained in:
Shadowfacts 2023-09-06 13:19:06 -04:00
parent f1a6a405c2
commit 75caf2c1eb
2 changed files with 56 additions and 5 deletions

View File

@ -7,6 +7,7 @@
// //
import UIKit import UIKit
import Combine
class MainSplitViewController: UISplitViewController { class MainSplitViewController: UISplitViewController {
@ -20,10 +21,13 @@ class MainSplitViewController: UISplitViewController {
private var tabBarViewController: MainTabBarViewController! private var tabBarViewController: MainTabBarViewController!
private var navigationMode: Preferences.WidescreenNavigationMode!
private var secondaryNavController: NavigationControllerProtocol! { private var secondaryNavController: NavigationControllerProtocol! {
viewController(for: .secondary) as? NavigationControllerProtocol viewController(for: .secondary) as? NavigationControllerProtocol
} }
private var cancellables = Set<AnyCancellable>()
private var sidebarVisibile: Bool { private var sidebarVisibile: Bool {
get { get {
(UserDefaults.standard.object(forKey: "MainSplitViewControllerSidebarVisible") as? Bool) ?? true (UserDefaults.standard.object(forKey: "MainSplitViewControllerSidebarVisible") as? Bool) ?? true
@ -60,11 +64,18 @@ class MainSplitViewController: UISplitViewController {
hide(.primary) hide(.primary)
} }
let multiColumnNav = MultiColumnNavigationController() let nav: UIViewController
setViewController(multiColumnNav, for: .secondary) navigationMode = Preferences.shared.widescreenNavigationMode
switch navigationMode! {
case .stack:
nav = EnhancedNavigationViewController()
case .splitScreen:
nav = SplitNavigationController()
case .multiColumn:
nav = MultiColumnNavigationController()
}
setViewController(nav, for: .secondary)
// let splitNav = SplitNavigationController()
// setViewController(splitNav, for: .secondary)
// don't unnecesarily construct a content VC unless the we're in actually split mode // 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 // when we change from compact -> split for the first time, the VC will be transferred anyways
if traitCollection.horizontalSizeClass != .compact { if traitCollection.horizontalSizeClass != .compact {
@ -94,6 +105,37 @@ class MainSplitViewController: UISplitViewController {
addKeyCommand(MenuController.composeCommand) addKeyCommand(MenuController.composeCommand)
MenuController.sidebarItemKeyCommands.forEach(addKeyCommand(_:)) 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) { func select(item: MainSidebarViewController.Item) {

View File

@ -290,6 +290,9 @@ private class SplitSecondaryNavigationController: EnhancedNavigationViewControll
override var viewControllers: [UIViewController] { override var viewControllers: [UIViewController] {
didSet { didSet {
for vc in oldValue where vc.parent !== self {
removeSecondarySplitCloseButton(for: vc)
}
if let first = viewControllers.first { if let first = viewControllers.first {
configureSecondarySplitCloseButton(for: first) configureSecondarySplitCloseButton(for: first)
} }
@ -324,6 +327,12 @@ private class SplitSecondaryNavigationController: EnhancedNavigationViewControll
viewController.navigationItem.leftBarButtonItem = item 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() { @objc private func closeSecondary() {
closeSecondaryImpl() closeSecondaryImpl()
} }