From b1cce85a3c6a9fc5c8afe8e7aef33912c8a5a778 Mon Sep 17 00:00:00 2001 From: Reto Lehnherr Date: Tue, 30 Apr 2024 15:40:37 +0200 Subject: [PATCH] Follow up breaking changes from SDK --- .../LocationSearchByNameView.swift | 29 ++++++++----------- SampleApp/OJPSampleApp/PlaceDetailView.swift | 9 ++++-- Sources/OJP/APIConfiguration.swift | 4 +-- Sources/OJP/Models/OJPv2+Extensions.swift | 8 ++--- Sources/OJP/Models/OJPv2.swift | 6 ++++ Sources/OJP/OJP.swift | 2 +- Tests/OJPTests/OjpSDKTests.swift | 4 +-- 7 files changed, 32 insertions(+), 30 deletions(-) diff --git a/SampleApp/OJPSampleApp/LocationSearchByNameView.swift b/SampleApp/OJPSampleApp/LocationSearchByNameView.swift index b7ded21..50a3602 100644 --- a/SampleApp/OJPSampleApp/LocationSearchByNameView.swift +++ b/SampleApp/OJPSampleApp/LocationSearchByNameView.swift @@ -16,12 +16,6 @@ struct LocationResult: Identifiable { var coordinates: CLLocationCoordinate2D } -extension OJPv2.PlaceResult: Identifiable { - public var id: String { - self.place.stopPlace.privateCode.value - } -} - struct LocationSearchByNameView: View { @State var inputName: String = "" @State var results: [OJPv2.PlaceResult] = [] @@ -37,23 +31,24 @@ struct LocationSearchByNameView: View { Text("Search Stations by Name") Form { TextField("Search Name", text: $inputName) - Picker(selection: $limit) { - ForEach(availableRange, id: \.self) { - Text("\($0)").tag($0) - } - } label: { - Text("Limit") - } + // can't define number of results any more +// Picker(selection: $limit) { +// ForEach(availableRange, id: \.self) { +// Text("\($0)").tag($0) +// } +// } label: { +// Text("Limit") +// } } List($results) { $stop in - Text(stop.place.stopPlace.stopPlaceName.text) .onTapGesture { + Text(stop.place.stopPlace?.stopPlaceName.text ?? "No Stop Place") .onTapGesture { self.selectetedPlace = stop } } Map { ForEach($results) { $stop in - Annotation(stop.place.stopPlace.stopPlaceName.text, - coordinate: stop.place.geoPosition.coordinates) { + Annotation(stop.place.stopPlace?.stopPlaceName.text ?? "No Stop Place", + coordinate: stop.place.geoPosition?.coordinates ?? CLLocationCoordinate2D(latitude: 0, longitude: 0)) { Circle().onTapGesture { self.selectetedPlace = stop } @@ -72,7 +67,7 @@ struct LocationSearchByNameView: View { let ojp = OJP(loadingStrategy: .http(.int)) let t = Task { do { - results = try await ojp.stations(by: newValue, limit: limit) + results = try await ojp.requestLocations(from: inputName) print(results) } catch { print(error) diff --git a/SampleApp/OJPSampleApp/PlaceDetailView.swift b/SampleApp/OJPSampleApp/PlaceDetailView.swift index 8332f36..b522e1b 100644 --- a/SampleApp/OJPSampleApp/PlaceDetailView.swift +++ b/SampleApp/OJPSampleApp/PlaceDetailView.swift @@ -16,9 +16,12 @@ struct PlaceDetailView: View { if let place { List { Text("Place").font(.headline) - Text("Name: \(place.place.name.text)") - Text("GeoPosition: (\(place.place.geoPosition.latitude), \(place.place.geoPosition.longitude))") - + Text("Name: \(place.place.name?.text ?? "")") + if let geoPosition = place.place.geoPosition { + Text("GeoPosition: (\(geoPosition.latitude), \(geoPosition.longitude))") + } else { + Text("⚠️ No Geopostion") + } } .cornerRadius(10.0) } diff --git a/Sources/OJP/APIConfiguration.swift b/Sources/OJP/APIConfiguration.swift index 739a832..a693593 100644 --- a/Sources/OJP/APIConfiguration.swift +++ b/Sources/OJP/APIConfiguration.swift @@ -29,9 +29,9 @@ public struct APIConfiguration { /// TEST environment. /// - Note: this configuration should only be used for demo / testing purposes. It can change frequently - static let test = Self(apiEndPoint: URL(string: "https://odpch-api.clients.liip.ch/ojp20-test")!, requesterReference: "OJP_Demo_iOS", authBearerToken: "eyJvcmciOiI2M2Q4ODhiMDNmZmRmODAwMDEzMDIwODkiLCJpZCI6IjUzYzAyNWI2ZTRhNjQyOTM4NzMxMDRjNTg2ODEzNTYyIiwiaCI6Im11cm11cjEyOCJ9") + public static let test = Self(apiEndPoint: URL(string: "https://odpch-api.clients.liip.ch/ojp20-test")!, requesterReference: "OJP_Demo_iOS", authBearerToken: "eyJvcmciOiI2M2Q4ODhiMDNmZmRmODAwMDEzMDIwODkiLCJpZCI6IjUzYzAyNWI2ZTRhNjQyOTM4NzMxMDRjNTg2ODEzNTYyIiwiaCI6Im11cm11cjEyOCJ9") /// INT environment. /// - Note: this configuration should only be used for demo / testing purposes. It can change frequently - static let int = Self(apiEndPoint: URL(string: "https://odpch-api.clients.liip.ch/ojp20-beta")!, requesterReference: "OJP_Demo_iOS", authBearerToken: "eyJvcmciOiI2M2Q4ODhiMDNmZmRmODAwMDEzMDIwODkiLCJpZCI6IjUzYzAyNWI2ZTRhNjQyOTM4NzMxMDRjNTg2ODEzNTYyIiwiaCI6Im11cm11cjEyOCJ9") + public static let int = Self(apiEndPoint: URL(string: "https://odpch-api.clients.liip.ch/ojp20-beta")!, requesterReference: "OJP_Demo_iOS", authBearerToken: "eyJvcmciOiI2M2Q4ODhiMDNmZmRmODAwMDEzMDIwODkiLCJpZCI6IjUzYzAyNWI2ZTRhNjQyOTM4NzMxMDRjNTg2ODEzNTYyIiwiaCI6Im11cm11cjEyOCJ9") } diff --git a/Sources/OJP/Models/OJPv2+Extensions.swift b/Sources/OJP/Models/OJPv2+Extensions.swift index 857041a..c46142e 100644 --- a/Sources/OJP/Models/OJPv2+Extensions.swift +++ b/Sources/OJP/Models/OJPv2+Extensions.swift @@ -5,8 +5,8 @@ // Created by Vasile Cotovanu on 20.03.2024. // -import Foundation import CoreLocation +import Foundation public extension OJPv2.Mode { enum PtMode: String { @@ -29,16 +29,14 @@ public extension OJPv2.Mode { } extension OJPv2.PlaceResult: GeoAware { - public var coords: Point { guard let geoPosition = place.geoPosition else { return (long: COORDINATE_FALLBACK, lat: COORDINATE_FALLBACK) } return (long: geoPosition.longitude, lat: geoPosition.latitude) } } -extension OJPv2.GeoPosition { - public var coordinates: CLLocationCoordinate2D { +public extension OJPv2.GeoPosition { + var coordinates: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: latitude, longitude: longitude) } } - diff --git a/Sources/OJP/Models/OJPv2.swift b/Sources/OJP/Models/OJPv2.swift index c011a27..6fec7d9 100644 --- a/Sources/OJP/Models/OJPv2.swift +++ b/Sources/OJP/Models/OJPv2.swift @@ -318,3 +318,9 @@ public struct OJPv2: Codable { } } } + +extension OJPv2.PlaceResult: Identifiable { + public var id: String { + place.stopPlace!.stopPlaceRef // stopPlace be renamed in https://github.com/openTdataCH/ojp-ios/tree/feature/address + } +} diff --git a/Sources/OJP/OJP.swift b/Sources/OJP/OJP.swift index 8cb0904..78988f3 100644 --- a/Sources/OJP/OJP.swift +++ b/Sources/OJP/OJP.swift @@ -85,7 +85,7 @@ public class OJP { return locationInformationDelivery.placeResults } - private func request(with ojp: OJPv2) async throws -> OJPv2.Response { + func request(with ojp: OJPv2) async throws -> OJPv2.Response { let ojpXMLData = try encoder.encode(ojp, withRootKey: "OJP", rootAttributes: OJP.requestXMLRootAttributes) guard String(data: ojpXMLData, encoding: .utf8) != nil else { throw OJPError.encodingFailed diff --git a/Tests/OJPTests/OjpSDKTests.swift b/Tests/OJPTests/OjpSDKTests.swift index c3a86e6..da5cb7d 100644 --- a/Tests/OJPTests/OjpSDKTests.swift +++ b/Tests/OJPTests/OjpSDKTests.swift @@ -87,14 +87,14 @@ final class OjpSDKTests: XCTestCase { dump(locationInformation) XCTAssertTrue(true) } - + func testParseMinimumRequiredLIRResponse() throws { let xmlData = try TestHelpers.loadXML(xmlFilename: "lir-minimum-response") let locationInformation = try OJPDecoder.parseXML(xmlData) dump(locationInformation) XCTAssertTrue(true) } - + func testParseRailBusAndUndergroundPtModes() throws { let xmlData = try TestHelpers.loadXML(xmlFilename: "lir-lausanne")