Skip to content

Commit

Permalink
Merge pull request wordpress-mobile#11290 from wordpress-mobile/issue…
Browse files Browse the repository at this point in the history
…/10530-domain-search-match

Domains Search: Filter an exact match to the top of the list
  • Loading branch information
frosty authored Mar 18, 2019
2 parents effd274 + 8c8c9d8 commit e262c83
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Redesigned Notices
* Changed offline error messages to be less disruptive.
* Resolved a defect in the new Site Creation flow where the site preview address bar could be edited.
* Made it easier to find a domain for your new site, by moving the best match to the top of the search results.

11.9
------
Expand Down
17 changes: 16 additions & 1 deletion WordPress/Classes/Services/DomainsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,27 @@ struct DomainsService {
remote.getDomainSuggestions(base: base,
domainSuggestionType: domainSuggestionType,
success: { suggestions in
success(suggestions)
let sorted = self.sortedSuggestions(suggestions, forBase: base)
success(sorted)
}) { error in
failure(error)
}
}

// If any of the suggestions matches the base exactly,
// then sort that suggestion up to the top of the list.
fileprivate func sortedSuggestions(_ suggestions: [DomainSuggestion], forBase base: String) -> [DomainSuggestion] {
let normalizedBase = base.lowercased().replacingMatches(of: " ", with: "")

var filteredSuggestions = suggestions
if let matchedSuggestionIndex = suggestions.firstIndex(where: { $0.subdomain == base || $0.subdomain == normalizedBase }) {
let matchedSuggestion = filteredSuggestions.remove(at: matchedSuggestionIndex)
filteredSuggestions.insert(matchedSuggestion, at: 0)
}

return filteredSuggestions
}

fileprivate func mergeDomains(_ domains: [Domain], forSite siteID: Int) {
let remoteDomains = domains
let localDomains = domainsForSite(siteID)
Expand Down

0 comments on commit e262c83

Please sign in to comment.