From 707f90f5a98870d17316f5a44d07c6701c1777fc Mon Sep 17 00:00:00 2001
From: VysotskiVadim <vadzim.vysotski@mapbox.com>
Date: Fri, 22 Sep 2023 09:34:34 +0200
Subject: [PATCH] wip

---
 .../navigation/base/route/NavigationRoute.kt  |  8 +++-
 .../route/internal/RouterWrapper.kt           | 12 ++++--
 .../internal/MapboxNativeNavigatorImpl.kt     | 39 ++++---------------
 3 files changed, 23 insertions(+), 36 deletions(-)

diff --git a/libnavigation-base/src/main/java/com/mapbox/navigation/base/route/NavigationRoute.kt b/libnavigation-base/src/main/java/com/mapbox/navigation/base/route/NavigationRoute.kt
index 00845d57ae8..e20c66a60e8 100644
--- a/libnavigation-base/src/main/java/com/mapbox/navigation/base/route/NavigationRoute.kt
+++ b/libnavigation-base/src/main/java/com/mapbox/navigation/base/route/NavigationRoute.kt
@@ -37,6 +37,11 @@ import java.io.InputStreamReader
 import java.net.URL
 import java.nio.ByteBuffer
 
+data class LatestRouteRefresh(
+    val geometryIndex: Int,
+    val directionsApiResponse: String
+)
+
 /**
  * Wraps a route object used across the Navigation SDK features.
  * @param directionsResponse the original response that returned this route object.
@@ -51,7 +56,8 @@ class NavigationRoute internal constructor(
     internal val nativeRoute: RouteInterface,
     internal val unavoidableClosures: List<List<Closure>>,
     internal var expirationTimeElapsedSeconds: Long?,
-) {
+    var latestRouteRefresh: LatestRouteRefresh? = null,
+    ) {
 
     internal constructor(
         directionsResponse: DirectionsResponse,
diff --git a/libnavigation-router/src/main/java/com/mapbox/navigation/route/internal/RouterWrapper.kt b/libnavigation-router/src/main/java/com/mapbox/navigation/route/internal/RouterWrapper.kt
index 6e92ca8b531..43e9d88eb62 100644
--- a/libnavigation-router/src/main/java/com/mapbox/navigation/route/internal/RouterWrapper.kt
+++ b/libnavigation-router/src/main/java/com/mapbox/navigation/route/internal/RouterWrapper.kt
@@ -17,6 +17,7 @@ import com.mapbox.navigation.base.internal.route.updateExpirationTime
 import com.mapbox.navigation.base.internal.utils.Constants
 import com.mapbox.navigation.base.internal.utils.mapToSdkRouteOrigin
 import com.mapbox.navigation.base.internal.utils.parseDirectionsResponse
+import com.mapbox.navigation.base.route.LatestRouteRefresh
 import com.mapbox.navigation.base.route.NavigationRoute
 import com.mapbox.navigation.base.route.NavigationRouterCallback
 import com.mapbox.navigation.base.route.NavigationRouterRefreshCallback
@@ -254,10 +255,10 @@ class RouterWrapper(
                         )
                     }
                 },
-                {
+                { directionsResponse ->
                     mainJobControl.scope.launch {
                         withContext(ThreadController.DefaultDispatcher) {
-                            parseDirectionsRouteRefresh(it)
+                            parseDirectionsRouteRefresh(directionsResponse)
                                 .onValue {
                                     logD(
                                         "Parsed route refresh response for route(${route.id})",
@@ -292,12 +293,17 @@ class RouterWrapper(
                                         refreshTtl = routeRefresh.unrecognizedJsonProperties
                                             ?.get(Constants.RouteResponse.KEY_REFRESH_TTL)?.asInt
                                     )
+                                    route.latestRouteRefresh = LatestRouteRefresh(
+                                        geometryIndex = routeRefreshRequestData.routeGeometryIndex,
+                                        directionsResponse
+                                    )
+                                    route
                                 }
                         }.fold(
                             { throwable ->
                                 callback.onFailure(
                                     RouterFactory.buildNavigationRouterRefreshError(
-                                        "failed for response: $it",
+                                        "failed for response: $directionsResponse",
                                         throwable,
                                     )
                                 )
diff --git a/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt b/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt
index 8269d3fa3eb..b6e929e7e1f 100644
--- a/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt
+++ b/libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt
@@ -212,35 +212,8 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator {
     override suspend fun refreshRoute(
         route: NavigationRoute
     ): Expected<String, List<RouteAlternative>> {
-        val refreshedLegs = route.directionsRoute.legs()?.map { routeLeg ->
-            RouteLegRefresh.builder()
-                .annotation(routeLeg.annotation())
-                .incidents(routeLeg.incidents())
-                .build()
-        }
-        val refreshedWaypoints = route.waypoints
-        val refreshRoute = DirectionsRouteRefresh.builder()
-            .legs(refreshedLegs)
-            .unrecognizedJsonProperties(
-                refreshedWaypoints?.let { waypoints ->
-                    mapOf(
-                        Constants.RouteResponse.KEY_WAYPOINTS to JsonArray().apply {
-                            waypoints.forEach { waypoint ->
-                                add(JsonParser.parseString(waypoint.toJson()))
-                            }
-                        }
-                    )
-                }
-            )
-            .build()
-        val refreshResponse = DirectionsRefreshResponse.builder()
-            .code("200")
-            .route(refreshRoute)
-            .build()
-
-        val refreshResponseJson = withContext(ThreadController.DefaultDispatcher) {
-            refreshResponse.toJson()
-        }
+
+        val latestRefresh = route.latestRouteRefresh!!
 
         val callback = {
                 continuation: Continuation<Expected<String, List<RouteAlternative>>>,
@@ -273,13 +246,15 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator {
         return suspendCancellableCoroutine { continuation ->
             logD(
                 "Refreshing native route ${route.nativeRoute().routeId} " +
-                    "with generated refresh response: $refreshResponseJson",
+                    "with generated refresh response: ${latestRefresh.directionsApiResponse}",
                 LOG_CATEGORY
             )
             navigator!!.refreshRoute(
-                refreshResponseJson,
-                route.nativeRoute().routeId
+                latestRefresh.directionsApiResponse,
+                route.nativeRoute().routeId,
+                latestRefresh.geometryIndex
             ) { callback(continuation, it) }
+            route.latestRouteRefresh = null
         }
     }