Search performance improvements

This commit is contained in:
Arkadiusz Fal 2022-08-17 17:34:25 +02:00
parent fda7839527
commit ff497cb09b
6 changed files with 84 additions and 89 deletions

View File

@ -0,0 +1,12 @@
import Foundation
import SwiftUI
extension Backport where Content: View {
@ViewBuilder func scrollDismissesKeyboard() -> some View {
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, *) {
content.scrollDismissesKeyboard(.immediately)
} else {
content
}
}
}

View File

@ -1,4 +1,5 @@
import Defaults import Defaults
import Repeat
import Siesta import Siesta
import SwiftUI import SwiftUI
@ -9,9 +10,9 @@ final class SearchModel: ObservableObject {
@Published var query = SearchQuery() @Published var query = SearchQuery()
@Published var queryText = "" @Published var queryText = ""
@Published var suggestionsText = "" @Published var suggestionsText = ""
@Published var suggestionSelection = ""
@Published var querySuggestions = Store<[String]>() @Published var querySuggestions = [String]()
private var suggestionsDebouncer = Debouncer(.milliseconds(200))
var accounts = AccountsModel() var accounts = AccountsModel()
private var resource: Resource! private var resource: Resource!
@ -74,34 +75,27 @@ final class SearchModel: ObservableObject {
} }
var suggestionsResource: Resource? { didSet { var suggestionsResource: Resource? { didSet {
oldValue?.removeObservers(ownedBy: querySuggestions)
oldValue?.cancelLoadIfUnobserved() oldValue?.cancelLoadIfUnobserved()
objectWillChange.send() objectWillChange.send()
}} }}
func loadSuggestions(_ query: String) { func loadSuggestions(_ query: String) {
guard !query.isEmpty else { suggestionsDebouncer.callback = {
querySuggestions.replace([]) guard !query.isEmpty else { return }
return DispatchQueue.main.async {
} self.accounts.api.searchSuggestions(query: query).load().onSuccess { response in
DispatchQueue.main.async {
self.suggestionsResource = self.accounts.api.searchSuggestions(query: query)
self.suggestionsResource?.addObserver(self.querySuggestions)
if let request = self.suggestionsResource?.loadIfNeeded() {
request.onSuccess { response in
if let suggestions: [String] = response.typedContent() { if let suggestions: [String] = response.typedContent() {
self.querySuggestions = Store<[String]>(suggestions) self.querySuggestions = suggestions
} else {
self.querySuggestions = []
} }
self.suggestionsText = query self.suggestionsText = query
} }
} else {
self.querySuggestions = Store<[String]>(self.querySuggestions.collection)
self.suggestionsText = query
} }
} }
suggestionsDebouncer.call()
} }
func loadNextPage() { func loadNextPage() {

View File

@ -8,16 +8,9 @@ struct SearchTextField: View {
@EnvironmentObject<RecentsModel> private var recents @EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var state @EnvironmentObject<SearchModel> private var state
@Binding var queryText: String
@Binding var favoriteItem: FavoriteItem? @Binding var favoriteItem: FavoriteItem?
private var queryDebouncer = Debouncer(.milliseconds(800)) init(favoriteItem: Binding<FavoriteItem?>? = nil) {
init(
queryText: Binding<String>,
favoriteItem: Binding<FavoriteItem?>? = nil
) {
_queryText = queryText
_favoriteItem = favoriteItem ?? .constant(nil) _favoriteItem = favoriteItem ?? .constant(nil)
} }
@ -36,7 +29,7 @@ struct SearchTextField: View {
.padding(.horizontal, 8) .padding(.horizontal, 8)
.opacity(0.8) .opacity(0.8)
#endif #endif
TextField("Search...", text: $queryText) { TextField("Search...", text: $state.queryText) {
state.changeQuery { query in state.changeQuery { query in
query.query = state.queryText query.query = state.queryText
navigation.hideKeyboard() navigation.hideKeyboard()
@ -44,33 +37,25 @@ struct SearchTextField: View {
recents.addQuery(state.queryText, navigation: navigation) recents.addQuery(state.queryText, navigation: navigation)
} }
.disableAutocorrection(true) .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) #if os(macOS)
.frame(maxWidth: 190) .frame(maxWidth: 190)
.textFieldStyle(.plain) .textFieldStyle(.plain)
#else #else
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)
.padding(.leading) .padding(.leading)
.padding(.trailing, 15) .padding(.trailing, 15)
#endif #endif
if !self.state.queryText.isEmpty { if let favoriteItem = favoriteItem {
#if os(iOS) #if os(iOS)
FavoriteButton(item: favoriteItem) FavoriteButton(item: favoriteItem)
.id(favoriteItem?.id) .id(favoriteItem.id)
.labelStyle(.iconOnly) .labelStyle(.iconOnly)
.padding(.trailing) .padding(.trailing)
#endif #endif
}
if !state.queryText.isEmpty {
clearButton clearButton
} else { } else {
#if os(macOS) #if os(macOS)
@ -96,7 +81,6 @@ struct SearchTextField: View {
private var clearButton: some View { private var clearButton: some View {
Button(action: { Button(action: {
queryText = ""
self.state.queryText = "" self.state.queryText = ""
}) { }) {
Image(systemName: "xmark.circle.fill") Image(systemName: "xmark.circle.fill")

View File

@ -7,20 +7,22 @@ struct SearchSuggestions: View {
var body: some View { var body: some View {
List { List {
Button { if !state.queryText.isEmpty {
runQueryAction(state.queryText) Button {
} label: { runQueryAction(state.queryText)
HStack { } label: {
Image(systemName: "magnifyingglass") HStack {
Text(state.queryText) Image(systemName: "magnifyingglass")
.lineLimit(1) Text(state.queryText)
.lineLimit(1)
}
} }
} .padding(.vertical, 5)
.padding(.vertical, 5)
#if os(macOS) #if os(macOS)
.onHover(perform: onHover(_:)) .onHover(perform: onHover(_:))
#endif #endif
}
ForEach(visibleSuggestions, id: \.self) { suggestion in ForEach(visibleSuggestions, id: \.self) { suggestion in
HStack { HStack {
@ -51,7 +53,7 @@ struct SearchSuggestions: View {
Spacer() Spacer()
Button { Button {
state.suggestionSelection = suggestion state.queryText = suggestion
} label: { } label: {
Image(systemName: "arrow.up.left.circle") Image(systemName: "arrow.up.left.circle")
.foregroundColor(.secondary) .foregroundColor(.secondary)
@ -65,14 +67,16 @@ struct SearchSuggestions: View {
#endif #endif
} }
} }
.id(UUID()) #if os(iOS)
.padding(.bottom, 90)
#endif
#if os(macOS) #if os(macOS)
.buttonStyle(.link) .buttonStyle(.link)
#endif #endif
} }
private func runQueryAction(_ queryText: String) { private func runQueryAction(_ queryText: String) {
state.suggestionSelection = queryText state.queryText = queryText
state.changeQuery { query in state.changeQuery { query in
query.query = queryText query.query = queryText
@ -83,7 +87,7 @@ struct SearchSuggestions: View {
} }
private var visibleSuggestions: [String] { private var visibleSuggestions: [String] {
state.querySuggestions.collection.filter { state.querySuggestions.filter {
$0.compare(state.queryText, options: .caseInsensitive) != .orderedSame $0.compare(state.queryText, options: .caseInsensitive) != .orderedSame
} }
} }

View File

@ -17,7 +17,6 @@ struct SearchView: View {
#endif #endif
@State private var favoriteItem: FavoriteItem? @State private var favoriteItem: FavoriteItem?
@State private var queryText = ""
@Environment(\.navigationStyle) private var navigationStyle @Environment(\.navigationStyle) private var navigationStyle
@ -61,25 +60,29 @@ struct SearchView: View {
}) { }) {
#if os(iOS) #if os(iOS)
VStack { 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() SearchSuggestions()
.opacity(state.queryText.isEmpty ? 0 : 1)
} else { } else {
results results
} }
} }
.backport
.scrollDismissesKeyboard()
#else #else
ZStack { ZStack {
results results
#if os(macOS) #if os(macOS)
if state.query.query != queryText, !queryText.isEmpty, !state.querySuggestions.collection.isEmpty { if state.query.query != state.queryText {
HStack { HStack {
Spacer() Spacer()
SearchSuggestions() SearchSuggestions()
.borderLeading(width: 1, color: Color("ControlsBorderColor")) .borderLeading(width: 1, color: Color("ControlsBorderColor"))
.frame(maxWidth: 280) .frame(maxWidth: 280)
.opacity(state.queryText.isEmpty ? 0 : 1)
} }
} }
#endif #endif
@ -108,13 +111,12 @@ struct SearchView: View {
filtersMenu filtersMenu
} }
SearchTextField(queryText: $queryText) SearchTextField(favoriteItem: $favoriteItem)
} }
#endif #endif
} }
.onAppear { .onAppear {
if let query = query { if let query = query {
queryText = query.query
state.queryText = query.query state.queryText = query.query
state.resetQuery(query) state.resetQuery(query)
updateFavoriteItem() updateFavoriteItem()
@ -125,10 +127,6 @@ struct SearchView: View {
} }
} }
.onChange(of: state.queryText) { newQuery in .onChange(of: state.queryText) { newQuery in
if queryText.isEmpty, queryText != newQuery {
queryText = newQuery
}
if newQuery.isEmpty { if newQuery.isEmpty {
favoriteItem = nil favoriteItem = nil
state.resetQuery() state.resetQuery()
@ -136,9 +134,7 @@ struct SearchView: View {
updateFavoriteItem() updateFavoriteItem()
} }
if state.query.query != queryText { state.loadSuggestions(newQuery)
state.loadSuggestions(newQuery)
}
#if os(tvOS) #if os(tvOS)
searchDebounce.invalidate() searchDebounce.invalidate()
@ -147,7 +143,6 @@ struct SearchView: View {
searchDebounce.debouncing(2) { searchDebounce.debouncing(2) {
state.changeQuery { query in state.changeQuery { query in
query.query = newQuery query.query = newQuery
updateFavoriteItem()
} }
} }
@ -176,9 +171,11 @@ struct SearchView: View {
} }
#if os(tvOS) #if os(tvOS)
.searchable(text: $state.queryText) { .searchable(text: $state.queryText) {
ForEach(state.querySuggestions.collection, id: \.self) { suggestion in if !state.queryText.isEmpty {
Text(suggestion) ForEach(state.querySuggestions, id: \.self) { suggestion in
.searchCompletion(suggestion) Text(suggestion)
.searchCompletion(suggestion)
}
} }
} }
#else #else
@ -224,6 +221,9 @@ struct SearchView: View {
VStack { VStack {
if showRecentQueries { if showRecentQueries {
recentQueries recentQueries
#if os(iOS)
.padding(.bottom, 90)
#endif
} else { } else {
#if os(tvOS) #if os(tvOS)
ScrollView(.vertical, showsIndicators: false) { ScrollView(.vertical, showsIndicators: false) {
@ -311,12 +311,9 @@ struct SearchView: View {
Button { Button {
switch item.type { switch item.type {
case .query: case .query:
#if os(tvOS) state.queryText = item.title
state.queryText = item.title
#else
queryText = item.title
#endif
state.changeQuery { query in query.query = item.title } state.changeQuery { query in query.query = item.title }
navigation.hideKeyboard()
updateFavoriteItem() updateFavoriteItem()
recents.add(item) recents.add(item)
@ -493,10 +490,10 @@ struct SearchView: View {
private func updateFavoriteItem() { private func updateFavoriteItem() {
favoriteItem = FavoriteItem(section: .searchQuery( favoriteItem = FavoriteItem(section: .searchQuery(
state.query.query, state.queryText,
state.query.date?.rawValue ?? "", searchDate.rawValue,
state.query.duration?.rawValue ?? "", searchDuration.rawValue,
state.query.sortBy.rawValue searchSortOrder.rawValue
)) ))
} }
} }

View File

@ -399,6 +399,7 @@
376CD21626FBE18D001E1AC1 /* Instance+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */; }; 376CD21626FBE18D001E1AC1 /* Instance+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */; };
376CD21726FBE18D001E1AC1 /* Instance+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */; }; 376CD21726FBE18D001E1AC1 /* Instance+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */; };
376CD21826FBE18D001E1AC1 /* Instance+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */; }; 376CD21826FBE18D001E1AC1 /* Instance+Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */; };
376E331228AD3B320070E30C /* ScrollDismissesKeyboard+Backport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 376E331128AD3B320070E30C /* ScrollDismissesKeyboard+Backport.swift */; };
3772003827E8EEB100CB2475 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3772003227E8EEA100CB2475 /* AudioToolbox.framework */; }; 3772003827E8EEB100CB2475 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3772003227E8EEA100CB2475 /* AudioToolbox.framework */; };
3772003927E8EEB700CB2475 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3772003427E8EEA100CB2475 /* AVFoundation.framework */; }; 3772003927E8EEB700CB2475 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3772003427E8EEA100CB2475 /* AVFoundation.framework */; };
3772003A27E8EEBE00CB2475 /* CoreMedia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3772003527E8EEA100CB2475 /* CoreMedia.framework */; }; 3772003A27E8EEBE00CB2475 /* CoreMedia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3772003527E8EEA100CB2475 /* CoreMedia.framework */; };
@ -1078,6 +1079,7 @@
376BE50627347B57009AD608 /* SettingsHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsHeader.swift; sourceTree = "<group>"; }; 376BE50627347B57009AD608 /* SettingsHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsHeader.swift; sourceTree = "<group>"; };
376BE50A27349108009AD608 /* BrowsingSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowsingSettings.swift; sourceTree = "<group>"; }; 376BE50A27349108009AD608 /* BrowsingSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowsingSettings.swift; sourceTree = "<group>"; };
376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Instance+Fixtures.swift"; sourceTree = "<group>"; }; 376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Instance+Fixtures.swift"; sourceTree = "<group>"; };
376E331128AD3B320070E30C /* ScrollDismissesKeyboard+Backport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ScrollDismissesKeyboard+Backport.swift"; sourceTree = "<group>"; };
3772002527E8ED2600CB2475 /* BridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BridgingHeader.h; sourceTree = "<group>"; }; 3772002527E8ED2600CB2475 /* BridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BridgingHeader.h; sourceTree = "<group>"; };
3772003127E8EEA100CB2475 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS15.4.sdk/usr/lib/libz.tbd; sourceTree = DEVELOPER_DIR; }; 3772003127E8EEA100CB2475 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS15.4.sdk/usr/lib/libz.tbd; sourceTree = DEVELOPER_DIR; };
3772003227E8EEA100CB2475 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS15.4.sdk/System/Library/Frameworks/AudioToolbox.framework; sourceTree = DEVELOPER_DIR; }; 3772003227E8EEA100CB2475 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS15.4.sdk/System/Library/Frameworks/AudioToolbox.framework; sourceTree = DEVELOPER_DIR; };
@ -1613,6 +1615,7 @@
3722AEBF274DAEB8005EA4D6 /* Tint+Backport.swift */, 3722AEBF274DAEB8005EA4D6 /* Tint+Backport.swift */,
3727B74927872A920021C15E /* VisualEffectBlur-iOS.swift */, 3727B74927872A920021C15E /* VisualEffectBlur-iOS.swift */,
3727B74727872A500021C15E /* VisualEffectBlur-macOS.swift */, 3727B74727872A500021C15E /* VisualEffectBlur-macOS.swift */,
376E331128AD3B320070E30C /* ScrollDismissesKeyboard+Backport.swift */,
); );
path = Backports; path = Backports;
sourceTree = "<group>"; sourceTree = "<group>";
@ -2812,6 +2815,7 @@
373C8FE4275B955100CB5936 /* CommentsPage.swift in Sources */, 373C8FE4275B955100CB5936 /* CommentsPage.swift in Sources */,
3700155B271B0D4D0049C794 /* PipedAPI.swift in Sources */, 3700155B271B0D4D0049C794 /* PipedAPI.swift in Sources */,
373031F32838388A000CFD59 /* PlayerLayerView.swift in Sources */, 373031F32838388A000CFD59 /* PlayerLayerView.swift in Sources */,
376E331228AD3B320070E30C /* ScrollDismissesKeyboard+Backport.swift in Sources */,
37B044B726F7AB9000E1419D /* SettingsView.swift in Sources */, 37B044B726F7AB9000E1419D /* SettingsView.swift in Sources */,
377FC7E3267A084A00A6BBAF /* VideoCell.swift in Sources */, 377FC7E3267A084A00A6BBAF /* VideoCell.swift in Sources */,
37C3A251272366440087A57A /* ChannelPlaylistView.swift in Sources */, 37C3A251272366440087A57A /* ChannelPlaylistView.swift in Sources */,