From 2916d7a72d0e394aad0ce5f1769aaf7f574e4b53 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 3 Jul 2020 19:36:48 -0400 Subject: [PATCH] Add tapping the active tab bar item to scroll to top Closes #106 --- Tusker.xcodeproj/project.pbxproj | 4 ++++ Tusker/Screens/Main/MainTabBarViewController.swift | 7 +++++++ .../Utilities/EnhancedTableViewController.swift | 10 ++++++++++ .../Utilities/SegmentedPageViewController.swift | 8 ++++++++ .../Utilities/TabBarScrollableViewController.swift | 13 +++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 Tusker/Screens/Utilities/TabBarScrollableViewController.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index a93af7ab..e2fa5ffb 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -118,6 +118,7 @@ D63F9C6C241C50B9004C03CF /* ComposeAttachmentTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63F9C6A241C50B9004C03CF /* ComposeAttachmentTableViewCell.xib */; }; D63F9C6E241D2D85004C03CF /* CompositionAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63F9C6D241D2D85004C03CF /* CompositionAttachment.swift */; }; D640D76922BAF5E6004FBE69 /* DomainBlocks.plist in Resources */ = {isa = PBXBuildFile; fileRef = D640D76822BAF5E6004FBE69 /* DomainBlocks.plist */; }; + D6412B0324AFF6A600F5412E /* TabBarScrollableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6412B0224AFF6A600F5412E /* TabBarScrollableViewController.swift */; }; D641C773213CAA25004B4513 /* NotificationsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D641C772213CAA25004B4513 /* NotificationsTableViewController.swift */; }; D641C77F213DC78A004B4513 /* InlineTextAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */; }; D6434EB3215B1856001A919A /* XCBRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6434EB2215B1856001A919A /* XCBRequest.swift */; }; @@ -422,6 +423,7 @@ D63F9C6A241C50B9004C03CF /* ComposeAttachmentTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ComposeAttachmentTableViewCell.xib; sourceTree = ""; }; D63F9C6D241D2D85004C03CF /* CompositionAttachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompositionAttachment.swift; sourceTree = ""; }; D640D76822BAF5E6004FBE69 /* DomainBlocks.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = DomainBlocks.plist; sourceTree = ""; }; + D6412B0224AFF6A600F5412E /* TabBarScrollableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarScrollableViewController.swift; sourceTree = ""; }; D641C772213CAA25004B4513 /* NotificationsTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsTableViewController.swift; sourceTree = ""; }; D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InlineTextAttachment.swift; sourceTree = ""; }; D6434EB2215B1856001A919A /* XCBRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBRequest.swift; sourceTree = ""; }; @@ -1238,6 +1240,7 @@ D693DE5623FE1A6A0061E07D /* EnhancedNavigationViewController.swift */, D693DE5823FE24300061E07D /* InteractivePushTransition.swift */, D6DFC69D242C490400ACC392 /* TrackpadScrollGestureRecognizer.swift */, + D6412B0224AFF6A600F5412E /* TabBarScrollableViewController.swift */, ); path = Utilities; sourceTree = ""; @@ -1701,6 +1704,7 @@ 0427033A22B31269000D31B6 /* AdvancedPrefsView.swift in Sources */, D626493C23C1000300612E6E /* AlbumTableViewCell.swift in Sources */, D60E2F292442372B005F8713 /* AccountMO.swift in Sources */, + D6412B0324AFF6A600F5412E /* TabBarScrollableViewController.swift in Sources */, D6757A822157E8FA00721E32 /* XCBSession.swift in Sources */, D6EBF01723C55E0D00AE061B /* UISceneSession+MastodonController.swift in Sources */, 04DACE8C212CB14B009840C4 /* MainTabBarViewController.swift in Sources */, diff --git a/Tusker/Screens/Main/MainTabBarViewController.swift b/Tusker/Screens/Main/MainTabBarViewController.swift index 0636d7bc..7e21e989 100644 --- a/Tusker/Screens/Main/MainTabBarViewController.swift +++ b/Tusker/Screens/Main/MainTabBarViewController.swift @@ -57,6 +57,13 @@ class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate { presentCompose() return false } + if viewController == viewControllers![selectedIndex], + let nav = viewController as? UINavigationController, + nav.viewControllers.count == 1, + let scrollableVC = nav.viewControllers.first as? TabBarScrollableViewController { + scrollableVC.tabBarScrollToTop() + return false + } return true } diff --git a/Tusker/Screens/Utilities/EnhancedTableViewController.swift b/Tusker/Screens/Utilities/EnhancedTableViewController.swift index 9aee1f54..95cf0f1b 100644 --- a/Tusker/Screens/Utilities/EnhancedTableViewController.swift +++ b/Tusker/Screens/Utilities/EnhancedTableViewController.swift @@ -95,3 +95,13 @@ extension EnhancedTableViewController { } } + +extension EnhancedTableViewController: TabBarScrollableViewController { + func tabBarScrollToTop() { + if scrollViewShouldScrollToTop(tableView) { + let topOffset = CGPoint(x: 0, y: -tableView.adjustedContentInset.top) + tableView.setContentOffset(topOffset, animated: true) + scrollViewDidScrollToTop(tableView) + } + } +} diff --git a/Tusker/Screens/Utilities/SegmentedPageViewController.swift b/Tusker/Screens/Utilities/SegmentedPageViewController.swift index 20386015..47ea6d51 100644 --- a/Tusker/Screens/Utilities/SegmentedPageViewController.swift +++ b/Tusker/Screens/Utilities/SegmentedPageViewController.swift @@ -63,3 +63,11 @@ class SegmentedPageViewController: UIPageViewController, UIPageViewControllerDel } } + +extension SegmentedPageViewController: TabBarScrollableViewController { + func tabBarScrollToTop() { + if let scrollableVC = pageControllers[currentIndex] as? TabBarScrollableViewController { + scrollableVC.tabBarScrollToTop() + } + } +} diff --git a/Tusker/Screens/Utilities/TabBarScrollableViewController.swift b/Tusker/Screens/Utilities/TabBarScrollableViewController.swift new file mode 100644 index 00000000..8c7df902 --- /dev/null +++ b/Tusker/Screens/Utilities/TabBarScrollableViewController.swift @@ -0,0 +1,13 @@ +// +// TabBarScrollableViewController.swift +// Tusker +// +// Created by Shadowfacts on 7/3/20. +// Copyright © 2020 Shadowfacts. All rights reserved. +// + +import UIKit + +protocol TabBarScrollableViewController: UIViewController { + func tabBarScrollToTop() +}