From 2f31b50a5b966e09f55d0606adb81d0daf609dec Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 8 Jun 2024 11:21:05 -0700 Subject: [PATCH] Fix search results always pushing new column in multi-column nav Closes #498 --- Tusker/Screens/Explore/ExploreViewController.swift | 2 +- .../Screens/Explore/InlineTrendsViewController.swift | 2 +- Tusker/Screens/Search/MastodonSearchController.swift | 12 +++++++++++- .../Utilities/MultiColumnNavigationController.swift | 9 +++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Tusker/Screens/Explore/ExploreViewController.swift b/Tusker/Screens/Explore/ExploreViewController.swift index f15ff81825..75c58f655b 100644 --- a/Tusker/Screens/Explore/ExploreViewController.swift +++ b/Tusker/Screens/Explore/ExploreViewController.swift @@ -59,7 +59,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect resultsController = SearchResultsViewController(mastodonController: mastodonController) resultsController.exploreNavigationController = self.navigationController! - searchController = MastodonSearchController(searchResultsController: resultsController) + searchController = MastodonSearchController(searchResultsController: resultsController, owner: self) definesPresentationContext = true navigationItem.searchController = searchController diff --git a/Tusker/Screens/Explore/InlineTrendsViewController.swift b/Tusker/Screens/Explore/InlineTrendsViewController.swift index 9eefaaab55..74ab5e36b9 100644 --- a/Tusker/Screens/Explore/InlineTrendsViewController.swift +++ b/Tusker/Screens/Explore/InlineTrendsViewController.swift @@ -34,7 +34,7 @@ class InlineTrendsViewController: UIViewController { resultsController = SearchResultsViewController(mastodonController: mastodonController) resultsController.exploreNavigationController = self.navigationController - searchController = MastodonSearchController(searchResultsController: resultsController) + searchController = MastodonSearchController(searchResultsController: resultsController, owner: self) searchController.obscuresBackgroundDuringPresentation = true searchController.hidesNavigationBarDuringPresentation = false definesPresentationContext = true diff --git a/Tusker/Screens/Search/MastodonSearchController.swift b/Tusker/Screens/Search/MastodonSearchController.swift index b35eb03427..bc62764553 100644 --- a/Tusker/Screens/Search/MastodonSearchController.swift +++ b/Tusker/Screens/Search/MastodonSearchController.swift @@ -24,7 +24,11 @@ class MastodonSearchController: UISearchController { super.searchResultsController as! SearchResultsViewController } - init(searchResultsController: SearchResultsViewController) { + private weak var owner: UIViewController? + + init(searchResultsController: SearchResultsViewController, owner: UIViewController) { + self.owner = owner + super.init(searchResultsController: searchResultsController) searchResultsController.tokenHandler = { [unowned self] token, op in @@ -152,6 +156,12 @@ extension MastodonSearchController: UISearchBarDelegate { } } +extension MastodonSearchController: MultiColumnNavigationCustomTargetProviding { + var multiColumnNavigationTargetViewController: UIViewController? { + owner + } +} + extension UISearchBar { var searchQueryWithOperators: String { var parts = searchTextField.tokens.compactMap { $0.representedObject as? String } diff --git a/Tusker/Screens/Utilities/MultiColumnNavigationController.swift b/Tusker/Screens/Utilities/MultiColumnNavigationController.swift index 96576a614a..b11bdd17e3 100644 --- a/Tusker/Screens/Utilities/MultiColumnNavigationController.swift +++ b/Tusker/Screens/Utilities/MultiColumnNavigationController.swift @@ -8,6 +8,13 @@ import UIKit +/// View controllers, such as `UISearchController`, that live outside the normal VC hierarchy +/// can adopt this protocol to indicate to `MultiColumnNavigationController` the context for +/// navigation operations. +protocol MultiColumnNavigationCustomTargetProviding { + var multiColumnNavigationTargetViewController: UIViewController? { get } +} + class MultiColumnNavigationController: UIViewController { private var isManuallyUpdating = false @@ -99,6 +106,8 @@ class MultiColumnNavigationController: UIViewController { index = _viewControllers.firstIndex(of: c) if index != nil { break + } else if let targetProviding = c as? MultiColumnNavigationCustomTargetProviding { + current = targetProviding.multiColumnNavigationTargetViewController } else { current = c.parent }