diff --git a/CHANGELOG.md b/CHANGELOG.md index 584f00654..e20e5374e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Guide: https://keepachangelog.com/en/1.0.0/ +- [SearchUI] Add `distanceFormatter` field to Configuration to support changing the search suggestions distance format. Nil values will use the default behavior. + - [Core] Add xcprivacy for MapboxSearch and MapboxSearchUI - [SearchUI] Update Maki icons to all SVG, latest versions from https://github.com/mapbox/maki diff --git a/Examples/SearchExamples/SimpleUISearchViewController.swift b/Examples/SearchExamples/SimpleUISearchViewController.swift index 9f7366353..c1b03ce83 100644 --- a/Examples/SearchExamples/SimpleUISearchViewController.swift +++ b/Examples/SearchExamples/SimpleUISearchViewController.swift @@ -1,12 +1,18 @@ import MapboxMaps import MapboxSearch import MapboxSearchUI +import MapKit import UIKit class SimpleUISearchViewController: MapsViewController { lazy var searchController: MapboxSearchController = { let locationProvider = PointLocationProvider(coordinate: .sanFrancisco) - var configuration = Configuration(locationProvider: locationProvider) + let formatter = MKDistanceFormatter() + formatter.unitStyle = .abbreviated + var configuration = Configuration( + locationProvider: locationProvider, + distanceFormatter: formatter + ) return MapboxSearchController(configuration: configuration) }() diff --git a/Sources/Demo/PlaceAutocompleteDetailsViewController.swift b/Sources/Demo/PlaceAutocompleteDetailsViewController.swift index abe69b313..4633deec7 100644 --- a/Sources/Demo/PlaceAutocompleteDetailsViewController.swift +++ b/Sources/Demo/PlaceAutocompleteDetailsViewController.swift @@ -101,7 +101,7 @@ extension PlaceAutocompleteResultViewController { } extension PlaceAutocomplete.Result { - static let measurumentFormatter: MeasurementFormatter = { + static let measurementFormatter: MeasurementFormatter = { let formatter = MeasurementFormatter() formatter.unitOptions = [.naturalScale] formatter.numberFormatter.roundingMode = .halfUp @@ -137,7 +137,7 @@ extension PlaceAutocomplete.Result { components.append( ( name: "Estimated time", - value: PlaceAutocomplete.Result.measurumentFormatter.string(from: estimatedTime) + value: PlaceAutocomplete.Result.measurementFormatter.string(from: estimatedTime) ) ) } diff --git a/Sources/Demo/PlaceAutocompleteMainViewController.swift b/Sources/Demo/PlaceAutocompleteMainViewController.swift index e107299c0..1c40039ba 100644 --- a/Sources/Demo/PlaceAutocompleteMainViewController.swift +++ b/Sources/Demo/PlaceAutocompleteMainViewController.swift @@ -79,7 +79,7 @@ extension PlaceAutocompleteMainViewController: UITableViewDataSource, UITableVie description += "\n\(PlaceAutocomplete.Result.distanceFormatter.string(fromDistance: distance))" } if let estimatedTime = suggestion.estimatedTime { - description += "\n\(PlaceAutocomplete.Result.measurumentFormatter.string(from: estimatedTime))" + description += "\n\(PlaceAutocomplete.Result.measurementFormatter.string(from: estimatedTime))" } tableViewCell.detailTextLabel?.text = description diff --git a/Sources/MapboxSearchUI/Configuration.swift b/Sources/MapboxSearchUI/Configuration.swift index 6cfb70409..caf03a679 100644 --- a/Sources/MapboxSearchUI/Configuration.swift +++ b/Sources/MapboxSearchUI/Configuration.swift @@ -1,4 +1,5 @@ import Foundation +import MapKit import UIKit /// General structure to configure MapboxSearchController UI and logic @@ -16,12 +17,14 @@ public struct Configuration { categoryDataProvider: CategoryDataProvider = DefaultCategoryDataProvider(), locationProvider: LocationProvider? = DefaultLocationProvider(), hideCategorySlots: Bool = false, - style: Style = .default + style: Style = .default, + distanceFormatter: MKDistanceFormatter? = nil ) { self.allowsFeedbackUI = allowsFeedbackUI self.categoryDataProvider = categoryDataProvider self.locationProvider = locationProvider self.hideCategorySlots = hideCategorySlots + self.distanceFormatter = distanceFormatter } /// Allow to show feedback related UI @@ -44,4 +47,9 @@ public struct Configuration { /// Style to be used for Search UI elements. /// It's possible to change style on the fly. Non-animatable. public var style = Style() + + /// Override the default ``MKDistanceFormatter`` behavior used by ``SearchSuggestionCell`` to display search + /// results in a specific unit system. A nil value will use the ``MKDistanceFormatter`` system behavior to infer + /// the unit system based on the device locale. + public var distanceFormatter: MKDistanceFormatter? } diff --git a/Sources/MapboxSearchUI/SearchSuggestionCell.swift b/Sources/MapboxSearchUI/SearchSuggestionCell.swift index d9021989a..75e3865a3 100644 --- a/Sources/MapboxSearchUI/SearchSuggestionCell.swift +++ b/Sources/MapboxSearchUI/SearchSuggestionCell.swift @@ -95,7 +95,12 @@ class SearchSuggestionCell: UITableViewCell { ) == .orderedSame populateSuggestionButton.isHidden = resultNameSameAsQuery - if let distanceString = suggestion.distance.map(SearchSuggestionCell.distanceFormatter.string) { + if let distanceFormatter = configuration.distanceFormatter, + let distanceString = suggestion.distance.map(distanceFormatter.string) + { + distanceLabel.text = distanceString + distanceLabel.isHidden = false + } else if let distanceString = suggestion.distance.map(SearchSuggestionCell.distanceFormatter.string) { distanceLabel.text = distanceString distanceLabel.isHidden = false } else {