Support tapping selected sidebar item to scroll to top

This commit is contained in:
Shadowfacts 2024-04-01 10:35:54 -04:00
parent d6faf3a37b
commit f5704e561b
3 changed files with 14 additions and 1 deletions

View File

@ -15,6 +15,7 @@ protocol MainSidebarViewControllerDelegate: AnyObject {
func sidebarRequestPresentCompose(_ sidebarViewController: MainSidebarViewController) func sidebarRequestPresentCompose(_ sidebarViewController: MainSidebarViewController)
func sidebar(_ sidebarViewController: MainSidebarViewController, didSelectItem item: MainSidebarViewController.Item) func sidebar(_ sidebarViewController: MainSidebarViewController, didSelectItem item: MainSidebarViewController.Item)
func sidebar(_ sidebarViewController: MainSidebarViewController, showViewController viewController: UIViewController) func sidebar(_ sidebarViewController: MainSidebarViewController, showViewController viewController: UIViewController)
func sidebar(_ sidebarViewController: MainSidebarViewController, scrollToTopFor item: MainSidebarViewController.Item)
} }
class MainSidebarViewController: UIViewController { class MainSidebarViewController: UIViewController {
@ -451,7 +452,9 @@ extension MainSidebarViewController: UICollectionViewDelegate {
return return
} }
itemLastSelectedTimestamps[item] = Date() itemLastSelectedTimestamps[item] = Date()
if [MainSidebarViewController.Item.tab(.compose), .addList, .addSavedHashtag, .addSavedInstance].contains(item) { if previouslySelectedItem == item {
sidebarDelegate?.sidebar(self, scrollToTopFor: item)
} else if [MainSidebarViewController.Item.tab(.compose), .addList, .addSavedHashtag, .addSavedInstance].contains(item) {
if let previous = previouslySelectedItem, let indexPath = dataSource.indexPath(for: previous) { if let previous = previouslySelectedItem, let indexPath = dataSource.indexPath(for: previous) {
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically) collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically)
} }

View File

@ -478,6 +478,10 @@ extension MainSplitViewController: MainSidebarViewControllerDelegate {
} }
secondaryNavController.viewControllers = [viewController] secondaryNavController.viewControllers = [viewController]
} }
func sidebar(_ sidebarViewController: MainSidebarViewController, scrollToTopFor item: MainSidebarViewController.Item) {
(secondaryNavController as? TabBarScrollableViewController)?.tabBarScrollToTop()
}
} }
fileprivate extension MainSidebarViewController.Item { fileprivate extension MainSidebarViewController.Item {

View File

@ -258,6 +258,12 @@ class SplitNavigationController: UIViewController {
} }
} }
extension SplitNavigationController: TabBarScrollableViewController {
func tabBarScrollToTop() {
(viewControllers.last as? TabBarScrollableViewController)?.tabBarScrollToTop()
}
}
extension SplitNavigationController: StatusBarTappableViewController { extension SplitNavigationController: StatusBarTappableViewController {
func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult { func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult {
let vcs = viewControllers let vcs = viewControllers