Skip to content

Commit 4d15427

Browse files
committed
Combine search term and filter to update predicate correctly
1 parent f9f28c7 commit 4d15427

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

WooCommerce/Classes/ViewRelated/Products/ProductSelector/ProductSelectorViewModel.swift

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,7 @@ final class ProductSelectorViewModel: ObservableObject {
3333

3434
/// Selected filter for the product list
3535
///
36-
var filters: FilterProductListViewModel.Filters = FilterProductListViewModel.Filters() {
37-
didSet {
38-
let contentIsNotSyncedYet = syncingCoordinator.highestPageBeingSynced ?? 0 == 0
39-
if filters != oldValue || contentIsNotSyncedYet {
40-
updateFilterButtonTitle()
41-
productsResultsController.updatePredicate(siteID: siteID,
42-
stockStatus: filters.stockStatus,
43-
productStatus: filters.productStatus,
44-
productType: filters.productType)
45-
updateProductsResultsController()
46-
syncingCoordinator.resynchronize {}
47-
}
48-
}
49-
}
36+
@Published var filters = FilterProductListViewModel.Filters()
5037

5138
/// Title of the filter button, should be updated with number of active filters.
5239
///
@@ -110,9 +97,9 @@ final class ProductSelectorViewModel: ObservableObject {
11097

11198
/// Predicate for the results controller.
11299
///
113-
private lazy var resultsPredicate: NSPredicate? = {
100+
private var resultsPredicate: NSPredicate? {
114101
productsResultsController.predicate
115-
}()
102+
}
116103

117104
/// Current search term entered by the user.
118105
/// Each update will trigger a remote product search and sync.
@@ -407,6 +394,22 @@ private extension ProductSelectorViewModel {
407394
}
408395
}
409396

397+
func updatePredicate(searchTerm: String, filters: FilterProductListViewModel.Filters) {
398+
productsResultsController.updatePredicate(siteID: siteID,
399+
stockStatus: filters.stockStatus,
400+
productStatus: filters.productStatus,
401+
productType: filters.productType)
402+
if searchTerm.isNotEmpty {
403+
// When the search query changes, also includes the original results predicate in addition to the search keyword.
404+
let searchResultsPredicate = NSPredicate(format: "ANY searchResults.keyword = %@", searchTerm)
405+
let subpredicates = [resultsPredicate, searchResultsPredicate].compactMap { $0 }
406+
productsResultsController.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: subpredicates)
407+
} else {
408+
// Resets the results to the full product list when there is no search query.
409+
productsResultsController.predicate = resultsPredicate
410+
}
411+
}
412+
410413
/// Setup: Syncing Coordinator
411414
///
412415
func configureSyncingCoordinator() {
@@ -428,28 +431,22 @@ private extension ProductSelectorViewModel {
428431
/// Updates the product results predicate & triggers a new sync when search term changes
429432
///
430433
func configureProductSearch() {
431-
$searchTerm
434+
let searchTermPublisher = $searchTerm
432435
.dropFirst() // Drop initial value
433436
.removeDuplicates()
434437
.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)
435-
.sink { [weak self] newSearchTerm in
438+
439+
searchTermPublisher.combineLatest($filters.removeDuplicates())
440+
.sink { [weak self] searchTerm, filters in
436441
guard let self = self else { return }
437-
438-
if newSearchTerm.isNotEmpty {
439-
// When the search query changes, also includes the original results predicate in addition to the search keyword.
440-
let searchResultsPredicate = NSPredicate(format: "ANY searchResults.keyword = %@", newSearchTerm)
441-
let subpredicates = [self.resultsPredicate, searchResultsPredicate].compactMap { $0 }
442-
self.productsResultsController.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: subpredicates)
443-
} else {
444-
// Resets the results to the full product list when there is no search query.
445-
self.productsResultsController.predicate = self.resultsPredicate
446-
}
447-
442+
self.updateFilterButtonTitle(with: filters)
443+
self.updatePredicate(searchTerm: searchTerm, filters: filters)
444+
self.updateProductsResultsController()
448445
self.syncingCoordinator.resynchronize()
449446
}.store(in: &subscriptions)
450447
}
451448

452-
func updateFilterButtonTitle() {
449+
func updateFilterButtonTitle(with filters: FilterProductListViewModel.Filters) {
453450
let activeFiltersCount = filters.numberOfActiveFilters
454451
if activeFiltersCount == 0 {
455452
filterButtonTitle = Localization.filterButtonWithoutActiveFilters

0 commit comments

Comments
 (0)