-
Notifications
You must be signed in to change notification settings - Fork 318
/
RouteComparator.kt
50 lines (41 loc) · 1.83 KB
/
RouteComparator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.mapbox.navigation.core.fasterroute
import com.mapbox.api.directions.v5.models.DirectionsRoute
import com.mapbox.api.directions.v5.models.LegStep
import com.mapbox.navigation.base.trip.model.RouteProgress
/**
* Compares if an alternative route is different from the current route progress.
*/
internal class RouteComparator {
private val mapLegStepToName: (LegStep) -> String = { it.name() ?: "" }
/**
* @param routeProgress current route progress
* @param alternativeRoute suggested new route
*
* @return true when the alternative route has different
* geometry from the current route progress
*/
fun isNewRoute(routeProgress: RouteProgress, alternativeRoute: DirectionsRoute): Boolean {
val currentDescription = routeDescription(routeProgress)
val alternativeDescription = alternativeDescription(alternativeRoute)
alternativeDescription.ifEmpty {
return false
}
return isNewRoute(currentDescription, alternativeDescription)
}
private fun routeDescription(routeProgress: RouteProgress): String {
val routeLeg = routeProgress.currentLegProgress?.routeLeg
val steps = routeLeg?.steps()
val stepIndex = routeProgress.currentLegProgress?.currentStepProgress?.stepIndex
?: return ""
return steps?.listIterator(stepIndex)?.asSequence()
?.joinToString(transform = mapLegStepToName) ?: ""
}
private fun alternativeDescription(directionsRoute: DirectionsRoute): String {
val steps = directionsRoute.legs()?.firstOrNull()?.steps()
return steps?.asSequence()
?.joinToString(transform = mapLegStepToName) ?: ""
}
private fun isNewRoute(currentGeometry: String, alternativeGeometry: String): Boolean {
return currentGeometry != alternativeGeometry
}
}