Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
VysotskiVadim committed Sep 22, 2023
1 parent 285009e commit 707f90f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})",
Expand Down Expand Up @@ -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,
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>>>,
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 707f90f

Please sign in to comment.