@@ -9,6 +9,7 @@ import com.mapbox.android.core.permissions.PermissionsManager
99import com.mapbox.api.directions.v5.DirectionsCriteria
1010import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
1111import com.mapbox.navigation.base.route.NavigationRoute
12+ import com.mapbox.navigation.base.trip.model.RouteProgress
1213import com.mapbox.navigation.core.MapboxNavigation
1314import com.mapbox.navigation.core.directions.session.RoutesObserver
1415import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
@@ -17,6 +18,7 @@ import com.mapbox.navigation.core.replay.MapboxReplayer
1718import com.mapbox.navigation.core.replay.history.ReplayEventBase
1819import com.mapbox.navigation.core.replay.history.ReplayEventUpdateLocation
1920import com.mapbox.navigation.core.replay.history.ReplayEventsObserver
21+ import com.mapbox.navigation.core.trip.session.RouteProgressObserver
2022import com.mapbox.navigation.utils.internal.logW
2123import java.util.Collections
2224
@@ -58,11 +60,23 @@ class ReplayRouteSession : MapboxNavigationObserver {
5860 private lateinit var replayRouteMapper: ReplayRouteMapper
5961 private var mapboxNavigation: MapboxNavigation ? = null
6062 private var lastLocationEvent: ReplayEventUpdateLocation ? = null
61- private var routesObserver: RoutesObserver ? = null
62- private var currentRouteId: String? = null
63+ private var currentRoute: NavigationRoute ? = null
64+
65+ private val routeProgressObserver = RouteProgressObserver { routeProgress ->
66+ if (currentRoute?.id != routeProgress.navigationRoute.id) {
67+ currentRoute = routeProgress.navigationRoute
68+ onRouteChanged(routeProgress)
69+ }
70+ }
71+
72+ private val routesObserver = RoutesObserver { result ->
73+ if (result.navigationRoutes.isEmpty()) {
74+ mapboxNavigation?.resetReplayLocation()
75+ }
76+ }
6377
6478 private val replayEventsObserver = ReplayEventsObserver { events ->
65- if (isLastEventPlayed(events)) {
79+ if (currentRoute != null && isLastEventPlayed(events)) {
6680 pushMorePoints()
6781 }
6882 }
@@ -92,17 +106,10 @@ class ReplayRouteSession : MapboxNavigationObserver {
92106 this .mapboxNavigation = mapboxNavigation
93107 mapboxNavigation.stopTripSession()
94108 mapboxNavigation.startReplayTripSession()
95-
96- routesObserver = RoutesObserver { result ->
97- if (result.navigationRoutes.isEmpty()) {
98- currentRouteId = null
99- mapboxNavigation.resetReplayLocation()
100- } else if (result.navigationRoutes.first().id != currentRouteId) {
101- onRouteChanged(result.navigationRoutes.first())
102- }
103- }.also { mapboxNavigation.registerRoutesObserver(it) }
109+ mapboxNavigation.registerRouteProgressObserver(routeProgressObserver)
110+ mapboxNavigation.registerRoutesObserver(routesObserver)
104111 mapboxNavigation.mapboxReplayer.registerObserver(replayEventsObserver)
105- mapboxNavigation.resetReplayLocation ()
112+ mapboxNavigation.mapboxReplayer.play ()
106113 }
107114
108115 private fun MapboxNavigation.resetReplayLocation () {
@@ -123,14 +130,18 @@ class ReplayRouteSession : MapboxNavigationObserver {
123130 }
124131
125132 override fun onDetached (mapboxNavigation : MapboxNavigation ) {
126- this .mapboxNavigation = null
133+ mapboxNavigation.unregisterRoutesObserver(routesObserver)
134+ mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver)
127135 mapboxNavigation.mapboxReplayer.unregisterObserver(replayEventsObserver)
128136 mapboxNavigation.mapboxReplayer.stop()
129137 mapboxNavigation.mapboxReplayer.clearEvents()
130138 mapboxNavigation.stopTripSession()
139+ this .mapboxNavigation = null
140+ this .currentRoute = null
131141 }
132142
133- private fun onRouteChanged (navigationRoute : NavigationRoute ) {
143+ private fun onRouteChanged (routeProgress : RouteProgress ) {
144+ val navigationRoute = routeProgress.navigationRoute
134145 val mapboxReplayer = mapboxNavigation?.mapboxReplayer ? : return
135146 mapboxReplayer.clearEvents()
136147 mapboxReplayer.play()
@@ -144,9 +155,12 @@ class ReplayRouteSession : MapboxNavigationObserver {
144155 }
145156 return
146157 }
147- currentRouteId = navigationRoute.id
148158 polylineDecodeStream = ReplayPolylineDecodeStream (geometry, 6 )
149- mapboxNavigation?.resetTripSession()
159+
160+ // Skip up to the current geometry index. There is some imprecision here because the
161+ // distance traveled is not equal to a route index.
162+ polylineDecodeStream.skip(routeProgress.currentRouteGeometryIndex)
163+
150164 pushMorePoints()
151165 }
152166
0 commit comments