Skip to content

Commit a35d044

Browse files
committed
Sanitize the search keyword just before searching
1 parent 70fc3e0 commit a35d044

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ final class OrderSearchUICommand: SearchUICommand {
8585
/// This allows searching for an order with `#123` and getting the results for order `123`.
8686
/// See https://github.com/woocommerce/woocommerce-ios/issues/2506
8787
///
88-
func sanitize(_ keyword: String) -> String {
88+
func sanitizeKeyword(_ keyword: String) -> String {
8989
guard keyword.starts(with: "#") else {
9090
return keyword
9191
}

WooCommerce/Classes/ViewRelated/Search/SearchViewController.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where Cell.SearchModel == Command.CellViewModel {
7070
/// Returns the active Keyword
7171
///
7272
private var keyword: String {
73-
return searchUICommand.sanitizeKeyword(searchBar.text ?? String())
73+
return searchBar.text ?? String()
7474
}
7575

7676
/// UI Active State
@@ -205,8 +205,7 @@ where Cell.SearchModel == Command.CellViewModel {
205205
// MARK: - UISearchBarDelegate Conformance
206206
//
207207
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
208-
let sanitizedSearchText = searchUICommand.sanitizeKeyword(searchText)
209-
synchronizeSearchResults(with: sanitizedSearchText)
208+
synchronizeSearchResults(with: searchText)
210209
}
211210

212211
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
@@ -363,14 +362,14 @@ extension SearchViewController: SyncingCoordinatorDelegate {
363362
/// Synchronizes the models for the Default Store (if any).
364363
///
365364
func sync(pageNumber: Int, pageSize: Int, reason: String?, onCompletion: ((Bool) -> Void)? = nil) {
366-
let keyword = self.keyword
365+
let keyword = searchUICommand.sanitizeKeyword(self.keyword)
367366
searchUICommand.synchronizeModels(siteID: storeID,
368367
keyword: keyword,
369368
pageNumber: pageNumber,
370369
pageSize: pageSize,
371-
onCompletion: { [weak self] isCompleted in
370+
onCompletion: { [weak self] isCompleted in
372371
// Disregard OPs that don't really match the latest keyword
373-
if keyword == self?.keyword {
372+
if keyword == self?.searchUICommand.sanitizeKeyword(self?.keyword ?? String()) {
374373
self?.transitionToResultsUpdatedState()
375374
}
376375
onCompletion?(isCompleted)
@@ -388,6 +387,7 @@ private extension SearchViewController {
388387
///
389388
func synchronizeSearchResults(with keyword: String) {
390389
// When the search query changes, also includes the original results predicate in addition to the search keyword.
390+
let keyword = searchUICommand.sanitizeKeyword(keyword)
391391
let searchResultsPredicate = NSPredicate(format: "ANY searchResults.keyword = %@", keyword)
392392
let subpredicates = [resultsPredicate].compactMap { $0 } + [searchResultsPredicate]
393393
resultsController.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: subpredicates)

0 commit comments

Comments
 (0)