Skip to content

Commit

Permalink
Feat: annotation parsing on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Archdoog committed Oct 7, 2024
1 parent 437f997 commit ae47cc1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions apple/DemoApp/Demo/DemoNavigationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct DemoNavigationView: View {
// NOTE: This is probably not ideal but works for demo purposes.
// This causes a thread performance checker warning log.
private let spokenInstructionObserver = AVSpeechSpokenInstructionObserver(isMuted: false)

private var locationProvider: LocationProviding
@ObservedObject private var ferrostarCore: FerrostarCore

Expand Down Expand Up @@ -227,7 +227,7 @@ struct DemoNavigationView: View {
}
return annotation.speedLimit?.measurementValue
}

private func preventAutoLock() {
UIApplication.shared.isIdleTimerDisabled = true
}
Expand Down
9 changes: 4 additions & 5 deletions apple/Sources/FerrostarCore/Models/Annotations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ import Foundation

/// A Valhalla OSRM flavored annotations object.
public struct ValhallaOsrmAnnotation: Decodable {

enum CodingKeys: String, CodingKey {
case speedLimit = "maxspeed"
case speed
case distance
case duration
}

/// The speed limit for the current line segment.
public let speedLimit: MaxSpeed?

public let speed: Double?

public let distance: Double?

public let duration: Double?
}
37 changes: 19 additions & 18 deletions apple/Sources/FerrostarCore/Models/MaxSpeed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,63 @@ import Foundation
/// - https://docs.mapbox.com/api/navigation/directions/#route-leg-object (search for `max_speed`)
/// - https://valhalla.github.io/valhalla/speeds/#assignment-of-speeds-to-roadways
public enum MaxSpeed: Decodable {

public enum Units: String, Decodable {
case kilometersPerHour = "km/h"
case milesPerHour = "mph"
case knots // "knots" are an option in core OSRM docs, though unsure if they're ever used in this context.
}

/// There is no speed limit (it's unlimited, e.g. German Autobahn)
case none

/// The speed limit is not known.
case unknown

/// The speed limit is a known value and unit (this may be localized depending on the API).
case speed(Double, unit: Units)

enum CodingKeys: CodingKey {
case none
case unknown
case speed
case unit
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

if let none = try container.decodeIfPresent(Bool.self, forKey: .none) {

Check warning on line 36 in apple/Sources/FerrostarCore/Models/MaxSpeed.swift

View workflow job for this annotation

GitHub Actions / test (FerrostarCore-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

value 'none' was defined but never used; consider replacing with boolean test
// The speed configuration is `{none: true}` for unlimited.
self = .none
} else if let unknown = try container.decodeIfPresent(Bool.self, forKey: .unknown) {

Check warning on line 39 in apple/Sources/FerrostarCore/Models/MaxSpeed.swift

View workflow job for this annotation

GitHub Actions / test (FerrostarCore-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

value 'unknown' was defined but never used; consider replacing with boolean test
// The speed configuration is `{unknown: true}` for unknown.
self = .unknown
} else if let value = try container.decodeIfPresent(Double.self, forKey: .speed),
let unit = try container.decodeIfPresent(Units.self, forKey: .unit) {
let unit = try container.decodeIfPresent(Units.self, forKey: .unit)
{
// The speed is a known value with units. Some API's may localize, others only support a single unit.
self = .speed(value, unit: unit)
} else {
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Invalid MaxSpeed, see docstrings for reference links"))
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "Invalid MaxSpeed, see docstrings for reference links"
))
}
}

/// The MaxSpeed as a measurement
public var measurementValue: Measurement<UnitSpeed>? {
switch self {

case .none: return nil
case .unknown: return nil
case .speed(let value, let unit):
case .none: nil
case .unknown: nil
case let .speed(value, unit):
switch unit {

case .kilometersPerHour:
return .init(value: value, unit: .kilometersPerHour)
.init(value: value, unit: .kilometersPerHour)
case .milesPerHour:
return .init(value: value, unit: .milesPerHour)
.init(value: value, unit: .milesPerHour)
case .knots:
return .init(value: value, unit: .knots)
.init(value: value, unit: .knots)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions apple/Sources/FerrostarCore/NavigationState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public struct NavigationState: Hashable {

return annotationJson
}

/// The current geometry segment's annotations.
///
/// A segment is the line between two coordinates on the geometry.
Expand All @@ -69,7 +69,7 @@ public struct NavigationState: Hashable {
guard let data = currentAnnotationJSON?.data(using: .utf8) else {
return nil
}

return try JSONDecoder().decode(type, from: data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct DynamicallyOrientingNavigationView: View, CustomizableNavigatingIn

var calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)?
@State var speedLimit: Measurement<UnitSpeed>?

var onTapExit: (() -> Void)?

public var minimumSafeAreaInsets: EdgeInsets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct LandscapeNavigationView: View, CustomizableNavigatingInnerGridView

var calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)?
@State var speedLimit: Measurement<UnitSpeed>?

var onTapExit: (() -> Void)?

public var minimumSafeAreaInsets: EdgeInsets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct PortraitNavigationView: View, CustomizableNavigatingInnerGridView

var calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)?
@State var speedLimit: Measurement<UnitSpeed>?

var onTapExit: (() -> Void)?

/// Create a portrait navigation view. This view is optimized for display on a portrait screen where the
Expand Down

0 comments on commit ae47cc1

Please sign in to comment.