Skip to content

Commit

Permalink
Added Android Support for Make display of snapped location configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
cu8code committed Sep 14, 2024
1 parent bc6a74a commit d1eb044
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import uniffi.ferrostar.UserLocation
import uniffi.ferrostar.VisualInstruction

data class NavigationUiState(
val location: UserLocation?,
val snappedLocation: UserLocation?,
val heading: Float?,
val routeGeometry: List<GeographicCoordinate>,
Expand All @@ -33,10 +34,12 @@ data class NavigationUiState(
fun fromFerrostar(
coreState: NavigationState,
isMuted: Boolean?,
userLocation: UserLocation?
location: UserLocation?,
snappedLocation: UserLocation?
): NavigationUiState =
NavigationUiState(
snappedLocation = userLocation,
snappedLocation = snappedLocation,
location = location,
// TODO: Heading/course over ground
heading = null,
routeGeometry = coreState.routeGeometry,
Expand All @@ -60,22 +63,22 @@ interface NavigationViewModel {
class DefaultNavigationViewModel(
private val ferrostarCore: FerrostarCore,
private val spokenInstructionObserver: SpokenInstructionObserver? = null,
locationProvider: LocationProvider
private val locationProvider: LocationProvider
) : ViewModel(), NavigationViewModel {

private var lastLocation: UserLocation? = locationProvider.lastLocation
private var snappedLocation: UserLocation? = locationProvider.lastLocation

override val uiState =
ferrostarCore.state
.map { coreState ->
lastLocation =
val location = locationProvider.lastLocation
snappedLocation =
when (coreState.tripState) {
is TripState.Navigating -> coreState.tripState.snappedUserLocation
is TripState.Complete,
TripState.Idle -> locationProvider.lastLocation
}

uiState(coreState, spokenInstructionObserver?.isMuted, lastLocation)
uiState(coreState, spokenInstructionObserver?.isMuted, location, snappedLocation)
// This awkward dance is required because Kotlin doesn't have a way to map over
// StateFlows
// without converting to a generic Flow in the process.
Expand All @@ -85,7 +88,7 @@ class DefaultNavigationViewModel(
started = SharingStarted.WhileSubscribed(),
initialValue =
uiState(
ferrostarCore.state.value, spokenInstructionObserver?.isMuted, lastLocation))
ferrostarCore.state.value, spokenInstructionObserver?.isMuted, locationProvider.lastLocation, locationProvider.lastLocation))

override fun stopNavigation() {
ferrostarCore.stopNavigation()
Expand All @@ -99,6 +102,6 @@ class DefaultNavigationViewModel(
spokenInstructionObserver.isMuted = !spokenInstructionObserver.isMuted
}

private fun uiState(coreState: NavigationState, isMuted: Boolean?, location: UserLocation?) =
NavigationUiState.fromFerrostar(coreState, isMuted, location)
private fun uiState(coreState: NavigationState, isMuted: Boolean?, location: UserLocation?, snappedLocation: UserLocation?) =
NavigationUiState.fromFerrostar(coreState, isMuted, location, snappedLocation)
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fun NavigationState.Companion.pedestrianExample(): NavigationState {
}

fun NavigationUiState.Companion.pedestrianExample(): NavigationUiState =
fromFerrostar(NavigationState.pedestrianExample(), false, UserLocation.pedestrianExample())
fromFerrostar(NavigationState.pedestrianExample(), false, UserLocation.pedestrianExample(), UserLocation.pedestrianExample())

class MockNavigationViewModel(override val uiState: StateFlow<NavigationUiState>) :
ViewModel(), NavigationViewModel {
Expand Down

0 comments on commit d1eb044

Please sign in to comment.