From c5aa263f55ef99ca06afbca2459d204aa33c71f7 Mon Sep 17 00:00:00 2001 From: Jack Alto <384288+aokj4ck@users.noreply.github.com> Date: Tue, 13 Feb 2024 13:21:10 -0500 Subject: [PATCH] Add distance formatter field to MapboxSearchUI.Configuration (#171) ### Description Fixes #168 - Add an optional [MKDistanceFormatter](https://developer.apple.com/documentation/mapkit/mkdistanceformatter) parameter to Configuration that will be used in the suggestion results to format the distance. - If no value is provided then the default system behavior of auto-detecting the device locale for the formatter style will be used. - Update the Examples/SearchExample.xcworkspace project with sample code ### Checklist - [x] Update `CHANGELOG` ### Screenshots | With nil value | With a custom formatter in locale en_US | | -- | -- | | ![Simulator Screen Shot - iPhone 13 - 2024-02-07 at 16 46 34](https://github.com/mapbox/mapbox-search-ios/assets/384288/f65b3f70-7fc2-4214-b369-61b5f570193e) | ![Simulator Screen Shot - iPhone 13 - 2024-02-07 at 16 46 54](https://github.com/mapbox/mapbox-search-ios/assets/384288/a8054483-d1fa-4814-860f-6354cad660a7) | Listing for "With a nil value" screenshot in SimpleUISearchViewController ```swift var configuration = Configuration() ``` Listing for "With a custom formatter in locale en_US" screenshot in SimpleUISearchViewController ```swift let formatter = MKDistanceFormatter() formatter.units = .metric formatter.unitStyle = .full var configuration = Configuration( distanceFormatter: formatter ) ``` --- CHANGELOG.md | 2 ++ .../SearchExamples/SimpleUISearchViewController.swift | 8 +++++++- .../Demo/PlaceAutocompleteDetailsViewController.swift | 4 ++-- Sources/Demo/PlaceAutocompleteMainViewController.swift | 2 +- Sources/MapboxSearchUI/Configuration.swift | 10 +++++++++- Sources/MapboxSearchUI/SearchSuggestionCell.swift | 7 ++++++- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b6179349..71787bc6b 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 - [Unit Tests] Update and correct tests for iOS 17 using all mocked data. diff --git a/Examples/SearchExamples/SimpleUISearchViewController.swift b/Examples/SearchExamples/SimpleUISearchViewController.swift index 7d1b6a59f..e815c3510 100644 --- a/Examples/SearchExamples/SimpleUISearchViewController.swift +++ b/Examples/SearchExamples/SimpleUISearchViewController.swift @@ -1,11 +1,17 @@ import MapboxMaps 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 {