Skip to content

Commit a54d825

Browse files
authored
Merge pull request #6574 from woocommerce/issue/2506-search-orders-with-hashtag
Orders: Support searching for orders with # prefix
2 parents b2828e5 + d1c9202 commit a54d825

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ final class OrderSearchUICommand: SearchUICommand {
7979

8080
viewController.navigationController?.pushViewController(detailsViewController, animated: true)
8181
}
82+
83+
/// Removes the `#` from the start of the search keyword, if present.
84+
///
85+
/// This allows searching for an order with `#123` and getting the results for order `123`.
86+
/// See https://github.com/woocommerce/woocommerce-ios/issues/2506
87+
///
88+
func sanitizeKeyword(_ keyword: String) -> String {
89+
if keyword.starts(with: "#") {
90+
return keyword.removing(at: keyword.startIndex)
91+
}
92+
return keyword
93+
}
8294
}
8395

8496
private extension OrderSearchUICommand {

WooCommerce/Classes/ViewRelated/Search/SearchUICommand.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,24 @@ protocol SearchUICommand {
8484

8585
/// The Accessibility Identifier for the cancel button
8686
var cancelButtonAccessibilityIdentifier: String { get }
87+
88+
/// Optionally sanitizes the search keyword.
89+
///
90+
/// - Parameter keyword: user-entered search keyword.
91+
/// - Returns: sanitized search keyword.
92+
func sanitizeKeyword(_ keyword: String) -> String
8793
}
8894

8995
// MARK: - Default implementation
9096
extension SearchUICommand {
9197
func configureActionButton(_ button: UIButton, onDismiss: @escaping () -> Void) {
9298
// If not implemented, keeps the default cancel UI/UX
9399
}
100+
101+
func sanitizeKeyword(_ keyword: String) -> String {
102+
// If not implemented, returns the keyword as entered
103+
return keyword
104+
}
94105
}
95106

96107
// MARK: - SearchUICommand using EmptySearchResultsViewController

WooCommerce/Classes/ViewRelated/Search/SearchViewController.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,16 @@ extension SearchViewController: SyncingCoordinatorDelegate {
362362
/// Synchronizes the models for the Default Store (if any).
363363
///
364364
func sync(pageNumber: Int, pageSize: Int, reason: String?, onCompletion: ((Bool) -> Void)? = nil) {
365-
let keyword = self.keyword
365+
let keyword = searchUICommand.sanitizeKeyword(self.keyword)
366366
searchUICommand.synchronizeModels(siteID: storeID,
367367
keyword: keyword,
368368
pageNumber: pageNumber,
369369
pageSize: pageSize,
370-
onCompletion: { [weak self] isCompleted in
370+
onCompletion: { [weak self] isCompleted in
371+
guard let self = self else { return }
371372
// Disregard OPs that don't really match the latest keyword
372-
if keyword == self?.keyword {
373-
self?.transitionToResultsUpdatedState()
373+
if keyword == self.searchUICommand.sanitizeKeyword(self.keyword) {
374+
self.transitionToResultsUpdatedState()
374375
}
375376
onCompletion?(isCompleted)
376377
})
@@ -387,6 +388,7 @@ private extension SearchViewController {
387388
///
388389
func synchronizeSearchResults(with keyword: String) {
389390
// When the search query changes, also includes the original results predicate in addition to the search keyword.
391+
let keyword = searchUICommand.sanitizeKeyword(keyword)
390392
let searchResultsPredicate = NSPredicate(format: "ANY searchResults.keyword = %@", keyword)
391393
let subpredicates = [resultsPredicate].compactMap { $0 } + [searchResultsPredicate]
392394
resultsController.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: subpredicates)

0 commit comments

Comments
 (0)