mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 02:04:07 +00:00
Search performance improvements
This commit is contained in:
@@ -8,16 +8,9 @@ struct SearchTextField: View {
|
||||
@EnvironmentObject<RecentsModel> private var recents
|
||||
@EnvironmentObject<SearchModel> private var state
|
||||
|
||||
@Binding var queryText: String
|
||||
@Binding var favoriteItem: FavoriteItem?
|
||||
|
||||
private var queryDebouncer = Debouncer(.milliseconds(800))
|
||||
|
||||
init(
|
||||
queryText: Binding<String>,
|
||||
favoriteItem: Binding<FavoriteItem?>? = nil
|
||||
) {
|
||||
_queryText = queryText
|
||||
init(favoriteItem: Binding<FavoriteItem?>? = nil) {
|
||||
_favoriteItem = favoriteItem ?? .constant(nil)
|
||||
}
|
||||
|
||||
@@ -36,7 +29,7 @@ struct SearchTextField: View {
|
||||
.padding(.horizontal, 8)
|
||||
.opacity(0.8)
|
||||
#endif
|
||||
TextField("Search...", text: $queryText) {
|
||||
TextField("Search...", text: $state.queryText) {
|
||||
state.changeQuery { query in
|
||||
query.query = state.queryText
|
||||
navigation.hideKeyboard()
|
||||
@@ -44,33 +37,25 @@ struct SearchTextField: View {
|
||||
recents.addQuery(state.queryText, navigation: navigation)
|
||||
}
|
||||
.disableAutocorrection(true)
|
||||
.onChange(of: state.suggestionSelection) { newValue in
|
||||
self.queryText = newValue
|
||||
}
|
||||
.onChange(of: queryText) { newValue in
|
||||
queryDebouncer.callback = {
|
||||
DispatchQueue.main.async {
|
||||
state.queryText = newValue
|
||||
}
|
||||
}
|
||||
queryDebouncer.call()
|
||||
}
|
||||
#if os(macOS)
|
||||
.frame(maxWidth: 190)
|
||||
.textFieldStyle(.plain)
|
||||
.frame(maxWidth: 190)
|
||||
.textFieldStyle(.plain)
|
||||
#else
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.leading)
|
||||
.padding(.trailing, 15)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.leading)
|
||||
.padding(.trailing, 15)
|
||||
#endif
|
||||
|
||||
if !self.state.queryText.isEmpty {
|
||||
if let favoriteItem = favoriteItem {
|
||||
#if os(iOS)
|
||||
FavoriteButton(item: favoriteItem)
|
||||
.id(favoriteItem?.id)
|
||||
.id(favoriteItem.id)
|
||||
.labelStyle(.iconOnly)
|
||||
.padding(.trailing)
|
||||
#endif
|
||||
}
|
||||
|
||||
if !state.queryText.isEmpty {
|
||||
clearButton
|
||||
} else {
|
||||
#if os(macOS)
|
||||
@@ -96,7 +81,6 @@ struct SearchTextField: View {
|
||||
|
||||
private var clearButton: some View {
|
||||
Button(action: {
|
||||
queryText = ""
|
||||
self.state.queryText = ""
|
||||
}) {
|
||||
Image(systemName: "xmark.circle.fill")
|
||||
|
@@ -7,20 +7,22 @@ struct SearchSuggestions: View {
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
Button {
|
||||
runQueryAction(state.queryText)
|
||||
} label: {
|
||||
HStack {
|
||||
Image(systemName: "magnifyingglass")
|
||||
Text(state.queryText)
|
||||
.lineLimit(1)
|
||||
if !state.queryText.isEmpty {
|
||||
Button {
|
||||
runQueryAction(state.queryText)
|
||||
} label: {
|
||||
HStack {
|
||||
Image(systemName: "magnifyingglass")
|
||||
Text(state.queryText)
|
||||
.lineLimit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 5)
|
||||
.padding(.vertical, 5)
|
||||
|
||||
#if os(macOS)
|
||||
.onHover(perform: onHover(_:))
|
||||
#endif
|
||||
#if os(macOS)
|
||||
.onHover(perform: onHover(_:))
|
||||
#endif
|
||||
}
|
||||
|
||||
ForEach(visibleSuggestions, id: \.self) { suggestion in
|
||||
HStack {
|
||||
@@ -51,7 +53,7 @@ struct SearchSuggestions: View {
|
||||
Spacer()
|
||||
|
||||
Button {
|
||||
state.suggestionSelection = suggestion
|
||||
state.queryText = suggestion
|
||||
} label: {
|
||||
Image(systemName: "arrow.up.left.circle")
|
||||
.foregroundColor(.secondary)
|
||||
@@ -65,14 +67,16 @@ struct SearchSuggestions: View {
|
||||
#endif
|
||||
}
|
||||
}
|
||||
.id(UUID())
|
||||
#if os(iOS)
|
||||
.padding(.bottom, 90)
|
||||
#endif
|
||||
#if os(macOS)
|
||||
.buttonStyle(.link)
|
||||
.buttonStyle(.link)
|
||||
#endif
|
||||
}
|
||||
|
||||
private func runQueryAction(_ queryText: String) {
|
||||
state.suggestionSelection = queryText
|
||||
state.queryText = queryText
|
||||
|
||||
state.changeQuery { query in
|
||||
query.query = queryText
|
||||
@@ -83,7 +87,7 @@ struct SearchSuggestions: View {
|
||||
}
|
||||
|
||||
private var visibleSuggestions: [String] {
|
||||
state.querySuggestions.collection.filter {
|
||||
state.querySuggestions.filter {
|
||||
$0.compare(state.queryText, options: .caseInsensitive) != .orderedSame
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,6 @@ struct SearchView: View {
|
||||
#endif
|
||||
|
||||
@State private var favoriteItem: FavoriteItem?
|
||||
@State private var queryText = ""
|
||||
|
||||
@Environment(\.navigationStyle) private var navigationStyle
|
||||
|
||||
@@ -61,25 +60,29 @@ struct SearchView: View {
|
||||
}) {
|
||||
#if os(iOS)
|
||||
VStack {
|
||||
SearchTextField(queryText: $queryText, favoriteItem: $favoriteItem)
|
||||
SearchTextField(favoriteItem: $favoriteItem)
|
||||
|
||||
if state.query.query != queryText, !queryText.isEmpty, !state.querySuggestions.collection.isEmpty {
|
||||
if state.query.query != state.queryText {
|
||||
SearchSuggestions()
|
||||
.opacity(state.queryText.isEmpty ? 0 : 1)
|
||||
} else {
|
||||
results
|
||||
}
|
||||
}
|
||||
.backport
|
||||
.scrollDismissesKeyboard()
|
||||
#else
|
||||
ZStack {
|
||||
results
|
||||
|
||||
#if os(macOS)
|
||||
if state.query.query != queryText, !queryText.isEmpty, !state.querySuggestions.collection.isEmpty {
|
||||
if state.query.query != state.queryText {
|
||||
HStack {
|
||||
Spacer()
|
||||
SearchSuggestions()
|
||||
.borderLeading(width: 1, color: Color("ControlsBorderColor"))
|
||||
.frame(maxWidth: 280)
|
||||
.opacity(state.queryText.isEmpty ? 0 : 1)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -108,13 +111,12 @@ struct SearchView: View {
|
||||
filtersMenu
|
||||
}
|
||||
|
||||
SearchTextField(queryText: $queryText)
|
||||
SearchTextField(favoriteItem: $favoriteItem)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
.onAppear {
|
||||
if let query = query {
|
||||
queryText = query.query
|
||||
state.queryText = query.query
|
||||
state.resetQuery(query)
|
||||
updateFavoriteItem()
|
||||
@@ -125,10 +127,6 @@ struct SearchView: View {
|
||||
}
|
||||
}
|
||||
.onChange(of: state.queryText) { newQuery in
|
||||
if queryText.isEmpty, queryText != newQuery {
|
||||
queryText = newQuery
|
||||
}
|
||||
|
||||
if newQuery.isEmpty {
|
||||
favoriteItem = nil
|
||||
state.resetQuery()
|
||||
@@ -136,9 +134,7 @@ struct SearchView: View {
|
||||
updateFavoriteItem()
|
||||
}
|
||||
|
||||
if state.query.query != queryText {
|
||||
state.loadSuggestions(newQuery)
|
||||
}
|
||||
state.loadSuggestions(newQuery)
|
||||
|
||||
#if os(tvOS)
|
||||
searchDebounce.invalidate()
|
||||
@@ -147,7 +143,6 @@ struct SearchView: View {
|
||||
searchDebounce.debouncing(2) {
|
||||
state.changeQuery { query in
|
||||
query.query = newQuery
|
||||
updateFavoriteItem()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,9 +171,11 @@ struct SearchView: View {
|
||||
}
|
||||
#if os(tvOS)
|
||||
.searchable(text: $state.queryText) {
|
||||
ForEach(state.querySuggestions.collection, id: \.self) { suggestion in
|
||||
Text(suggestion)
|
||||
.searchCompletion(suggestion)
|
||||
if !state.queryText.isEmpty {
|
||||
ForEach(state.querySuggestions, id: \.self) { suggestion in
|
||||
Text(suggestion)
|
||||
.searchCompletion(suggestion)
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
@@ -224,6 +221,9 @@ struct SearchView: View {
|
||||
VStack {
|
||||
if showRecentQueries {
|
||||
recentQueries
|
||||
#if os(iOS)
|
||||
.padding(.bottom, 90)
|
||||
#endif
|
||||
} else {
|
||||
#if os(tvOS)
|
||||
ScrollView(.vertical, showsIndicators: false) {
|
||||
@@ -311,12 +311,9 @@ struct SearchView: View {
|
||||
Button {
|
||||
switch item.type {
|
||||
case .query:
|
||||
#if os(tvOS)
|
||||
state.queryText = item.title
|
||||
#else
|
||||
queryText = item.title
|
||||
#endif
|
||||
state.queryText = item.title
|
||||
state.changeQuery { query in query.query = item.title }
|
||||
navigation.hideKeyboard()
|
||||
|
||||
updateFavoriteItem()
|
||||
recents.add(item)
|
||||
@@ -493,10 +490,10 @@ struct SearchView: View {
|
||||
|
||||
private func updateFavoriteItem() {
|
||||
favoriteItem = FavoriteItem(section: .searchQuery(
|
||||
state.query.query,
|
||||
state.query.date?.rawValue ?? "",
|
||||
state.query.duration?.rawValue ?? "",
|
||||
state.query.sortBy.rawValue
|
||||
state.queryText,
|
||||
searchDate.rawValue,
|
||||
searchDuration.rawValue,
|
||||
searchSortOrder.rawValue
|
||||
))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user