From e3d27e0720f4c3b55e3f88e41b7812f181d1570a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 10 Nov 2018 11:48:09 -0500 Subject: [PATCH] Add undo scroll-to-top --- Tusker.xcodeproj/project.pbxproj | 4 ++ .../ConversationTableViewController.swift | 2 +- .../NotificationsTableViewController.swift | 2 +- .../Profile/ProfileTableViewController.swift | 2 +- .../TimelineTableViewController.swift | 2 +- .../EnhancedTableViewController.swift | 38 +++++++++++++++++++ 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 Tusker/Screens/Utilities/EnhancedTableViewController.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 6e19d704a6..495c66f6b2 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -117,6 +117,7 @@ D6B8DB342182A59300424AF7 /* UIAlertController+Visibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6B8DB332182A59300424AF7 /* UIAlertController+Visibility.swift */; }; D6BC874521961F73006163F1 /* Gifu.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D6BC874421961F73006163F1 /* Gifu.framework */; }; D6BC874621961F73006163F1 /* Gifu.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D6BC874421961F73006163F1 /* Gifu.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + D6BC8748219738E1006163F1 /* EnhancedTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BC8747219738E1006163F1 /* EnhancedTableViewController.swift */; }; D6BED170212663DA00F02DA0 /* SwiftSoup.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; D6BED174212667E900F02DA0 /* StatusTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BED173212667E900F02DA0 /* StatusTableViewCell.swift */; }; D6C693CA2161253F007D6A6D /* SilentActionPermissionsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6C693C92161253F007D6A6D /* SilentActionPermissionsTableViewController.swift */; }; @@ -313,6 +314,7 @@ D6A5FAFA217B86CE003DB2D9 /* OnboardingViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = OnboardingViewController.xib; sourceTree = ""; }; D6B8DB332182A59300424AF7 /* UIAlertController+Visibility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIAlertController+Visibility.swift"; sourceTree = ""; }; D6BC874421961F73006163F1 /* Gifu.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Gifu.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + D6BC8747219738E1006163F1 /* EnhancedTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnhancedTableViewController.swift; sourceTree = ""; }; D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftSoup.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D6BED173212667E900F02DA0 /* StatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusTableViewCell.swift; sourceTree = ""; }; D6C693C92161253F007D6A6D /* SilentActionPermissionsTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SilentActionPermissionsTableViewController.swift; sourceTree = ""; }; @@ -740,6 +742,7 @@ D6E0DC8D216EDF1E00369478 /* Previewing.swift */, D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */, D6B8DB332182A59300424AF7 /* UIAlertController+Visibility.swift */, + D6BC8747219738E1006163F1 /* EnhancedTableViewController.swift */, ); path = Utilities; sourceTree = ""; @@ -1208,6 +1211,7 @@ D6F1F84D2193B56E00F5FE67 /* Cache.swift in Sources */, D6757A7E2157E02600721E32 /* XCBRequestSpec.swift in Sources */, D667E5F12134D5050057A976 /* UIViewController+Delegates.swift in Sources */, + D6BC8748219738E1006163F1 /* EnhancedTableViewController.swift in Sources */, D663625F2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift in Sources */, D667E5E721349D4C0057A976 /* ProfileTableViewController.swift in Sources */, D6D4DDD0212518A000E1C4BB /* AppDelegate.swift in Sources */, diff --git a/Tusker/Screens/Conversation/ConversationTableViewController.swift b/Tusker/Screens/Conversation/ConversationTableViewController.swift index e0ab9279f0..5bf8686ff5 100644 --- a/Tusker/Screens/Conversation/ConversationTableViewController.swift +++ b/Tusker/Screens/Conversation/ConversationTableViewController.swift @@ -9,7 +9,7 @@ import UIKit import Pachyderm -class ConversationTableViewController: UITableViewController { +class ConversationTableViewController: EnhancedTableViewController { let router: AppRouter diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index a45444c9ee..2c4afefb76 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -9,7 +9,7 @@ import UIKit import Pachyderm -class NotificationsTableViewController: UITableViewController { +class NotificationsTableViewController: EnhancedTableViewController { let router: AppRouter diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index b83726393f..fc46272a19 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -10,7 +10,7 @@ import UIKit import Pachyderm import SafariServices -class ProfileTableViewController: UITableViewController, PreferencesAdaptive { +class ProfileTableViewController: EnhancedTableViewController, PreferencesAdaptive { let router: AppRouter diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index 29b26b3be3..a9d2eb1012 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -9,7 +9,7 @@ import UIKit import Pachyderm -class TimelineTableViewController: UITableViewController { +class TimelineTableViewController: EnhancedTableViewController { let router: AppRouter diff --git a/Tusker/Screens/Utilities/EnhancedTableViewController.swift b/Tusker/Screens/Utilities/EnhancedTableViewController.swift new file mode 100644 index 0000000000..e82d9bf2b2 --- /dev/null +++ b/Tusker/Screens/Utilities/EnhancedTableViewController.swift @@ -0,0 +1,38 @@ +// +// EnhancedTableViewController.swift +// Tusker +// +// Created by Shadowfacts on 11/10/18. +// Copyright © 2018 Shadowfacts. All rights reserved. +// + +import UIKit + +class EnhancedTableViewController: UITableViewController { + + var prevScrollToTopOffset: CGPoint? = nil + + private var topOffset: CGPoint { + // when scrolled to top, the content offset is negative the height of the UI above the scroll view (i.e. the nav and status bars) + let barOffset = -1 * (navigationController!.navigationBar.frame.height + UIApplication.shared.statusBarFrame.height) + // add one so it's not technically all the way at the top, and scrollViewWShouldScrollToTop is still called to trigger undo + return CGPoint(x: 0, y: barOffset + 1) + } + + override func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { + if let offset = prevScrollToTopOffset { + tableView.setContentOffset(offset, animated: true) + prevScrollToTopOffset = nil + } else { + prevScrollToTopOffset = tableView.contentOffset + tableView.setContentOffset(topOffset, animated: true) + } + return false + } + + override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { + prevScrollToTopOffset = nil + print(tableView.contentOffset) + } + +}