Merge pull request #1169 from rizwankce/feature/double-tap-search

Add option to focus the search bar when double tapping the search tab
This commit is contained in:
Nathan Mattes 2023-11-21 12:57:01 +01:00 committed by GitHub
commit 2c26b3f97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 34 deletions

View File

@ -12,6 +12,7 @@ import MastodonCore
protocol ContentSplitViewControllerDelegate: AnyObject { protocol ContentSplitViewControllerDelegate: AnyObject {
func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab) func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didSelectTab tab: MainTabBarController.Tab)
func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didDoubleTapTab tab: MainTabBarController.Tab)
} }
final class ContentSplitViewController: UIViewController, NeedsDependency { final class ContentSplitViewController: UIViewController, NeedsDependency {
@ -121,16 +122,7 @@ extension ContentSplitViewController: SidebarViewControllerDelegate {
} }
func sidebarViewController(_ sidebarViewController: SidebarViewController, didDoubleTapItem item: SidebarViewModel.Item, sourceView: UIView) { func sidebarViewController(_ sidebarViewController: SidebarViewController, didDoubleTapItem item: SidebarViewModel.Item, sourceView: UIView) {
guard case let .tab(tab) = item, tab == .me else { return } guard case let .tab(tab) = item else { return }
guard let authContext = authContext else { return } delegate?.contentSplitViewController(self, sidebarViewController: sidebarViewController, didDoubleTapTab: tab)
assert(Thread.isMainThread)
guard let nextAccount = context.nextAccount(in: authContext) else { return }
Task { @MainActor in
let isActive = try await context.authenticationService.activeMastodonUser(domain: nextAccount.domain, userID: nextAccount.userID)
guard isActive else { return }
self.coordinator.setup()
}
} }
} }

View File

@ -292,12 +292,11 @@ extension MainTabBarController {
tabBarLongPressGestureRecognizer.delegate = self tabBarLongPressGestureRecognizer.delegate = self
tabBar.addGestureRecognizer(tabBarLongPressGestureRecognizer) tabBar.addGestureRecognizer(tabBarLongPressGestureRecognizer)
// todo: reconsider the "double tap to change account" feature -> https://github.com/mastodon/mastodon-ios/issues/628 let tabBarDoubleTapGestureRecognizer = UITapGestureRecognizer()
// let tabBarDoubleTapGestureRecognizer = UITapGestureRecognizer() tabBarDoubleTapGestureRecognizer.numberOfTapsRequired = 2
// tabBarDoubleTapGestureRecognizer.numberOfTapsRequired = 2 tabBarDoubleTapGestureRecognizer.addTarget(self, action: #selector(MainTabBarController.tabBarDoubleTapGestureRecognizerHandler(_:)))
// tabBarDoubleTapGestureRecognizer.addTarget(self, action: #selector(MainTabBarController.tabBarDoubleTapGestureRecognizerHandler(_:))) tabBarDoubleTapGestureRecognizer.delaysTouchesEnded = false
// tabBarDoubleTapGestureRecognizer.delaysTouchesEnded = false tabBar.addGestureRecognizer(tabBarDoubleTapGestureRecognizer)
// tabBar.addGestureRecognizer(tabBarDoubleTapGestureRecognizer)
self.isReadyForWizardAvatarButton = authContext != nil self.isReadyForWizardAvatarButton = authContext != nil
@ -359,17 +358,10 @@ extension MainTabBarController {
guard let tab = touchedTab(by: sender) else { return } guard let tab = touchedTab(by: sender) else { return }
switch tab { switch tab {
case .me: case .search:
guard let authContext = authContext else { return }
assert(Thread.isMainThread) assert(Thread.isMainThread)
// double tapping search tab opens the search bar without additional taps
guard let nextAccount = context.nextAccount(in: authContext) else { return } searchViewController?.searchBar.becomeFirstResponder()
Task { @MainActor in
let isActive = try await context.authenticationService.activeMastodonUser(domain: nextAccount.domain, userID: nextAccount.userID)
guard isActive else { return }
self.coordinator.setup()
}
default: default:
break break
} }

View File

@ -157,6 +157,24 @@ extension RootSplitViewController: ContentSplitViewControllerDelegate {
} }
} }
func contentSplitViewController(_ contentSplitViewController: ContentSplitViewController, sidebarViewController: SidebarViewController, didDoubleTapTab tab: MainTabBarController.Tab) {
guard let _ = MainTabBarController.Tab.allCases.firstIndex(of: tab) else {
assertionFailure()
return
}
switch tab {
case .search:
// allow double tap to focus search bar only when is not primary display (iPad potrait)
guard !isPrimaryDisplay else {
return
}
contentSplitViewController.mainTabBarController.searchViewController?.searchBar.becomeFirstResponder()
default:
break
}
}
} }
// MARK: - UISplitViewControllerDelegate // MARK: - UISplitViewControllerDelegate

View File

@ -128,13 +128,12 @@ extension SidebarViewController {
sidebarLongPressGestureRecognizer.addTarget(self, action: #selector(SidebarViewController.sidebarLongPressGestureRecognizerHandler(_:))) sidebarLongPressGestureRecognizer.addTarget(self, action: #selector(SidebarViewController.sidebarLongPressGestureRecognizerHandler(_:)))
collectionView.addGestureRecognizer(sidebarLongPressGestureRecognizer) collectionView.addGestureRecognizer(sidebarLongPressGestureRecognizer)
// todo: reconsider the "double tap to change account" feature -> https://github.com/mastodon/mastodon-ios/issues/628 let sidebarDoubleTapGestureRecognizer = UITapGestureRecognizer()
// let sidebarDoubleTapGestureRecognizer = UITapGestureRecognizer() sidebarDoubleTapGestureRecognizer.numberOfTapsRequired = 2
// sidebarDoubleTapGestureRecognizer.numberOfTapsRequired = 2 sidebarDoubleTapGestureRecognizer.addTarget(self, action: #selector(SidebarViewController.sidebarDoubleTapGestureRecognizerHandler(_:)))
// sidebarDoubleTapGestureRecognizer.addTarget(self, action: #selector(SidebarViewController.sidebarDoubleTapGestureRecognizerHandler(_:))) sidebarDoubleTapGestureRecognizer.delaysTouchesEnded = false
// sidebarDoubleTapGestureRecognizer.delaysTouchesEnded = false sidebarDoubleTapGestureRecognizer.cancelsTouchesInView = true
// sidebarDoubleTapGestureRecognizer.cancelsTouchesInView = true collectionView.addGestureRecognizer(sidebarDoubleTapGestureRecognizer)
// collectionView.addGestureRecognizer(sidebarDoubleTapGestureRecognizer)
} }

View File

@ -233,6 +233,7 @@ extension SearchDetailViewController {
navigationItem.setHidesBackButton(true, animated: false) navigationItem.setHidesBackButton(true, animated: false)
navigationItem.titleView = nil navigationItem.titleView = nil
navigationItem.searchController = searchController navigationItem.searchController = searchController
navigationItem.preferredSearchBarPlacement = .stacked
searchController.searchBar.sizeToFit() searchController.searchBar.sizeToFit()
} }