Skip to content

Commit

Permalink
Add mapboxId to SearchResult and derived types (#216)
Browse files Browse the repository at this point in the history
### Description

- Add `mapboxId: String?` to SearchResult
- Available in responses when engine's apiType == .searchBox
- Follow-up to #150

### Checklist
- [X] Update `CHANGELOG`
  • Loading branch information
aokj4ck authored Apr 19, 2024
1 parent fa0a370 commit b30ea74
Show file tree
Hide file tree
Showing 30 changed files with 86 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ Guide: https://keepachangelog.com/en/1.0.0/

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

- [SearchResult] Add support for `mapboxId` field when availalbe.
- [FavoriteRecord] Add support for `mapboxId` field when availalbe.
- [HistoryRecord] Add support for `mapboxId` field when availalbe.
- [Discover] Add more complete support for `mapboxId` field in Result subtype when availalbe.
- [Address Autofill] Add more complete support for `mapboxId` field in Result and Suggestion subtypes when availalbe.
- [Place Autocomplete] Add more complete support for `mapboxId` field in Result and Suggestion subtypes when availalbe.
- [Demo] Add `mapboxId` table view cell to PlaceAutocomplete detail view controller when available.

- [Offline] Remove `CoreOfflineIndexChangeEventType` extension previously used for development.
- [Core] Remove usages of `@_implementationOnly import` due to compilation issue.

Expand Down
6 changes: 6 additions & 0 deletions Sources/Demo/PlaceAutocompleteDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ extension PlaceAutocomplete.Result {
)
}

if let mapboxId {
components.append(
(name: "Mapbox ID", value: mapboxId)
)
}

return components
}
}
6 changes: 6 additions & 0 deletions Sources/MapboxSearch/PublicAPI/FavoriteRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public struct FavoriteRecord: IndexableRecord, SearchResult, Codable, Equatable
/// Displayable name of the record.
public var name: String

/// A unique identifier for the geographic feature
public var mapboxId: String?

/**
The feature name, as matched by the search algorithm.

Expand Down Expand Up @@ -79,6 +82,7 @@ public struct FavoriteRecord: IndexableRecord, SearchResult, Codable, Equatable
/// - resultType: Favorite result type
public init(
id: String? = nil,
mapboxId: String? = nil,
name: String,
matchingName: String?,
coordinate: CLLocationCoordinate2D,
Expand All @@ -93,6 +97,7 @@ public struct FavoriteRecord: IndexableRecord, SearchResult, Codable, Equatable
metadata: SearchResultMetadata? = nil
) {
self.id = id ?? UUID().uuidString
self.mapboxId = mapboxId
self.name = name
self.matchingName = matchingName
self.coordinateCodable = .init(coordinate)
Expand All @@ -118,6 +123,7 @@ public struct FavoriteRecord: IndexableRecord, SearchResult, Codable, Equatable
) {
self.init(
id: id,
mapboxId: searchResult.mapboxId,
name: name,
matchingName: searchResult.matchingName,
coordinate: searchResult.coordinate,
Expand Down
5 changes: 5 additions & 0 deletions Sources/MapboxSearch/PublicAPI/HistoryRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public struct HistoryRecord: IndexableRecord, SearchResult, Codable, Hashable {
/// Unique identifier
public private(set) var id: String

public private(set) var mapboxId: String?

/// Record's name
public private(set) var name: String

Expand Down Expand Up @@ -97,6 +99,7 @@ public struct HistoryRecord: IndexableRecord, SearchResult, Codable, Hashable {
/// - routablePoints: Coordinates of building entries
public init(
id: String = UUID().uuidString,
mapboxId: String?,
name: String,
matchingName: String?,
serverIndex: Int?,
Expand All @@ -112,6 +115,7 @@ public struct HistoryRecord: IndexableRecord, SearchResult, Codable, Hashable {
routablePoints: [RoutablePoint]? = nil
) {
self.id = id
self.mapboxId = mapboxId
self.name = name
self.matchingName = matchingName
self.serverIndex = serverIndex
Expand All @@ -138,6 +142,7 @@ public struct HistoryRecord: IndexableRecord, SearchResult, Codable, Hashable {
timestamp: Date = Date()
) {
self.id = searchResult.id
self.mapboxId = searchResult.mapboxId
self.name = searchResult.name
self.matchingName = searchResult.matchingName
self.serverIndex = searchResult.serverIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import CoreLocation
import Foundation

class ExternalRecordPlaceholder: SearchResultSuggestion, CoreResponseProvider {
var mapboxId: String?

var originalResponse: CoreSearchResultResponse

var dataLayerIdentifier: String

var id: String

var mapboxId: String?

var name: String

var address: Address?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import CoreLocation
import Foundation

class SearchCategorySuggestionImpl: SearchCategorySuggestion, CoreResponseProvider {
var mapboxId: String?

var originalResponse: CoreSearchResultResponse

var id: String

var mapboxId: String?

var name: String

var address: Address?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public protocol SearchResult {
/// Result name.
var name: String { get }

/// A unique identifier for the geographic feature
var mapboxId: String? { get }

/// Icon name according to [Mapbox Maki icon set](https://github.com/mapbox/maki/)
var iconName: String? { get }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import CoreLocation
import Foundation

class SearchResultSuggestionImpl: SearchResultSuggestion, CoreResponseProvider {
var mapboxId: String?

var originalResponse: CoreSearchResultResponse

let dataLayerIdentifier = SearchEngine.providerIdentifier

var id: String

var mapboxId: String?

var name: String

var address: Address?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public protocol SearchSuggestion {
/// - Attention: Mapbox backend may change the identifier of the object in the future.
var id: String { get }

/// A unique identifier for the geographic feature
var mapboxId: String? { get }

/// Suggestion name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import CoreLocation
import Foundation

class ServerSearchResult: SearchResult, SearchResultSuggestion, CoreResponseProvider {
var mapboxId: String?

var distance: CLLocationDistance?

var originalResponse: CoreSearchResultResponse
Expand Down Expand Up @@ -37,6 +35,8 @@ class ServerSearchResult: SearchResult, SearchResultSuggestion, CoreResponseProv

var id: String

var mapboxId: String?

var name: String

var matchingName: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ extension AddressAutofill {
}
let result = AddressAutofill.Result(
name: suggestion.name,
mapboxId: suggestion.mapboxId,
formattedAddress: suggestion.formattedAddress,
coordinate: coordinate,
addressComponents: suggestion.addressComponents
Expand Down Expand Up @@ -248,6 +249,7 @@ extension AddressAutofill {

return Suggestion(
name: name,
mapboxId: result.mapboxId,
formattedAddress: fullAddress,
coordinate: result.center?.value,
addressComponents: resultAddress,
Expand Down Expand Up @@ -287,6 +289,7 @@ extension AddressAutofill {

let autofillResult = AddressAutofill.Result(
name: result.name,
mapboxId: result.mapboxId,
formattedAddress: formattedAddress,
coordinate: result.coordinate,
addressComponents: addressComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ extension AddressAutofill {
/// Result name.
public let name: String

/// A unique identifier for the geographic feature
public let mapboxId: String?

/// Textual representation of the address.
public let formattedAddress: String

Expand All @@ -16,11 +19,13 @@ extension AddressAutofill {

init(
name: String,
mapboxId: String?,
formattedAddress: String,
coordinate: CLLocationCoordinate2D,
addressComponents: NonEmptyArray<AddressComponent>
) {
self.name = name
self.mapboxId = mapboxId
self.formattedAddress = formattedAddress
self.coordinate = coordinate
self.addressComponents = addressComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extension AddressAutofill {
/// Suggestion name.
public let name: String

/// A unique identifier for the geographic feature
public let mapboxId: String?

/// Textual representation of the address.
public let formattedAddress: String

Expand All @@ -21,12 +24,14 @@ extension AddressAutofill {

init(
name: String,
mapboxId: String?,
formattedAddress: String,
coordinate: CLLocationCoordinate2D?,
addressComponents: NonEmptyArray<AddressComponent>,
underlying: Underlying
) {
self.name = name
self.mapboxId = mapboxId
self.formattedAddress = formattedAddress
self.coordinate = coordinate
self.addressComponents = addressComponents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extension AddressAutofill.Suggestion {

return try .init(
name: searchResult.name,
mapboxId: searchResult.mapboxId,
formattedAddress: formattedAddress,
coordinate: searchResult.coordinate,
addressComponents: address.toAutofillComponents(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extension Discover {
/// Result's name
public let name: String

/// A unique identifier for the geographic feature
public let mapboxId: String?

/// Result's address
public let address: AddressComponents

Expand Down Expand Up @@ -34,6 +37,7 @@ extension Discover.Result {

return .init(
name: searchResult.name,
mapboxId: searchResult.mapboxId,
address: .init(searchResult: searchResult),
coordinate: searchResult.coordinate,
routablePoints: routablePointsArray,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ extension PlaceAutocomplete {
/// Result name.
public let name: String

/// A unique identifier for the geographic feature
public let mapboxId: String?

/// Contains formatted address.
public let description: String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extension PlaceAutocomplete {
/// Place's name.
public let name: String

/// A unique identifier for the geographic feature
public let mapboxId: String?

/// Contains formatted address.
public let description: String?

Expand Down Expand Up @@ -36,6 +39,7 @@ extension PlaceAutocomplete {

init(
name: String,
mapboxId: String?,
description: String?,
coordinate: CLLocationCoordinate2D?,
iconName: String?,
Expand All @@ -47,6 +51,7 @@ extension PlaceAutocomplete {
underlying: Underlying
) {
self.name = name
self.mapboxId = mapboxId
self.description = description
self.coordinate = coordinate
self.iconName = iconName
Expand All @@ -69,6 +74,7 @@ extension PlaceAutocomplete.Suggestion {
func result(for underlyingResult: SearchResult) -> PlaceAutocomplete.Result {
.init(
name: name,
mapboxId: underlyingResult.mapboxId,
description: description,
type: placeType,
coordinate: coordinate,
Expand Down Expand Up @@ -108,6 +114,7 @@ extension PlaceAutocomplete.Suggestion {

return .init(
name: searchResult.name,
mapboxId: searchResult.mapboxId,
description: searchResult.descriptionText,
coordinate: searchResult.coordinate,
iconName: searchResult.iconName,
Expand Down Expand Up @@ -136,6 +143,7 @@ extension PlaceAutocomplete.Suggestion {

return .init(
name: searchSuggestion.names.first ?? "",
mapboxId: searchSuggestion.mapboxId,
description: searchSuggestion.addressDescription,
coordinate: coordinate,
iconName: searchSuggestion.icon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ extension PlaceAutocomplete {

return Suggestion(
name: name,
mapboxId: result.mapboxId,
description: result.addressDescription,
coordinate: result.center?.value,
iconName: result.icon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class AddressAutofillIntegrationTests: MockServerIntegrationTestCase<Autof
case .success(let suggestions):
XCTAssertEqual(suggestions.count, 8)
suggestion = suggestions.first
suggestion = suggestions[0]
XCTAssertNotNil(suggestion?.mapboxId)
case .failure:
XCTFail("Should return success")
}
Expand Down Expand Up @@ -76,6 +76,7 @@ final class AddressAutofillIntegrationTests: MockServerIntegrationTestCase<Autof
CLLocationCoordinate2D(latitude: 37.784592, longitude: -122.434671)
)
XCTAssertEqual(resolvedSuggestion.addressComponents, expectedAddressComponents)
XCTAssertNotNil(resolvedSuggestion.mapboxId)
case .failure:
XCTFail("Should return success")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class DiscoverIntegrationTests: MockServerIntegrationTestCase<SBSMockResponse> {
switch result {
case .success(let searchResults):
XCTAssertFalse(searchResults.isEmpty)
let firstResult = searchResults.first
XCTAssertNotNil(firstResult?.mapboxId)
expectation.fulfill()
case .failure:
XCTFail("Error not expected")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ final class PlaceAutocompleteIntegrationTests: MockServerIntegrationTestCase<SBS
resolvedSuggestion.coordinate,
CLLocationCoordinate2D(latitude: 37.7648, longitude: -122.463)
)
XCTAssertNil(resolvedSuggestion.mapboxId)
case .failure:
XCTFail("Should return success")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MapboxSearch
extension SearchResultStub {
static let sample1 = SearchResultStub(
id: "sample-1",
mapboxId: UUID().uuidString, // not actually UUID format but useful for tests
categories: ["sample-1", "sample-2"],
name: "Sample No 1",
matchingName: nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class SearchResultTests: XCTestCase {
func testPlacemarkGeneration() throws {
let resultStub = SearchResultStub(
id: "unit-test-random",
mapboxId: nil,
name: "Unit Test",
matchingName: nil,
serverIndex: nil,
Expand Down
Loading

0 comments on commit b30ea74

Please sign in to comment.