Improve search suggestions

This commit is contained in:
Arkadiusz Fal 2021-12-06 19:12:33 +01:00
parent 62bff9283c
commit eeb7b1f151
2 changed files with 19 additions and 11 deletions

View File

@ -9,6 +9,7 @@ final class SearchModel: ObservableObject {
@Published var query = SearchQuery() @Published var query = SearchQuery()
@Published var queryText = "" @Published var queryText = ""
@Published var querySuggestions = Store<[String]>() @Published var querySuggestions = Store<[String]>()
@Published var suggestionsText = ""
@Published var fieldIsFocused = false @Published var fieldIsFocused = false
@ -88,7 +89,7 @@ final class SearchModel: ObservableObject {
suggestionsDebounceTimer?.invalidate() suggestionsDebounceTimer?.invalidate()
suggestionsDebounceTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in suggestionsDebounceTimer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { _ in
let resource = self.accounts.api.searchSuggestions(query: query) let resource = self.accounts.api.searchSuggestions(query: query)
resource.addObserver(self.querySuggestions) resource.addObserver(self.querySuggestions)
@ -99,9 +100,11 @@ final class SearchModel: ObservableObject {
if let suggestions: [String] = response.typedContent() { if let suggestions: [String] = response.typedContent() {
self.querySuggestions = Store<[String]>(suggestions) self.querySuggestions = Store<[String]>(suggestions)
} }
self.suggestionsText = query
} }
} else { } else {
self.querySuggestions = Store<[String]>(self.querySuggestions.collection) self.querySuggestions = Store<[String]>(self.querySuggestions.collection)
self.suggestionsText = query
} }
} }
} }

View File

@ -14,8 +14,9 @@ struct SearchSuggestions: View {
recents.addQuery(state.queryText) recents.addQuery(state.queryText)
} label: { } label: {
HStack(spacing: 5) { HStack {
Label(state.queryText, systemImage: "magnifyingglass") Image(systemName: "magnifyingglass")
Text(state.queryText)
.lineLimit(1) .lineLimit(1)
} }
} }
@ -27,8 +28,11 @@ struct SearchSuggestions: View {
Button { Button {
state.queryText = suggestion state.queryText = suggestion
} label: { } label: {
HStack {
Image(systemName: "arrow.up.left.circle")
.foregroundColor(.secondary)
HStack(spacing: 0) { HStack(spacing: 0) {
Label(state.queryText, systemImage: "arrow.up.left.circle") Text(state.suggestionsText)
.lineLimit(1) .lineLimit(1)
.layoutPriority(2) .layoutPriority(2)
.foregroundColor(.secondary) .foregroundColor(.secondary)
@ -38,6 +42,7 @@ struct SearchSuggestions: View {
.layoutPriority(1) .layoutPriority(1)
} }
} }
}
#if os(macOS) #if os(macOS)
.onHover(perform: onHover(_:)) .onHover(perform: onHover(_:))
#endif #endif
@ -55,7 +60,7 @@ struct SearchSuggestions: View {
} }
private func querySuffix(_ suggestion: String) -> String { private func querySuffix(_ suggestion: String) -> String {
suggestion.replacingFirstOccurrence(of: state.queryText.lowercased(), with: "") suggestion.replacingFirstOccurrence(of: state.suggestionsText.lowercased(), with: "")
} }
#if os(macOS) #if os(macOS)