diff --git a/react-native/src/core/NavigationUiState.ts b/react-native/src/core/NavigationUiState.ts index eb369742..6e4ba41a 100644 --- a/react-native/src/core/NavigationUiState.ts +++ b/react-native/src/core/NavigationUiState.ts @@ -8,6 +8,13 @@ import { type UserLocation, type VisualInstruction, } from '../generated/ferrostar'; +import { + currentRoadName, + deviation, + progress, + remainingSteps, + visualInstruction, +} from './_utils'; import type { NavigationState } from './FerrostarCore'; export class NavigationUiState { @@ -86,23 +93,19 @@ export class NavigationUiState { location?: UserLocation, snappedLocation?: UserLocation ): NavigationUiState { - let tripState; - if (TripState.Navigating.instanceOf(coreState.tripState)) { - tripState = coreState.tripState; - } return new NavigationUiState( location, - snappedLocation ?? tripState?.inner.snappedUserLocation, + snappedLocation, undefined, coreState.routeGeometry, - tripState?.inner.visualInstruction, + visualInstruction(coreState.tripState), undefined, - tripState?.inner.progress, + progress(coreState.tripState), coreState.isCalculatingNewRoute, - tripState?.inner.deviation, + deviation(coreState.tripState), isMuted, - undefined, // TODO: Android seems to have this type but it's not in the TS definition - tripState?.inner.remainingSteps + currentRoadName(coreState.tripState), + remainingSteps(coreState.tripState) ); } diff --git a/react-native/src/core/_utils.ts b/react-native/src/core/_utils.ts index e73ffafc..5273b6df 100644 --- a/react-native/src/core/_utils.ts +++ b/react-native/src/core/_utils.ts @@ -1,3 +1,12 @@ +import { + RouteDeviation, + RouteStep, + TripProgress, + TripState, + UserLocation, + VisualInstruction, +} from '../generated/ferrostar'; + export function getNanoTime(): number { const hrTime = process.hrtime(); return hrTime[0] * 1000000000 + hrTime[1]; @@ -8,3 +17,56 @@ export function ab2json(ab: ArrayBuffer): object { String.fromCharCode.apply(null, Array.from(new Uint8Array(ab))) ); } + +export function currentRoadName(tripState: TripState): string | undefined { + if (TripState.Navigating.instanceOf(tripState)) { + return tripState.inner.remainingSteps[0]?.roadName; + } else { + return undefined; + } +} + +export function visualInstruction( + tripState: TripState +): VisualInstruction | undefined { + if (TripState.Navigating.instanceOf(tripState)) { + return tripState.inner.visualInstruction; + } else { + return undefined; + } +} + +export function deviation(tripState: TripState): RouteDeviation | undefined { + if (TripState.Navigating.instanceOf(tripState)) { + return tripState.inner.deviation; + } else { + return undefined; + } +} + +export function progress(tripState: TripState): TripProgress | undefined { + if (TripState.Navigating.instanceOf(tripState)) { + return tripState.inner.progress; + } else { + return undefined; + } +} + +export function remainingSteps(tripState: TripState): RouteStep[] | undefined { + if (TripState.Navigating.instanceOf(tripState)) { + return tripState.inner.remainingSteps; + } else { + return undefined; + } +} + +export function snappedUserLocation( + tripState: TripState, + fallback?: UserLocation +): UserLocation | undefined { + if (TripState.Navigating.instanceOf(tripState)) { + return tripState.inner.snappedUserLocation; + } else { + return fallback; + } +} diff --git a/react-native/src/views/NavigationView.tsx b/react-native/src/views/NavigationView.tsx index 243e34b8..ad968f57 100644 --- a/react-native/src/views/NavigationView.tsx +++ b/react-native/src/views/NavigationView.tsx @@ -23,6 +23,7 @@ import TripProgressView from './TripProgressView'; import { StyleSheet, View } from 'react-native'; import InstructionsView from './InstructionsView'; import MapControls from './MapControls'; +import { snappedUserLocation } from '../core/_utils'; setAccessToken(null); @@ -125,7 +126,10 @@ const NavigationView = (props: NavigationViewProps) => { NavigationUiState.fromFerrostar( state, isMuted, - core.locationProvider.lastLocation + snappedUserLocation( + state.tripState, + core.locationProvider.lastLocation + ) ) ); });