Skip to content

Commit

Permalink
add empty waypoint names to history event map navigation route as old…
Browse files Browse the repository at this point in the history
… records may not include them causing parsing issues when replaying old history files
  • Loading branch information
Guardiola31337 committed Mar 29, 2022
1 parent 6663aa2 commit b4bfddf
Showing 1 changed file with 22 additions and 1 deletion.
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,7 +84,9 @@ internal class HistoryEventMapper {
"request URL or route options of set route history event cannot be null or empty"
)
try {
val directionsResponse = DirectionsResponse.fromJson(response)
// Old records may not include waypoint names
val jsonResponse = addWaypointNames(response)
val directionsResponse = DirectionsResponse.fromJson(jsonResponse)
val routeOptions = setRoute.routeRequest?.let {
// Old records may include empty routeRequest
if (it.isEmpty()) return@let null
Expand All @@ -105,6 +109,20 @@ internal class HistoryEventMapper {
}
}

private fun addWaypointNames(response: String): String {
val gson = GsonBuilder().create()
val jsonObject = gson.fromJson(response, JsonObject::class.java)
val waypoints = jsonObject?.getAsJsonArray(WAYPOINTS_JSON_KEY)
val isNameNotIncluded = waypoints?.get(0)?.asJsonObject?.get(NAME_JSON_KEY) == null
if (isNameNotIncluded) {
waypoints?.forEachIndexed { index, _ ->
jsonObject.getAsJsonArray(WAYPOINTS_JSON_KEY)
.get(index).asJsonObject.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 +154,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"
}
}

0 comments on commit b4bfddf

Please sign in to comment.