Search: Fix keyboard lag with searchText
Make searchText a local variable passed to ScrapingModel to prevent extreme keyboard lag and CPU usage when tracking this EnvironmentObject. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
parent
4f303e1c1e
commit
75be076e0b
3 changed files with 13 additions and 7 deletions
|
|
@ -22,7 +22,7 @@ class ScrapingViewModel: ObservableObject {
|
|||
runningSearchTask = nil
|
||||
}
|
||||
|
||||
@Published var searchText: String = ""
|
||||
var cleanedSearchText: String = ""
|
||||
@Published var searchResults: [SearchResult] = []
|
||||
|
||||
// Only add results with valid magnet hashes to the search results array
|
||||
|
|
@ -66,7 +66,7 @@ class ScrapingViewModel: ObservableObject {
|
|||
await logManager?.error(description, showToast: false)
|
||||
}
|
||||
|
||||
public func scanSources(sources: [Source], debridManager: DebridManager) async {
|
||||
public func scanSources(sources: [Source], searchText: String, debridManager: DebridManager) async {
|
||||
await logManager?.info("Started scanning sources for query \"\(searchText)\"")
|
||||
|
||||
if sources.isEmpty {
|
||||
|
|
@ -78,6 +78,8 @@ class ScrapingViewModel: ObservableObject {
|
|||
return
|
||||
}
|
||||
|
||||
cleanedSearchText = searchText.lowercased()
|
||||
|
||||
if await !debridManager.enabledDebrids.isEmpty {
|
||||
await debridManager.clearIAValues()
|
||||
}
|
||||
|
|
@ -152,7 +154,7 @@ class ScrapingViewModel: ObservableObject {
|
|||
// Default to HTML scraping
|
||||
let preferredParser = SourcePreferredParser(rawValue: source.preferredParser) ?? .none
|
||||
|
||||
guard let encodedQuery = searchText.lowercased().addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
|
||||
guard let encodedQuery = cleanedSearchText.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
|
||||
await sendSourceError("\(source.name): Could not process search query, invalid characters present.")
|
||||
|
||||
return nil
|
||||
|
|
@ -933,7 +935,7 @@ class ScrapingViewModel: ObservableObject {
|
|||
func runRegex(parsedValue: String, regexString: String) -> String? {
|
||||
// TODO: Maybe dynamically parse flags
|
||||
let replacedRegexString = regexString
|
||||
.replacingOccurrences(of: "{query}", with: searchText.lowercased())
|
||||
.replacingOccurrences(of: "{query}", with: cleanedSearchText)
|
||||
|
||||
guard
|
||||
let matchedRegex = try? Regex(
|
||||
|
|
|
|||
|
|
@ -17,13 +17,15 @@ struct SearchResultsView: View {
|
|||
|
||||
@AppStorage("Behavior.UsesRandomSearchText") var usesRandomSearchText: Bool = false
|
||||
|
||||
@Binding var searchText: String
|
||||
|
||||
var body: some View {
|
||||
ForEach(scrapingModel.searchResults, id: \.self) { result in
|
||||
if pluginManager.filteredInstalledSources.isEmpty || pluginManager.filteredInstalledSources.contains(where: { result.source == $0.name }) {
|
||||
SearchResultButtonView(result: result)
|
||||
}
|
||||
}
|
||||
.onChange(of: scrapingModel.searchText) { newText in
|
||||
.onChange(of: searchText) { newText in
|
||||
if newText.isEmpty, isSearching {
|
||||
navModel.getSearchPrompt()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ struct ContentView: View {
|
|||
sortDescriptors: []
|
||||
) var sources: FetchedResults<Source>
|
||||
|
||||
@State private var searchText = ""
|
||||
@State private var isSearching = false
|
||||
@State private var isEditingSearch = false
|
||||
@State private var dismissAction: () -> Void = {}
|
||||
|
|
@ -28,7 +29,7 @@ struct ContentView: View {
|
|||
var body: some View {
|
||||
NavView {
|
||||
List {
|
||||
SearchResultsView()
|
||||
SearchResultsView(searchText: $searchText)
|
||||
}
|
||||
.listStyle(.insetGrouped)
|
||||
.inlinedList(inset: 20)
|
||||
|
|
@ -49,7 +50,7 @@ struct ContentView: View {
|
|||
}
|
||||
}
|
||||
.expandedSearchable(
|
||||
text: $scrapingModel.searchText,
|
||||
text: $searchText,
|
||||
isSearching: $isSearching,
|
||||
isEditingSearch: $isEditingSearch,
|
||||
prompt: navModel.searchPrompt,
|
||||
|
|
@ -89,6 +90,7 @@ struct ContentView: View {
|
|||
(pluginManager.filteredInstalledSources.isEmpty ?
|
||||
sources.compactMap { $0 } :
|
||||
pluginManager.filteredInstalledSources),
|
||||
searchText: searchText,
|
||||
debridManager: debridManager
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue