Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Reselection #833

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions Mlem/Views/Shared/Search Bar/SearchBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,34 @@ import SwiftUI
(uiView as? _UISearchBar)?.isFirstResponderBinding = isFocused

do {
if let uiView = uiView as? _UISearchBar, environment.isEnabled {
DispatchQueue.main.async {
if let isFocused, uiView.window != nil {
uiView.isFirstResponderBinding = isFocused

if isFocused.wrappedValue, !uiView.isFirstResponder {
uiView.becomeFirstResponder()
} else if !isFocused.wrappedValue, uiView.isFirstResponder {
uiView.resignFirstResponder()
}
// version of below with no responder binding. it's not a pretty hack but it does work
// note that switching tabs with search selected will result in search still displaying "search for communities and users,"
// but since the keyboard hides the tab bar that probably won't come up for 99% of users
if let isFocused, environment.isEnabled {
if isFocused.wrappedValue, !uiView.isFirstResponder {
DispatchQueue.main.async {
uiView.becomeFirstResponder()
}
} else if !isFocused.wrappedValue, uiView.isFirstResponder {
DispatchQueue.main.async {
uiView.resignFirstResponder()
}
}
}

// if let uiView = uiView as? _UISearchBar, environment.isEnabled {
// DispatchQueue.main.async {
// if let isFocused, uiView.window != nil {
// uiView.isFirstResponderBinding = isFocused
//
// if isFocused.wrappedValue, !uiView.isFirstResponder {
// uiView.becomeFirstResponder()
// } else if !isFocused.wrappedValue, uiView.isFirstResponder {
// uiView.resignFirstResponder()
// }
// }
// }
// }
}
}

Expand Down
28 changes: 22 additions & 6 deletions Mlem/Views/Tabs/Search/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ struct SearchView: View {

// environment
@Environment(\.scrollViewProxy) private var scrollProxy
@Environment(\.navigationPathWithRoutes) private var navigationPath
@Environment(\.tabSelectionHashValue) var tabSelectionHashValue
@EnvironmentObject var appState: AppState
@EnvironmentObject private var recentSearchesTracker: RecentSearchesTracker
@StateObject var searchModel: SearchModel

@StateObject var homeSearchModel: SearchModel
@StateObject var homeContentTracker: ContentTracker<AnyContentModel> = .init()

@State var searchBarFocused: Bool = false
@State var isSearching: Bool = false
@State var page: Page = .home

Expand Down Expand Up @@ -66,6 +69,7 @@ struct SearchView: View {
resultsScrollToTopSignal += 1
recentsScrollToTopSignal += 1
}
.focused($searchBarFocused)
}
.navigationSearchBarHiddenWhenScrolling(false)
.autocorrectionDisabled(true)
Expand All @@ -85,6 +89,24 @@ struct SearchView: View {
resultsScrollToTopSignal += 1
}
}
.onChange(of: tabSelectionHashValue) { newValue in
// due to the hack in SearchBar:136, this is required to move focus off of the search bar when changing tabs
// because the keyboard hides the tab bar and so users are required to cancel a search to switch tabs, this
// probably isn't needed in most cases, but since it's possible to disable the on-screen keyboard I've included it
if tabSelectionHashValue == TabSelection.search.hashValue, newValue != TabSelection.search.hashValue {
searchBarFocused = false
}
}
.hoistNavigation {
if scrollToTopAppeared {
searchBarFocused = true
} else {
withAnimation {
scrollToTop(page: page)
}
}
return true
}
}

@ViewBuilder
Expand Down Expand Up @@ -146,12 +168,6 @@ struct SearchView: View {
}
}
}
.hoistNavigation {
withAnimation {
scrollToTop(page: page)
}
return true
}
}

private func scrollToTop(page: Page) {
Expand Down