Skip to content

Commit

Permalink
Add TripState extensions as utils functions and apply them to the Nav…
Browse files Browse the repository at this point in the history
…igationUiState correctly
  • Loading branch information
bjtrounson committed Jan 28, 2025
1 parent f5f2ade commit 779e3b8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
23 changes: 13 additions & 10 deletions react-native/src/core/NavigationUiState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
);
}

Expand Down
62 changes: 62 additions & 0 deletions react-native/src/core/_utils.ts
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -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;
}
}
6 changes: 5 additions & 1 deletion react-native/src/views/NavigationView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -125,7 +126,10 @@ const NavigationView = (props: NavigationViewProps) => {
NavigationUiState.fromFerrostar(
state,
isMuted,
core.locationProvider.lastLocation
snappedUserLocation(
state.tripState,
core.locationProvider.lastLocation
)
)
);
});
Expand Down

0 comments on commit 779e3b8

Please sign in to comment.