Skip to content

Commit ae9be7d

Browse files
committed
Remove the search results predicate when the keyword is empty.
1 parent 9f51030 commit ae9be7d

File tree

6 files changed

+10
-7
lines changed

6 files changed

+10
-7
lines changed

WooCommerce/Classes/ViewRelated/Coupons/CouponSearchUICommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ final class CouponSearchUICommand: SearchUICommand {
8383
viewController.configure(.simple(message: message, image: .emptySearchResultsImage))
8484
}
8585

86-
func searchResultsPredicate(keyword: String) -> NSPredicate {
86+
func searchResultsPredicate(keyword: String) -> NSPredicate? {
8787
NSPredicate(format: "ANY searchResults.keyword = %@", keyword)
8888
}
8989
}

WooCommerce/Classes/ViewRelated/Products/Edit Product/Linked Products List Selector/ProductListSelector/ProductListMultiSelectorSearchUICommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ final class ProductListMultiSelectorSearchUICommand: NSObject, SearchUICommand {
9393
updateActionButton()
9494
}
9595

96-
func searchResultsPredicate(keyword: String) -> NSPredicate {
96+
func searchResultsPredicate(keyword: String) -> NSPredicate? {
9797
NSPredicate(format: "ANY searchResults.keyword = %@", keyword)
9898
}
9999
}

WooCommerce/Classes/ViewRelated/Search/Order/OrderSearchUICommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ final class OrderSearchUICommand: SearchUICommand {
9494
return keyword
9595
}
9696

97-
func searchResultsPredicate(keyword: String) -> NSPredicate {
97+
func searchResultsPredicate(keyword: String) -> NSPredicate? {
9898
NSPredicate(format: "ANY searchResults.keyword = %@", keyword)
9999
}
100100
}

WooCommerce/Classes/ViewRelated/Search/Product/ProductSearchUICommand.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ final class ProductSearchUICommand: SearchUICommand {
121121
}
122122
}
123123

124-
func searchResultsPredicate(keyword: String) -> NSPredicate {
124+
func searchResultsPredicate(keyword: String) -> NSPredicate? {
125+
guard keyword.isNotEmpty else {
126+
return nil
127+
}
125128
guard isSearchProductsBySKUEnabled else {
126129
return NSPredicate(format: "ANY searchResults.keyword = %@", keyword)
127130
}

WooCommerce/Classes/ViewRelated/Search/SearchUICommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ protocol SearchUICommand {
101101
/// The predicate to fetch product search results based on the keyword.
102102
/// Called when a search API request is made for the keyword.
103103
/// - Parameter keyword: search query.
104-
/// - Returns: predicate that is based on the search keyword.
105-
func searchResultsPredicate(keyword: String) -> NSPredicate
104+
/// - Returns: predicate that is based on the search keyword. When the keyword is empty, `nil` can be returned as an example use case.
105+
func searchResultsPredicate(keyword: String) -> NSPredicate?
106106
}
107107

108108
// MARK: - Default implementation

WooCommerce/Classes/ViewRelated/Search/SearchViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ private extension SearchViewController {
431431
// When the search query changes, also includes the original results predicate in addition to the search keyword.
432432
let keyword = searchUICommand.sanitizeKeyword(keyword)
433433
let searchResultsPredicate = searchUICommand.searchResultsPredicate(keyword: keyword)
434-
let subpredicates = [resultsPredicate].compactMap { $0 } + [searchResultsPredicate]
434+
let subpredicates = [resultsPredicate].compactMap { $0 } + [searchResultsPredicate].compactMap { $0 }
435435
resultsController.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: subpredicates)
436436

437437
tableView.setContentOffset(.zero, animated: false)

0 commit comments

Comments
 (0)