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 {
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 {
@ -121,16 +122,7 @@ extension ContentSplitViewController: SidebarViewControllerDelegate {
}
func sidebarViewController(_ sidebarViewController: SidebarViewController, didDoubleTapItem item: SidebarViewModel.Item, sourceView: UIView) {
guard case let .tab(tab) = item, tab == .me else { return }
guard let authContext = authContext else { return }
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()
}
guard case let .tab(tab) = item else { return }
delegate?.contentSplitViewController(self, sidebarViewController: sidebarViewController, didDoubleTapTab: tab)
}
}

View File

@ -292,12 +292,11 @@ extension MainTabBarController {
tabBarLongPressGestureRecognizer.delegate = self
tabBar.addGestureRecognizer(tabBarLongPressGestureRecognizer)
// todo: reconsider the "double tap to change account" feature -> https://github.com/mastodon/mastodon-ios/issues/628
// let tabBarDoubleTapGestureRecognizer = UITapGestureRecognizer()
// tabBarDoubleTapGestureRecognizer.numberOfTapsRequired = 2
// tabBarDoubleTapGestureRecognizer.addTarget(self, action: #selector(MainTabBarController.tabBarDoubleTapGestureRecognizerHandler(_:)))
// tabBarDoubleTapGestureRecognizer.delaysTouchesEnded = false
// tabBar.addGestureRecognizer(tabBarDoubleTapGestureRecognizer)
let tabBarDoubleTapGestureRecognizer = UITapGestureRecognizer()
tabBarDoubleTapGestureRecognizer.numberOfTapsRequired = 2
tabBarDoubleTapGestureRecognizer.addTarget(self, action: #selector(MainTabBarController.tabBarDoubleTapGestureRecognizerHandler(_:)))
tabBarDoubleTapGestureRecognizer.delaysTouchesEnded = false
tabBar.addGestureRecognizer(tabBarDoubleTapGestureRecognizer)
self.isReadyForWizardAvatarButton = authContext != nil
@ -359,17 +358,10 @@ extension MainTabBarController {
guard let tab = touchedTab(by: sender) else { return }
switch tab {
case .me:
guard let authContext = authContext else { return }
case .search:
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()
}
// double tapping search tab opens the search bar without additional taps
searchViewController?.searchBar.becomeFirstResponder()
default:
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

View File

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

View File

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