Skip to content

Commit

Permalink
Fix "SetRoutesReason" when alternative route is chosen as a primary. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DzmitryFomchyn authored Sep 22, 2023
1 parent 9643076 commit 7f1c98b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,12 @@ class MapboxNavigation @VisibleForTesting internal constructor(
// Telemetry uses this field to determine what type of event should be triggered.
val setRoutesInfo = when {
routes.isEmpty() -> SetRoutes.CleanUp
routes.first() == directionsSession.routes.firstOrNull() ->
routes.first() == directionsSession.routes.firstOrNull() -> {
SetRoutes.Alternatives(initialLegIndex)
}
directionsSession.routes.map { it.id }.contains(routes.first().id) -> {
SetRoutes.Reorder(initialLegIndex)
}
else -> SetRoutes.NewRoutes(initialLegIndex)
}
internalSetNavigationRoutes(
Expand Down Expand Up @@ -1079,7 +1083,8 @@ class MapboxNavigation @VisibleForTesting internal constructor(
when (setRoutesInfo) {
SetRoutes.CleanUp,
is SetRoutes.NewRoutes,
is SetRoutes.Reroute -> {
is SetRoutes.Reroute,
is SetRoutes.Reorder -> {
rerouteController?.interrupt()
}
is SetRoutes.RefreshRoutes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ internal sealed class SetRoutes {
val legIndex: Int,
) : SetRoutes()

/**
* Triggered when primary route changed and previous primary became alternative.
*/
internal data class Reorder(val legIndex: Int) : SetRoutes()

/**
* Triggered when the **routes do not change but are refreshed**.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object RoutesExtra {
const val ROUTES_UPDATE_REASON_CLEAN_UP = "ROUTES_UPDATE_REASON_CLEAN_UP"

/**
* Routes update reason is *new routes**.
* Routes update reason is **new routes**.
* @see [RoutesObserver]
*/
const val ROUTES_UPDATE_REASON_NEW = "ROUTES_UPDATE_REASON_NEW"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ internal fun SetRoutes.mapToReason(): String =
when (this) {
is SetRoutes.Alternatives -> RoutesExtra.ROUTES_UPDATE_REASON_ALTERNATIVE
SetRoutes.CleanUp -> RoutesExtra.ROUTES_UPDATE_REASON_CLEAN_UP
is SetRoutes.NewRoutes -> RoutesExtra.ROUTES_UPDATE_REASON_NEW
is SetRoutes.NewRoutes,
is SetRoutes.Reorder -> RoutesExtra.ROUTES_UPDATE_REASON_NEW
is SetRoutes.RefreshRoutes -> RoutesExtra.ROUTES_UPDATE_REASON_REFRESH
is SetRoutes.Reroute -> RoutesExtra.ROUTES_UPDATE_REASON_REROUTE
}

internal fun SetRoutes.initialLegIndex(): Int =
when (this) {
is SetRoutes.Alternatives -> legIndex
is SetRoutes.Reorder -> legIndex
SetRoutes.CleanUp -> MapboxDirectionsSession.DEFAULT_INITIAL_LEG_INDEX
is SetRoutes.NewRoutes -> legIndex
is SetRoutes.RefreshRoutes -> routeProgressData.legIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ internal class MapboxTripSession(
nativeAlternatives = navigator.setAlternativeRoutes(routes.drop(1))
)
}
is SetRoutes.Reorder -> {
setRouteToNativeNavigator(
routes,
setRoutes.initialLegIndex(),
SetRoutesReason.ALTERNATIVE
)
}
is SetRoutes.RefreshRoutes -> {
if (routes.isNotEmpty()) {
val primaryRoute = routes.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,30 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
}
}

@Test
fun `setNavigationRoutes alternative for changed primary route`() = coroutineRule.runBlockingTest {
createMapboxNavigation()
val route1 = mockk<NavigationRoute>(relaxed = true) {
every { id } returns "id1"
}
val route2 = mockk<NavigationRoute>(relaxed = true) {
every { id } returns "id2"
}
val route3 = mockk<NavigationRoute>(relaxed = true) {
every { id } returns "id3"
}
every { directionsSession.routes } returns listOf(route1, route2, route3)

mapboxNavigation.setNavigationRoutes(listOf(route2, route1, route3))

coVerify(exactly = 1) {
tripSession.setRoutes(any(), eq(SetRoutes.Reorder(0)))
}
verify(exactly = 1) {
directionsSession.setNavigationRoutesFinished(any())
}
}

@Test
fun `setNavigationRoutes new routes`() = coroutineRule.runBlockingTest {
createMapboxNavigation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class SetRoutesLegIndexTest(
arrayOf(SetRoutes.RefreshRoutes(RouteProgressData(1, 2, 3)), 1),
arrayOf(SetRoutes.Reroute(5), 5),
arrayOf(SetRoutes.Alternatives(6), 6),
arrayOf(SetRoutes.Reorder(7), 7),
).also {
assertEquals(SetRoutes::class.sealedSubclasses.size, it.size)
}
Expand Down

0 comments on commit 7f1c98b

Please sign in to comment.