Skip to content

Commit

Permalink
Add spoken instructions to Route model; cleanup+refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthetechie committed Nov 19, 2023
1 parent 24ea122 commit dfaec1a
Show file tree
Hide file tree
Showing 20 changed files with 345 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.compose.ui.unit.sp
import uniffi.ferrostar.ManeuverModifier
import uniffi.ferrostar.ManeuverType
import uniffi.ferrostar.VisualInstructionContent
import uniffi.ferrostar.VisualInstructions
import uniffi.ferrostar.VisualInstruction

val VisualInstructionContent.maneuverIcon: ImageVector?
get() {
Expand All @@ -47,7 +47,7 @@ val VisualInstructionContent.maneuverIcon: ImageVector?


@Composable
fun BannerView(instructions: VisualInstructions, distanceToNextManeuver: Double?) {
fun BannerView(instructions: VisualInstruction, distanceToNextManeuver: Double?) {
Column(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -85,7 +85,7 @@ fun BannerView(instructions: VisualInstructions, distanceToNextManeuver: Double?
@Preview()
@Composable
fun PreviewBannerView() {
val instructions = VisualInstructions(
val instructions = VisualInstruction(
primaryContent = VisualInstructionContent(
text = "Hyde Street",
maneuverType = ManeuverType.TURN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import okhttp3.mock.url
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Test
import uniffi.ferrostar.GeographicCoordinates
import uniffi.ferrostar.GeographicCoordinate
import uniffi.ferrostar.Route
import uniffi.ferrostar.RouteAdapter
import uniffi.ferrostar.RouteRequest
Expand All @@ -30,7 +30,7 @@ private val valhallaEndpointUrl = "https://api.stadiamaps.com/navigate/v1"
class MockRouteRequestGenerator: RouteRequestGenerator {
override fun generateRequest(
userLocation: UserLocation,
waypoints: List<GeographicCoordinates>
waypoints: List<GeographicCoordinate>
): RouteRequest = RouteRequest.HttpPost(valhallaEndpointUrl, mapOf(), byteArrayOf())

}
Expand Down Expand Up @@ -69,8 +69,8 @@ class FerrostarCoreTest {
try {
// Tests that the core generates a request and attempts to process it, but throws due to the mocked network layer
core.getRoutes(
initialLocation = UserLocation(coordinates = GeographicCoordinates(-149.543469, 60.5347155), 0.0, null, Instant.now()),
waypoints = listOf(GeographicCoordinates(-149.5485806, 60.5349908))
initialLocation = UserLocation(coordinates = GeographicCoordinate(-149.543469, 60.5347155), 0.0, null, Instant.now()),
waypoints = listOf(GeographicCoordinate(-149.5485806, 60.5349908))
)
fail("Expected the request to fail")
} catch (e: InvalidStatusCodeException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import okhttp3.mock.rule
import okhttp3.mock.url
import org.junit.Assert.assertEquals
import org.junit.Test
import uniffi.ferrostar.GeographicCoordinates
import uniffi.ferrostar.GeographicCoordinate
import uniffi.ferrostar.UserLocation
import java.net.URL
import java.time.Instant
Expand Down Expand Up @@ -254,41 +254,41 @@ class ValhallaCoreTest {
return runTest {
val routes = core.getRoutes(
UserLocation(
GeographicCoordinates(60.5347155, -149.543469), 12.0, null, Instant.now()
), waypoints = listOf(GeographicCoordinates(60.5349908, -149.5485806))
GeographicCoordinate(60.5347155, -149.543469), 12.0, null, Instant.now()
), waypoints = listOf(GeographicCoordinate(60.5349908, -149.5485806))
)

assertEquals(routes.count(), 1)
assertEquals(
listOf(
GeographicCoordinates(
GeographicCoordinate(
-149.543469, 60.534716
),
GeographicCoordinates(
GeographicCoordinate(
-149.543879, 60.534782
),
GeographicCoordinates(
GeographicCoordinate(
-149.544134, 60.534829
),
GeographicCoordinates(
GeographicCoordinate(
-149.5443, 60.534856
),
GeographicCoordinates(
GeographicCoordinate(
-149.544533, 60.534887
),
GeographicCoordinates(
GeographicCoordinate(
-149.544976, 60.534941
),
GeographicCoordinates(
GeographicCoordinate(
-149.545485, 60.534971
),
GeographicCoordinates(
GeographicCoordinate(
-149.546177, 60.535003
),
GeographicCoordinates(
GeographicCoordinate(
-149.546937, 60.535008
),
GeographicCoordinates(
GeographicCoordinate(
-149.548581, 60.534991
),
), routes.first().geometry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.stadiamaps.ferrostar.core
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import uniffi.ferrostar.GeographicCoordinates
import uniffi.ferrostar.GeographicCoordinate
import uniffi.ferrostar.NavigationController
import uniffi.ferrostar.NavigationControllerConfig
import uniffi.ferrostar.Route
Expand Down Expand Up @@ -46,7 +46,7 @@ public class FerrostarCore(
)

suspend fun getRoutes(
initialLocation: UserLocation, waypoints: List<GeographicCoordinates>
initialLocation: UserLocation, waypoints: List<GeographicCoordinate>
): List<Route> {
when (val request = routeAdapter.generateRequest(initialLocation, waypoints)) {
is RouteRequest.HttpPost -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.stadiamaps.ferrostar.core

import uniffi.ferrostar.CourseOverGround
import uniffi.ferrostar.GeographicCoordinates
import uniffi.ferrostar.GeographicCoordinate
import uniffi.ferrostar.UserLocation
import java.time.Instant
import java.util.concurrent.Executor

interface Location {
val coordinates: GeographicCoordinates
val coordinates: GeographicCoordinate
val horizontalAccuracy: Double
val courseOverGround: CourseOverGround?
val timestamp: Instant
Expand All @@ -21,21 +21,21 @@ interface Location {
}

data class SimulatedLocation(
override val coordinates: GeographicCoordinates,
override val coordinates: GeographicCoordinate,
override val horizontalAccuracy: Double,
override val courseOverGround: CourseOverGround?,
override val timestamp: Instant
) : Location

// TODO: Decide if we want to have a compile-time dependency on Android
data class AndroidLocation(
override val coordinates: GeographicCoordinates,
override val coordinates: GeographicCoordinate,
override val horizontalAccuracy: Double,
override val courseOverGround: CourseOverGround?,
override val timestamp: Instant
) : Location {
constructor(location: android.location.Location) : this(
GeographicCoordinates(location.latitude, location.longitude),
GeographicCoordinate(location.latitude, location.longitude),
location.accuracy.toDouble(),
if (location.hasBearing() && location.hasBearingAccuracy()) {
CourseOverGround(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.stadiamaps.ferrostar.core
import org.junit.Test

import org.junit.Assert.*
import uniffi.ferrostar.GeographicCoordinates
import uniffi.ferrostar.GeographicCoordinate
import java.time.Instant
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
Expand All @@ -21,7 +21,7 @@ class SimulatedLocationProviderTest {
@Test
fun `set location`() {
val locationProvider = SimulatedLocationProvider()
val location = SimulatedLocation(GeographicCoordinates(42.02, 24.0), 12.0, null, Instant.now())
val location = SimulatedLocation(GeographicCoordinate(42.02, 24.0), 12.0, null, Instant.now())

locationProvider.lastLocation = location

Expand All @@ -32,7 +32,7 @@ class SimulatedLocationProviderTest {
fun `test listener events`() {
val latch = CountDownLatch(1)
val locationProvider = SimulatedLocationProvider()
val location = SimulatedLocation(GeographicCoordinates(42.02, 24.0), 12.0, null, Instant.now())
val location = SimulatedLocation(GeographicCoordinate(42.02, 24.0), 12.0, null, Instant.now())

val listener = object : LocationUpdateListener {
override fun onLocationUpdated(location: Location) {
Expand Down
6 changes: 3 additions & 3 deletions apple/Sources/FerrostarCore/CoreLocation Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import CoreLocation
import UniFFI

extension CLLocationCoordinate2D {
var geographicCoordinates: UniFFI.GeographicCoordinates {
UniFFI.GeographicCoordinates(lng: longitude, lat: latitude)
var geographicCoordinates: UniFFI.GeographicCoordinate {
UniFFI.GeographicCoordinate(lng: longitude, lat: latitude)
}

init(geographicCoordinates: GeographicCoordinates) {
init(geographicCoordinates: GeographicCoordinate) {
self.init(latitude: geographicCoordinates.lat, longitude: geographicCoordinates.lng)
}
}
Expand Down
4 changes: 2 additions & 2 deletions apple/Sources/FerrostarCore/ObservableState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class FerrostarObservableState: ObservableObject {
@Published public internal(set) var fullRouteShape: [CLLocationCoordinate2D]
@Published public internal(set) var remainingWaypoints: [CLLocationCoordinate2D]
@Published public internal(set) var currentStep: UniFFI.RouteStep
@Published public internal(set) var visualInstructions: UniFFI.VisualInstructions?
@Published public internal(set) var visualInstructions: UniFFI.VisualInstruction?
@Published public internal(set) var spokenInstruction: UniFFI.SpokenInstruction?
@Published public internal(set) var distanceToNextManeuver: CLLocationDistance?

Expand All @@ -32,7 +32,7 @@ public final class FerrostarObservableState: ObservableObject {
let remainingWaypoints = Array(samplePedestrianWaypoints.dropFirst(n))
let lastUserLocation = remainingWaypoints.first!

let result = FerrostarObservableState(snappedLocation: CLLocation(latitude: samplePedestrianWaypoints.first!.latitude, longitude: samplePedestrianWaypoints.first!.longitude), fullRoute: samplePedestrianWaypoints, steps: [UniFFI.RouteStep(geometry: [lastUserLocation.geographicCoordinates], distance: 100, roadName: "Jefferson St.", instruction: "Walk west on Jefferson St.", visualInstructions: [UniFFI.VisualInstructions(primaryContent: VisualInstructionContent(text: "Hyde Street", maneuverType: .turn, maneuverModifier: .left, roundaboutExitDegrees: nil), secondaryContent: nil, triggerDistanceBeforeManeuver: 42.0)])])
let result = FerrostarObservableState(snappedLocation: CLLocation(latitude: samplePedestrianWaypoints.first!.latitude, longitude: samplePedestrianWaypoints.first!.longitude), fullRoute: samplePedestrianWaypoints, steps: [UniFFI.RouteStep(geometry: [lastUserLocation.geographicCoordinates], distance: 100, roadName: "Jefferson St.", instruction: "Walk west on Jefferson St.", visualInstructions: [UniFFI.VisualInstruction(primaryContent: VisualInstructionContent(text: "Hyde Street", maneuverType: .turn, maneuverModifier: .left, roundaboutExitDegrees: nil), secondaryContent: nil, triggerDistanceBeforeManeuver: 42.0)], spokenInstructions: [])])

result.remainingWaypoints = remainingWaypoints
result.snappedLocation = CLLocation(latitude: lastUserLocation.latitude, longitude: lastUserLocation.longitude)
Expand Down
6 changes: 3 additions & 3 deletions apple/Sources/FerrostarMapLibreUI/BannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension UniFFI.VisualInstructionContent {
}

struct BannerView: View {
let instructions: VisualInstructions
let instructions: VisualInstruction
let distanceToNextManeuver: CLLocationDistance?
let formatter = MKDistanceFormatter()

Expand Down Expand Up @@ -80,8 +80,8 @@ struct BannerView: View {
}

#Preview {
let location = GeographicCoordinates(lng: 0, lat: 0)
let instructions = UniFFI.VisualInstructions(primaryContent: VisualInstructionContent(text: "Hyde Street", maneuverType: .turn, maneuverModifier: .left, roundaboutExitDegrees: nil), secondaryContent: nil, triggerDistanceBeforeManeuver: 42.0)
let location = GeographicCoordinate(lng: 0, lat: 0)
let instructions = UniFFI.VisualInstruction(primaryContent: VisualInstructionContent(text: "Hyde Street", maneuverType: .turn, maneuverModifier: .left, roundaboutExitDegrees: nil), secondaryContent: nil, triggerDistanceBeforeManeuver: 42.0)

return BannerView(instructions: instructions, distanceToNextManeuver: 42)
}
Loading

0 comments on commit dfaec1a

Please sign in to comment.