Skip to content

Commit 562e8e7

Browse files
author
Łukasz Paczos
committed
migrate to Navigator.resetRideSession with callback
1 parent 0c51c9a commit 562e8e7

File tree

9 files changed

+125
-17
lines changed

9 files changed

+125
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Mapbox welcomes participation and contributions from everyone.
77
#### Bug fixes and improvements
88
- Fixed an issue where `RouteProgress#BannerInstructions` could've become `null` when `MapboxNavigation#updateLegIndex` was called. [#6684](https://github.com/mapbox/mapbox-navigation-android/pull/6684)
99
- Fixed an issue where `RouteProgress#VoiceInstructions` could've become `null` when `MapboxNavigation#updateLegIndex` was called. [#6689](https://github.com/mapbox/mapbox-navigation-android/pull/6689)
10+
- Fixed `BannerInstructions` issue where the banner instruction might have been removed from `RouteProgress` at some point around a edge's leg. [#6684](https://github.com/mapbox/mapbox-navigation-android/pull/6684)
11+
- Added `MapboxNavigation#resetTripSession(callback)` and deprecated the counterpart without a callback. [#6685](https://github.com/mapbox/mapbox-navigation-android/pull/6685)
1012

1113
## Mapbox Navigation SDK 2.10.0-beta.2 - 01 December, 2022
1214
### Changelog
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mapbox.navigation.instrumentation_tests.core
2+
3+
import android.location.Location
4+
import androidx.test.espresso.Espresso
5+
import com.mapbox.navigation.base.options.NavigationOptions
6+
import com.mapbox.navigation.core.MapboxNavigation
7+
import com.mapbox.navigation.core.MapboxNavigationProvider
8+
import com.mapbox.navigation.instrumentation_tests.activity.EmptyTestActivity
9+
import com.mapbox.navigation.instrumentation_tests.utils.MapboxNavigationRule
10+
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.resetTripSessionAndWaitForResult
11+
import com.mapbox.navigation.instrumentation_tests.utils.coroutines.sdkTest
12+
import com.mapbox.navigation.testing.ui.BaseTest
13+
import com.mapbox.navigation.testing.ui.utils.getMapboxAccessTokenFromResources
14+
import com.mapbox.navigation.testing.ui.utils.runOnMainSync
15+
import org.junit.Before
16+
import org.junit.Rule
17+
import org.junit.Test
18+
19+
class MapboxNavigationTest : BaseTest<EmptyTestActivity>(EmptyTestActivity::class.java) {
20+
21+
@get:Rule
22+
val mapboxNavigationRule = MapboxNavigationRule()
23+
24+
private lateinit var mapboxNavigation: MapboxNavigation
25+
26+
override fun setupMockLocation(): Location = mockLocationUpdatesRule.generateLocationUpdate {
27+
latitude = 38.894721
28+
longitude = -77.031991
29+
}
30+
31+
@Before
32+
fun setup() {
33+
Espresso.onIdle()
34+
35+
runOnMainSync {
36+
mapboxNavigation = MapboxNavigationProvider.create(
37+
NavigationOptions.Builder(activity)
38+
.accessToken(getMapboxAccessTokenFromResources(activity))
39+
.build()
40+
)
41+
}
42+
}
43+
44+
@Test
45+
fun trip_session_resets_successfully() = sdkTest {
46+
mapboxNavigation.resetTripSessionAndWaitForResult()
47+
}
48+
}

instrumentation-tests/src/androidTest/java/com/mapbox/navigation/instrumentation_tests/utils/coroutines/TestUtils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ private suspend fun MapboxNavigation.waitForRoutesUpdate(
116116
.first()
117117
}
118118

119+
suspend fun MapboxNavigation.resetTripSessionAndWaitForResult() =
120+
suspendCancellableCoroutine<Unit> { cont ->
121+
resetTripSession { cont.resume(Unit) }
122+
}
123+
119124
inline fun <T> withLogOnTimeout(message: String, body: () -> T): T {
120125
try {
121126
return body()

libnavigation-core/api/current.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ package com.mapbox.navigation.core {
6565
method public void requestRoadGraphDataUpdate(com.mapbox.navigation.core.RoadGraphDataUpdateCallback callback);
6666
method @Deprecated public long requestRoutes(com.mapbox.api.directions.v5.models.RouteOptions routeOptions, com.mapbox.navigation.base.route.RouterCallback routesRequestCallback);
6767
method public long requestRoutes(com.mapbox.api.directions.v5.models.RouteOptions routeOptions, com.mapbox.navigation.base.route.NavigationRouterCallback callback);
68-
method public void resetTripSession();
68+
method @Deprecated public void resetTripSession();
69+
method public void resetTripSession(com.mapbox.navigation.core.TripSessionResetCallback callback);
6970
method public void setArrivalController(com.mapbox.navigation.core.arrival.ArrivalController? arrivalController = com.mapbox.navigation.core.arrival.AutoArrivalController());
7071
method public void setArrivalController();
7172
method public void setNavigationRoutes(java.util.List<com.mapbox.navigation.base.route.NavigationRoute> routes, int initialLegIndex = 0, com.mapbox.navigation.core.RoutesSetCallback? callback = null);
@@ -153,6 +154,10 @@ package com.mapbox.navigation.core {
153154
property public final java.util.Map<java.lang.String,com.mapbox.navigation.core.RoutesSetError> ignoredAlternatives;
154155
}
155156

157+
public fun interface TripSessionResetCallback {
158+
method public void onTripSessionReset();
159+
}
160+
156161
}
157162

158163
package com.mapbox.navigation.core.arrival {
@@ -777,13 +782,13 @@ package com.mapbox.navigation.core.routealternatives {
777782
}
778783

779784
public final class AlternativeRouteMetadata {
780-
method public int getAlternativeId();
785+
method @Deprecated public int getAlternativeId();
781786
method public com.mapbox.navigation.core.routealternatives.AlternativeRouteIntersection getForkIntersectionOfAlternativeRoute();
782787
method public com.mapbox.navigation.core.routealternatives.AlternativeRouteIntersection getForkIntersectionOfPrimaryRoute();
783788
method public com.mapbox.navigation.core.routealternatives.AlternativeRouteInfo getInfoFromFork();
784789
method public com.mapbox.navigation.core.routealternatives.AlternativeRouteInfo getInfoFromStartOfPrimary();
785790
method public com.mapbox.navigation.base.route.NavigationRoute getNavigationRoute();
786-
property public final int alternativeId;
791+
property @Deprecated public final int alternativeId;
787792
property public final com.mapbox.navigation.core.routealternatives.AlternativeRouteIntersection forkIntersectionOfAlternativeRoute;
788793
property public final com.mapbox.navigation.core.routealternatives.AlternativeRouteIntersection forkIntersectionOfPrimaryRoute;
789794
property public final com.mapbox.navigation.core.routealternatives.AlternativeRouteInfo infoFromFork;
@@ -826,6 +831,9 @@ package com.mapbox.navigation.core.routealternatives {
826831

827832
package com.mapbox.navigation.core.routeoptions {
828833

834+
public final class RouteOptionsExKt {
835+
}
836+
829837
public final class RouteOptionsUpdater {
830838
ctor public RouteOptionsUpdater();
831839
method public com.mapbox.navigation.core.routeoptions.RouteOptionsUpdater.RouteOptionsResult update(com.mapbox.api.directions.v5.models.RouteOptions? routeOptions, com.mapbox.navigation.base.trip.model.RouteProgress? routeProgress, com.mapbox.navigation.core.trip.session.LocationMatcherResult? locationMatcherResult);

libnavigation-core/src/main/java/com/mapbox/navigation/core/MapboxNavigation.kt

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ import com.mapbox.navigation.utils.internal.ThreadController
125125
import com.mapbox.navigation.utils.internal.ifNonNull
126126
import com.mapbox.navigation.utils.internal.logD
127127
import com.mapbox.navigation.utils.internal.logE
128+
import com.mapbox.navigation.utils.internal.logI
128129
import com.mapbox.navigation.utils.internal.monitorChannelWithException
129130
import com.mapbox.navigator.AlertsServiceOptions
130131
import com.mapbox.navigator.ConfigHandle
@@ -638,11 +639,38 @@ class MapboxNavigation @VisibleForTesting internal constructor(
638639

639640
/**
640641
* Reset the session with the same configuration. The location becomes unknown,
641-
* but the [NavigationOptions] stay the same. This can be used to transport the
642-
* navigator to a new location.
642+
* but the [NavigationOptions] stay the same.
643+
*
644+
* Call this function before significant change of location, e.g. when restarting
645+
* navigation simulation, or before resetting location to not real (simulated)
646+
* position without recreation of [MapboxNavigation].
643647
*/
648+
@Deprecated(message = "use a function withe the callback instead")
644649
fun resetTripSession() {
645-
navigator.resetRideSession()
650+
resetTripSession {
651+
// no-op
652+
}
653+
}
654+
655+
/**
656+
* Reset the session with the same configuration. The location becomes unknown,
657+
* but the [NavigationOptions] stay the same.
658+
*
659+
* Call this function before significant change of location, e.g. when restarting
660+
* navigation simulation, or before resetting location to not real (simulated)
661+
* position without recreation of [MapboxNavigation].
662+
*/
663+
fun resetTripSession(callback: TripSessionResetCallback) {
664+
logD(LOG_CATEGORY) {
665+
"Resetting trip session"
666+
}
667+
mainJobController.scope.launch(Dispatchers.Main.immediate) {
668+
navigator.resetRideSession()
669+
logI(LOG_CATEGORY) {
670+
"Trip session reset"
671+
}
672+
callback.onTripSessionReset()
673+
}
646674
}
647675

648676
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mapbox.navigation.core
2+
3+
/**
4+
* Callback invoked when [MapboxNavigation.resetTripSession] finishes.
5+
*/
6+
fun interface TripSessionResetCallback {
7+
/**
8+
* Invoked when resetting the trip finishes and the matching history is removed
9+
* which can be helpful to transition the user to new location in simulated environments.
10+
*/
11+
fun onTripSessionReset()
12+
}

libnavigation-core/src/test/java/com/mapbox/navigation/core/MapboxNavigationTest.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
333333
createMapboxNavigation()
334334
mapboxNavigation.onDestroy()
335335

336-
verify(exactly = 1) { navigator.resetRideSession() }
336+
coVerify(exactly = 1) { navigator.resetRideSession() }
337337
}
338338

339339
@Test
@@ -770,7 +770,16 @@ internal class MapboxNavigationTest : MapboxNavigationBaseTest() {
770770
createMapboxNavigation()
771771
mapboxNavigation.resetTripSession()
772772

773-
verify { navigator.resetRideSession() }
773+
coVerify { navigator.resetRideSession() }
774+
}
775+
776+
@Test
777+
fun `resetTripSession should reset the navigator and call back`() {
778+
createMapboxNavigation()
779+
val callback = mockk<TripSessionResetCallback>(relaxUnitFun = true)
780+
mapboxNavigation.resetTripSession(callback)
781+
782+
verify { callback.onTripSessionReset() }
774783
}
775784

776785
@Test

libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigator.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import com.mapbox.navigator.FixLocation
1717
import com.mapbox.navigator.GraphAccessor
1818
import com.mapbox.navigator.HistoryRecorderHandle
1919
import com.mapbox.navigator.NavigationStatus
20-
import com.mapbox.navigator.NavigatorConfig
2120
import com.mapbox.navigator.NavigatorObserver
2221
import com.mapbox.navigator.PredictiveCacheController
2322
import com.mapbox.navigator.RoadObjectMatcher
@@ -57,12 +56,7 @@ interface MapboxNativeNavigator {
5756
router: RouterInterface,
5857
)
5958

60-
/**
61-
* Reset the navigator state with the same configuration. The location becomes unknown,
62-
* but the [NavigatorConfig] stays the same. This can be used to transport the
63-
* navigator to a new location.
64-
*/
65-
fun resetRideSession()
59+
suspend fun resetRideSession()
6660

6761
// Route following
6862

libnavigator/src/main/java/com/mapbox/navigation/navigator/internal/MapboxNativeNavigatorImpl.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ object MapboxNativeNavigatorImpl : MapboxNativeNavigator {
121121
}
122122
}
123123

124-
override fun resetRideSession() {
125-
navigator!!.resetRideSession()
124+
override suspend fun resetRideSession() = suspendCancellableCoroutine<Unit> {
125+
navigator!!.reset {
126+
it.resume(Unit)
127+
}
126128
}
127129

128130
/**

0 commit comments

Comments
 (0)