Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add distance formatter field to MapboxSearchUI.Configuration #171

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

- [SearchUI] Update Maki icons to all SVG, latest versions from https://github.com/mapbox/maki
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}()
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