Skip to content

Commit

Permalink
Add distance formatter field to MapboxSearchUI.Configuration (#171)
Browse files Browse the repository at this point in the history
### 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
)
```
  • Loading branch information
aokj4ck committed Apr 4, 2024
1 parent 3fe0032 commit c5aa263
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Guide: https://keepachangelog.com/en/1.0.0/

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

- [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.
Expand Down
8 changes: 7 additions & 1 deletion Examples/SearchExamples/SimpleUISearchViewController.swift
Original file line number Diff line number Diff line change
@@ -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)
}()
Expand Down
4 changes: 2 additions & 2 deletions Sources/Demo/PlaceAutocompleteDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Demo/PlaceAutocompleteMainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion Sources/MapboxSearchUI/Configuration.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import MapKit
import UIKit

/// General structure to configure MapboxSearchController UI and logic
Expand All @@ -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
Expand All @@ -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?
}
7 changes: 6 additions & 1 deletion Sources/MapboxSearchUI/SearchSuggestionCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c5aa263

Please sign in to comment.