From 9b2e6140a3c0f41e7f4b5fcb688bd60c1a284385 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 22 Aug 2024 11:39:39 -0400 Subject: [PATCH] Fix reselecting current sidebar item not popping to root on Catalyst and new sidebar Closes #525 --- Tusker/Screens/Main/MainSidebarViewController.swift | 4 ++-- Tusker/Screens/Main/MainSplitViewController.swift | 8 ++++++-- Tusker/Screens/Main/NewMainTabBarViewController.swift | 10 ++++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Tusker/Screens/Main/MainSidebarViewController.swift b/Tusker/Screens/Main/MainSidebarViewController.swift index 3391bbd5..38a88385 100644 --- a/Tusker/Screens/Main/MainSidebarViewController.swift +++ b/Tusker/Screens/Main/MainSidebarViewController.swift @@ -15,7 +15,7 @@ protocol MainSidebarViewControllerDelegate: AnyObject { func sidebarRequestPresentCompose(_ sidebarViewController: MainSidebarViewController) func sidebar(_ sidebarViewController: MainSidebarViewController, didSelectItem item: MainSidebarViewController.Item, previousItem: MainSidebarViewController.Item?) func sidebar(_ sidebarViewController: MainSidebarViewController, showViewController viewController: UIViewController, previousItem: MainSidebarViewController.Item?) - func sidebar(_ sidebarViewController: MainSidebarViewController, scrollToTopFor item: MainSidebarViewController.Item) + func sidebar(_ sidebarViewController: MainSidebarViewController, didReselectItem item: MainSidebarViewController.Item) } class MainSidebarViewController: UIViewController { @@ -452,7 +452,7 @@ extension MainSidebarViewController: UICollectionViewDelegate { } itemLastSelectedTimestamps[item] = Date() if previouslySelectedItem == item { - sidebarDelegate?.sidebar(self, scrollToTopFor: item) + sidebarDelegate?.sidebar(self, didReselectItem: item) } else if [MainSidebarViewController.Item.tab(.compose), .addList, .addSavedHashtag, .addSavedInstance].contains(item) { if let previous = previouslySelectedItem, let indexPath = dataSource.indexPath(for: previous) { collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically) diff --git a/Tusker/Screens/Main/MainSplitViewController.swift b/Tusker/Screens/Main/MainSplitViewController.swift index 23cf8973..23f62fe9 100644 --- a/Tusker/Screens/Main/MainSplitViewController.swift +++ b/Tusker/Screens/Main/MainSplitViewController.swift @@ -493,8 +493,12 @@ extension MainSplitViewController: MainSidebarViewControllerDelegate { secondaryNavController.viewControllers = [viewController] } - func sidebar(_ sidebarViewController: MainSidebarViewController, scrollToTopFor item: MainSidebarViewController.Item) { - (secondaryNavController as? TabBarScrollableViewController)?.tabBarScrollToTop() + func sidebar(_ sidebarViewController: MainSidebarViewController, didReselectItem item: MainSidebarViewController.Item) { + if secondaryNavController.viewControllers.count == 1 { + (secondaryNavController.topViewController as? TabBarScrollableViewController)?.tabBarScrollToTop() + } else { + secondaryNavController.popToRootViewController(animated: true) + } } } diff --git a/Tusker/Screens/Main/NewMainTabBarViewController.swift b/Tusker/Screens/Main/NewMainTabBarViewController.swift index de660811..17b19e1e 100644 --- a/Tusker/Screens/Main/NewMainTabBarViewController.swift +++ b/Tusker/Screens/Main/NewMainTabBarViewController.swift @@ -441,10 +441,12 @@ extension NewMainTabBarViewController: UITabBarControllerDelegate { return true } else if let selectedTab, selectedTab == tab, - let nav = selectedViewController as? any NavigationControllerProtocol, - nav.viewControllers.count == 1, - let scrollableVC = nav.viewControllers[0] as? TabBarScrollableViewController { - scrollableVC.tabBarScrollToTop() + let nav = selectedViewController as? any NavigationControllerProtocol { + if nav.viewControllers.count == 1 { + (nav.viewControllers[0] as? TabBarScrollableViewController)?.tabBarScrollToTop() + } else { + nav.popToRootViewController(animated: true) + } return false } else { return true