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

Add empty waypoint names to HistoryEventMapper#mapNavigationRoute as old records may not include them #5627

Merged
merged 1 commit into from
Mar 30, 2022
Merged
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
@@ -1,5 +1,7 @@
package com.mapbox.navigation.core.history.model

import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.mapbox.api.directions.v5.DirectionsCriteria
import com.mapbox.api.directions.v5.models.DirectionsResponse
import com.mapbox.api.directions.v5.models.DirectionsRoute
Expand Down Expand Up @@ -82,29 +84,58 @@ internal class HistoryEventMapper {
"request URL or route options of set route history event cannot be null or empty"
)
try {
val directionsResponse = DirectionsResponse.fromJson(response)
val routeOptions = setRoute.routeRequest?.let {
// Old records may include empty routeRequest
if (it.isEmpty()) return@let null
RouteOptions.fromUrl(URL(it))
} ?: directionsResponse.routes().firstOrNull()?.routeOptions()
?: throw noOptionsException
runBlocking {
NavigationRoute.create(directionsResponse, routeOptions).first()
}
retrieveNavigationRoute(response, setRoute, noOptionsException)
} catch (t: Throwable) {
if (t === noOptionsException) {
throw t
} else {
DirectionsRoute.fromJson(response).toBuilder()
.routeIndex("0")
.build()
.toNavigationRoute()
try {
// Old records may not include waypoint namfes
val jsonResponse = addWaypointNames(response)
retrieveNavigationRoute(jsonResponse, setRoute, noOptionsException)
} catch (t: Throwable) {
if (t === noOptionsException) {
throw t
} else {
DirectionsRoute.fromJson(response).toBuilder()
.routeIndex("0")
.build()
.toNavigationRoute()
}
}
}
}
}
}

private fun retrieveNavigationRoute(
jsonResponse: String,
setRoute: SetRouteHistoryRecord,
noOptionsException: IllegalArgumentException
): NavigationRoute {
val directionsResponse = DirectionsResponse.fromJson(jsonResponse)
val routeOptions = setRoute.routeRequest?.let {
// Old records may include empty routeRequest
if (it.isEmpty()) return@let null
RouteOptions.fromUrl(URL(it))
} ?: directionsResponse.routes().firstOrNull()?.routeOptions() ?: throw noOptionsException
return runBlocking {
NavigationRoute.create(directionsResponse, routeOptions).first()
}
}

private fun addWaypointNames(response: String): String {
korshaknn marked this conversation as resolved.
Show resolved Hide resolved
val gson = GsonBuilder().create()
val jsonObject = gson.fromJson(response, JsonObject::class.java)
val waypoints = jsonObject?.getAsJsonArray(WAYPOINTS_JSON_KEY)?.map { it.asJsonObject }
waypoints?.forEach { waypoint ->
if (!waypoint.has(NAME_JSON_KEY)) {
waypoint.addProperty(NAME_JSON_KEY, "")
}
}
return jsonObject.toString()
}

private fun mapToWaypoints(routeOptions: RouteOptions?): List<HistoryWaypoint> {
val coordinatesList = routeOptions?.coordinatesList()
val waypointIndices = routeOptions?.waypointIndicesList()
Expand Down Expand Up @@ -136,6 +167,9 @@ internal class HistoryEventMapper {
)

private companion object {

private const val NANOS_PER_SECOND = 1e-9
private const val WAYPOINTS_JSON_KEY = "waypoints"
private const val NAME_JSON_KEY = "name"
}
}