fix(Categories): search text works without crashes while quickly adding or removing characters
MODIFY
ACHNBrowserUI/ACHNBrowserUI/viewModels/CategoriesSearchViewModel.swift
ACHNBrowserUI/ACHNBrowserUI/viewModels/CategoriesSearchViewModel.swift
@@ -17,25 +17,22 @@ class CategoriesSearchViewModel: ObservableObject {
private var searchCancellable: AnyCancellable?
init() {
- searchCancellable = _searchText
- .projectedValue
+ searchCancellable = $searchText
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.removeDuplicates()
.filter { !$0.isEmpty }
.map(itemsInCategories(with:))
- .assign(to: \.searchResults, on: self)
+ .subscribe(on: DispatchQueue.global())
+ .receive(on: DispatchQueue.main)
+ .sink(receiveValue: { [weak self] in self?.searchResults = $0})
}
private func itemsInCategories(with string: String) -> [Categories: [Item]] {
- var results: [Categories: [Item]] = [:]
- Items.shared.categories.forEach {
- let items = $1.filter {
+ Items.shared.categories
+ .mapValues({ $0.filter({
$0.name.lowercased().contains(string.lowercased())
- }.prefix(5).compactMap{ $0 }
- if !items.isEmpty {
- results[$0] = items
- }
- }
- return results
+ }) })
+ .filter { !$0.value.isEmpty }
+ .mapValues { Array($0.prefix(5)) }
}
}

MODIFY
ACHNBrowserUI/ACHNBrowserUI/views/categories/CategoriesView.swift
ACHNBrowserUI/ACHNBrowserUI/views/categories/CategoriesView.swift
@@ -83,7 +83,7 @@ struct CategoriesView: View {
}
}
}
- }
+ }.modifier(DismissingKeyboardOnSwipe())
}
.navigationBarTitle(Text("Catalog"), displayMode: .inline)
.background(Color.grassBackground)

fix(Categories): search text works without crashes while quickly adding or removing characters