Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route refresh memory optimisation #7528

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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