From ad0df8542e5e85870fb7abadc3cd63c02c8bdcb5 Mon Sep 17 00:00:00 2001 From: CMK Date: Wed, 29 Sep 2021 17:37:32 +0800 Subject: [PATCH] feat: add compact to regular split view controller expand restore logic --- Mastodon/Coordinator/SceneCoordinator.swift | 6 ++++ .../Scene/Root/RootSplitViewController.swift | 35 +++++++++++-------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Mastodon/Coordinator/SceneCoordinator.swift b/Mastodon/Coordinator/SceneCoordinator.swift index 216394c50..83ae61a84 100644 --- a/Mastodon/Coordinator/SceneCoordinator.swift +++ b/Mastodon/Coordinator/SceneCoordinator.swift @@ -20,6 +20,8 @@ final public class SceneCoordinator { weak var splitViewController: RootSplitViewController? + private(set) var secondaryStackHashValues = Set() + init(scene: UIScene, sceneDelegate: SceneDelegate, appContext: AppContext) { self.scene = scene self.sceneDelegate = sceneDelegate @@ -183,9 +185,13 @@ extension SceneCoordinator { { fallthrough } else { + if secondaryStackHashValues.contains(presentingViewController.hashValue) { + secondaryStackHashValues.insert(viewController.hashValue) + } presentingViewController.show(viewController, sender: sender) } case .showDetail: + secondaryStackHashValues.insert(viewController.hashValue) let navigationController = AdaptiveStatusBarStyleNavigationController(rootViewController: viewController) presentingViewController.showDetailViewController(navigationController, sender: sender) diff --git a/Mastodon/Scene/Root/RootSplitViewController.swift b/Mastodon/Scene/Root/RootSplitViewController.swift index bba68defb..e7a632cb8 100644 --- a/Mastodon/Scene/Root/RootSplitViewController.swift +++ b/Mastodon/Scene/Root/RootSplitViewController.swift @@ -132,7 +132,7 @@ extension RootSplitViewController: SidebarViewControllerDelegate { func sidebarViewController(_ sidebarViewController: SidebarViewController, didSelectSearchHistory searchHistoryViewModel: SidebarViewModel.SearchHistoryViewModel) { // self.sidebarViewController(sidebarViewController, didSelectTab: .search) - let supplementaryViewController = viewController(for: .supplementary) + let supplementaryViewController = viewController(for: .supplementary) let managedObjectContext = context.managedObjectContext managedObjectContext.perform { let searchHistory = managedObjectContext.object(with: searchHistoryViewModel.searchHistoryObjectID) as! SearchHistory @@ -200,21 +200,26 @@ extension RootSplitViewController: UISplitViewControllerDelegate { _ svc: UISplitViewController, displayModeForExpandingToProposedDisplayMode proposedDisplayMode: UISplitViewController.DisplayMode ) -> UISplitViewController.DisplayMode { + let compactNavigationController = mainTabBarController.selectedViewController as? UINavigationController + let viewControllers = compactNavigationController?.popToRootViewController(animated: true) ?? [] + var supplementaryViewControllers: [UIViewController] = [] + var secondaryViewControllers: [UIViewController] = [] + for viewController in viewControllers { + if coordinator.secondaryStackHashValues.contains(viewController.hashValue) { + secondaryViewControllers.append(viewController) + } else { + supplementaryViewControllers.append(viewController) + } + + } + if let supplementary = viewController(for: .supplementary) as? UINavigationController { + supplementary.setViewControllers(supplementary.viewControllers + supplementaryViewControllers, animated: false) + } + if let secondaryNavigationController = viewController(for: .secondary) as? UINavigationController { + secondaryNavigationController.setViewControllers(secondaryNavigationController.viewControllers + secondaryViewControllers, animated: false) + } return proposedDisplayMode } - - func splitViewController( - _ splitViewController: UISplitViewController, - show vc: UIViewController, - sender: Any? - ) -> Bool { - if !splitViewController.isCollapsed { - // display in .secondary when expand - splitViewController.showDetailViewController(vc, sender: sender) - return true - } else { - return false - } - } + }