Skip to content

Commit ee9c56b

Browse files
committed
Add ability to check configuration changing
1 parent 6b92019 commit ee9c56b

File tree

6 files changed

+141
-50
lines changed

6 files changed

+141
-50
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ This release depends on, and has been tested with, the following Mapbox dependen
584584
- Mapbox Core Common `v23.1.0-rc.2`
585585
- Mapbox Java `v6.8.0` ([release notes](https://github.com/mapbox/mapbox-java/releases/tag/v6.8.0))
586586
- Mapbox Android Core `v5.0.2` ([release notes](https://github.com/mapbox/mapbox-events-android/releases/tag/core-5.0.2))
587+
- Add ability to check when `NavigationOptions` are changing with `MapboxNavigationApp.isOptionsChanging`. [#6484](https://github.com/mapbox/mapbox-navigation-android/pull/6484)
588+
- Add ability to check when Lifecycle events are triggered by configuration changes with `MapboxNavigationApp.isConfigurationChanging`. [#6484](https://github.com/mapbox/mapbox-navigation-android/pull/6484)
589+
- Added ability to check when `NavigationOptions` are changing with `MapboxNavigationApp.isOptionsChanging`. [#6484](https://github.com/mapbox/mapbox-navigation-android/pull/6484)
590+
- Added ability to check when Lifecycle events are triggered by configuration changes with `MapboxNavigationApp.isConfigurationChanging`. [#6484](https://github.com/mapbox/mapbox-navigation-android/pull/6484)
587591

588592
## Mapbox Navigation SDK 2.9.0-beta.2 - 14 October, 2022
589593
### Changelog

libnavigation-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ dependencies {
6161
testImplementation project(':libtesting-utils')
6262
testImplementation project(':libtesting-navigation-base')
6363
testImplementation project(':libtesting-navigation-util')
64+
testImplementation(dependenciesList.androidXLifecycleTesting)
6465
apply from: "${rootDir}/gradle/unit-testing-dependencies.gradle"
6566
testImplementation dependenciesList.commonsIO
6667

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ object MapboxNavigationApp {
7777
@JvmStatic
7878
fun isSetup(): Boolean = mapboxNavigationAppDelegate.isSetup
7979

80+
/**
81+
* When calling [setup] multiple times, all registered [MapboxNavigationObserver] will be
82+
* detached from the instance of [MapboxNavigation] that is being destroyed. This is needed for
83+
* maintaining state across options changes. You do not need to clear the state when
84+
* [isOptionsChanging] is true.
85+
*/
86+
@UiThread
87+
@JvmStatic
88+
fun isOptionsChanging(): Boolean = mapboxNavigationAppDelegate.isOptionsChanging
89+
90+
/**
91+
* When observing [MapboxNavigation] from a [Lifecycle], the [Lifecycle.Event] may be triggered
92+
* because of a configuration change. This will help you know if the lifecycle events are
93+
* triggered events like mobile device orientation changes.
94+
*/
95+
@UiThread
96+
@JvmStatic
97+
fun isConfigurationChanging(): Boolean = mapboxNavigationAppDelegate.isConfigurationChanging()
98+
8099
/**
81100
* Call [MapboxNavigationApp.setup] to provide the application with [NavigationOptions].
82101
*

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,31 @@ internal class MapboxNavigationAppDelegate {
1616

1717
val lifecycleOwner: LifecycleOwner by lazy { carAppLifecycleOwner }
1818

19+
var isOptionsChanging = false
20+
private set
21+
1922
var isSetup = false
23+
private set
2024

2125
fun setup(navigationOptionsProvider: NavigationOptionsProvider) = apply {
2226
if (carAppLifecycleOwner.isConfigurationChanging()) {
2327
return this
2428
}
2529

2630
if (isSetup) {
31+
isOptionsChanging = true
2732
disable()
2833
}
29-
3034
mapboxNavigationOwner.setup(navigationOptionsProvider)
3135
carAppLifecycleOwner.lifecycle.addObserver(mapboxNavigationOwner.carAppLifecycleObserver)
36+
isOptionsChanging = false
3237
isSetup = true
3338
}
3439

40+
fun isConfigurationChanging(): Boolean {
41+
return carAppLifecycleOwner.isConfigurationChanging()
42+
}
43+
3544
fun attachAllActivities(application: Application) {
3645
carAppLifecycleOwner.attachAllActivities(application)
3746
}

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import androidx.fragment.app.Fragment
88
import androidx.fragment.app.FragmentActivity
99
import androidx.lifecycle.DefaultLifecycleObserver
1010
import androidx.lifecycle.Lifecycle
11-
import androidx.lifecycle.LifecycleOwner
12-
import androidx.lifecycle.LifecycleRegistry
11+
import androidx.lifecycle.testing.TestLifecycleOwner
1312
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
1413
import com.mapbox.navigation.testing.LoggingFrontendTestRule
1514
import io.mockk.every
@@ -405,8 +404,8 @@ class CarAppLifecycleOwnerTest {
405404
val testLifecycleOwner = TestLifecycleOwner()
406405
carAppLifecycleOwner.attach(testLifecycleOwner)
407406

408-
testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
409-
testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
407+
testLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
408+
testLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
410409

411410
verify(exactly = 1) { testLifecycleObserver.onCreate(any()) }
412411
verify(exactly = 1) { testLifecycleObserver.onStart(any()) }
@@ -422,10 +421,10 @@ class CarAppLifecycleOwnerTest {
422421
carAppLifecycleOwner.attach(testLifecycleOwnerA)
423422
carAppLifecycleOwner.attach(testLifecycleOwnerB)
424423

425-
testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
426-
testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
427-
testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
428-
testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
424+
testLifecycleOwnerA.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
425+
testLifecycleOwnerB.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
426+
testLifecycleOwnerA.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
427+
testLifecycleOwnerB.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
429428

430429
verify(exactly = 1) { testLifecycleObserver.onCreate(any()) }
431430
verify(exactly = 1) { testLifecycleObserver.onStart(any()) }
@@ -441,10 +440,10 @@ class CarAppLifecycleOwnerTest {
441440
carAppLifecycleOwner.attach(testLifecycleOwnerA)
442441
carAppLifecycleOwner.attach(testLifecycleOwnerB)
443442

444-
testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
445-
testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle.State.STARTED
446-
testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
447-
testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
443+
testLifecycleOwnerA.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
444+
testLifecycleOwnerB.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
445+
testLifecycleOwnerA.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
446+
testLifecycleOwnerB.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
448447

449448
verify(exactly = 1) { testLifecycleObserver.onCreate(any()) }
450449
verify(exactly = 1) { testLifecycleObserver.onStart(any()) }
@@ -458,7 +457,7 @@ class CarAppLifecycleOwnerTest {
458457
val testLifecycleOwner = TestLifecycleOwner()
459458

460459
carAppLifecycleOwner.attach(testLifecycleOwner)
461-
testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
460+
testLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
462461
carAppLifecycleOwner.detach(testLifecycleOwner)
463462

464463
verify(exactly = 1) { testLifecycleObserver.onCreate(any()) }
@@ -468,13 +467,6 @@ class CarAppLifecycleOwnerTest {
468467
verify(exactly = 0) { testLifecycleObserver.onDestroy(any()) }
469468
}
470469

471-
class TestLifecycleOwner : LifecycleOwner {
472-
val lifecycleRegistry = LifecycleRegistry(this)
473-
.also { it.currentState = Lifecycle.State.INITIALIZED }
474-
475-
override fun getLifecycle(): Lifecycle = lifecycleRegistry
476-
}
477-
478470
private fun mockActivity(isChangingConfig: Boolean = false): FragmentActivity = mockk {
479471
every { isChangingConfigurations } returns isChangingConfig
480472
}

0 commit comments

Comments
 (0)