Skip to content

Commit

Permalink
Add nullable Country.default, add Discover.Options(country, proximity…
Browse files Browse the repository at this point in the history
…, 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
  • Loading branch information
aokj4ck committed Jan 31, 2024
1 parent ca72601 commit 949a0d8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Guide: https://keepachangelog.com/en/1.0.0/

<!-- Add changes for active work here -->

- [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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 949a0d8

Please sign in to comment.