From 949a0d82bff00bf5c85db8e34936d2baae5663d7 Mon Sep 17 00:00:00 2001 From: Jack Alto Date: Wed, 31 Jan 2024 14:54:12 -0500 Subject: [PATCH] Add nullable Country.default, add Discover.Options(country, proximity, origin) params - SSDK-399 - Add Country.default: Self? to auto-detect the current device region and provide a value if found - Add proximity and origin CLLocationCoordinate2D fields to Discover.Option and forward these to SearchOption parameters --- CHANGELOG.md | 1 + .../Common/Models/Country/Country.swift | 21 ++++++++++++++++++- .../Use Cases/Discover API/Discover.swift | 11 ++++++++-- .../Models/Discover+Options.swift | 17 ++++++++++++++- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41f04cd67..c2e7134ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Guide: https://keepachangelog.com/en/1.0.0/ +- [Discover] Add support for country, proximity, and origin parameters in Discover.Options search parameters. This fixes an issue when using search-along-route to query category results. - [Discover] Fix charging station category canonical ID - [SearchUI] Rename MapboxPanelController.Configuration to .PanelConfiguration. This disambiguates PanelConfiguration from the broader Configuration struct. - [Core] Update SwiftLint to 0.54.0 and SwiftFormat to 0.52.11 diff --git a/Sources/MapboxSearch/PublicAPI/Common/Models/Country/Country.swift b/Sources/MapboxSearch/PublicAPI/Common/Models/Country/Country.swift index 169fe908b..f28ef6856 100644 --- a/Sources/MapboxSearch/PublicAPI/Common/Models/Country/Country.swift +++ b/Sources/MapboxSearch/PublicAPI/Common/Models/Country/Country.swift @@ -3,7 +3,7 @@ import Foundation public struct Country: Equatable { public let countryCode: String - /// Country model initializier + /// Country model initializer /// - Parameter countryCode: Permitted values are ISO 3166-1 alpha 2 country codes (e.g. US, DE, GB) public init?(countryCode: String) { guard ISO3166_1_alpha2(rawValue: countryCode.uppercased()) != nil else { @@ -12,4 +12,23 @@ public struct Country: Equatable { self.countryCode = countryCode.lowercased() } + + init(code: ISO3166_1_alpha2) { + self.countryCode = code.rawValue.lowercased() + } + + /// Detect the system region ISO3166\_1\_Alpha2 identifier and return an instance for it + static var `default`: Self? { + if #available(iOS 16, *) { + return Country(countryCode: Locale.current.region?.identifier ?? "") + } else { + let regionComponents = Locale.current.identifier.components(separatedBy: "_") + if regionComponents.count >= 2 { + let countryIdentifier = regionComponents[1] + return Country(countryCode: countryIdentifier) + } else { + return nil + } + } + } } diff --git a/Sources/MapboxSearch/PublicAPI/Use Cases/Discover API/Discover.swift b/Sources/MapboxSearch/PublicAPI/Use Cases/Discover API/Discover.swift index 0e1818670..c5811927e 100644 --- a/Sources/MapboxSearch/PublicAPI/Use Cases/Discover API/Discover.swift +++ b/Sources/MapboxSearch/PublicAPI/Use Cases/Discover API/Discover.swift @@ -59,9 +59,11 @@ extension Discover { userActivityReporter.reportActivity(forComponent: "discover-search-nearby") let searchOptions = SearchOptions( + countries: [options.country?.countryCode].compactMap { $0 }, languages: [options.language.languageCode], limit: options.limit, - proximity: proximity + proximity: proximity, + origin: options.origin ) search(for: query, with: searchOptions, completion: completion) @@ -85,10 +87,12 @@ extension Discover { userActivityReporter.reportActivity(forComponent: "discover-search-in-area") let searchOptions = SearchOptions( + countries: [options.country?.countryCode].compactMap { $0 }, languages: [options.language.languageCode], limit: options.limit, proximity: proximity, - boundingBox: region + boundingBox: region, + origin: options.origin ) search(for: query, with: searchOptions, completion: completion) @@ -110,8 +114,11 @@ extension Discover { userActivityReporter.reportActivity(forComponent: "discover-search-along-the-route") let searchOptions = SearchOptions( + countries: [options.country?.countryCode].compactMap { $0 }, languages: [options.language.languageCode], limit: options.limit, + proximity: options.proximity, + origin: options.origin, routeOptions: route ) diff --git a/Sources/MapboxSearch/PublicAPI/Use Cases/Discover API/Models/Discover+Options.swift b/Sources/MapboxSearch/PublicAPI/Use Cases/Discover API/Models/Discover+Options.swift index bbaf21d9f..6721ee480 100644 --- a/Sources/MapboxSearch/PublicAPI/Use Cases/Discover API/Models/Discover+Options.swift +++ b/Sources/MapboxSearch/PublicAPI/Use Cases/Discover API/Models/Discover+Options.swift @@ -15,12 +15,27 @@ extension Discover { /// English (en) language parameter, but Frankreich (“France”) with a German (de) language parameter. public let language: Language + /// See ``MapboxSearch.Country.ISO3166_1_alpha2`` for the list of ISO 3166 alpha 2 country codes. + /// The default value will be selected from the Country.ISO3166\_1\_alpha2 identifiers based on the current + /// locale identifier or nil if no match is found. + public let country: Country? + + public let proximity: CLLocationCoordinate2D? + + public let origin: CLLocationCoordinate2D? + public init( limit: Int = 10, - language: Language? = nil + language: Language? = nil, + country: Country? = nil, + proximity: CLLocationCoordinate2D? = nil, + origin: CLLocationCoordinate2D? = nil ) { self.limit = limit self.language = language ?? .default + self.country = country ?? .default + self.proximity = proximity + self.origin = origin } } }