-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GH] [Navigation] Introduce NavBackStackEntry#provideToCompositionLocals
…that provides the NavBackStackEntry to the composition locals needed for all Compose-related navigators. Relnote: Introduced a new NavBackStackEntry#provideToCompositionLocals that provides the NavBackStackEntry to the relevant composition locals. ## Testing Test: NavBackStackEntryProviderTest `NavBackStackEntryProviderTest#testSaveableValueInContentIsSaved` is pretty much just copied from [RememberSaveableTest](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/runtime/runtime-saveable/src/androidAndroidTest/kotlin/androidx/compose/runtime/saveable/RememberSaveableTest.kt) and as always, I'll be very happy about suggestions on improving the tests and additional tests! ## Issues Fixed Fixes: b/187229439 This is an imported pull request from #175. Resolves #175 Github-Pr-Head-Sha: a8100a0 GitOrigin-RevId: 7441a30 Change-Id: I25a46dfefc52adc1411536359a285bfbc4fb2e38
- Loading branch information
1 parent
6423af5
commit a8c1f68
Showing
6 changed files
with
256 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...compose/src/androidTest/java/androidx/navigation/compose/NavBackStackEntryProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Copyright 2021 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.navigation.compose | ||
|
||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.saveable.rememberSaveableStateHolder | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner | ||
import androidx.compose.ui.test.junit4.StateRestorationTester | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner | ||
import androidx.navigation.NavBackStackEntry | ||
import androidx.navigation.testing.TestNavigatorState | ||
import androidx.savedstate.SavedStateRegistryOwner | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.filters.LargeTest | ||
import androidx.testutils.TestNavigator | ||
import com.google.common.truth.Truth.assertThat | ||
import com.google.common.truth.Truth.assertWithMessage | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@LargeTest | ||
@RunWith(AndroidJUnit4::class) | ||
class NavBackStackEntryProviderTest { | ||
|
||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun testViewModelStoreOwnerProvided() { | ||
val backStackEntry = createBackStackEntry() | ||
var viewModelStoreOwner: ViewModelStoreOwner? = null | ||
|
||
composeTestRule.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.LocalOwnersProvider(saveableStateHolder) { | ||
viewModelStoreOwner = LocalViewModelStoreOwner.current | ||
} | ||
} | ||
|
||
assertWithMessage("ViewModelStoreOwner is provided by $backStackEntry") | ||
.that(viewModelStoreOwner).isEqualTo(backStackEntry) | ||
} | ||
|
||
@Test | ||
fun testLifecycleOwnerProvided() { | ||
val backStackEntry = createBackStackEntry() | ||
var lifecycleOwner: LifecycleOwner? = null | ||
|
||
composeTestRule.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.LocalOwnersProvider(saveableStateHolder) { | ||
lifecycleOwner = LocalLifecycleOwner.current | ||
} | ||
} | ||
|
||
assertWithMessage("LifecycleOwner is provided by $backStackEntry") | ||
.that(lifecycleOwner).isEqualTo(backStackEntry) | ||
} | ||
|
||
@Test | ||
fun testLocalSavedStateRegistryOwnerProvided() { | ||
val backStackEntry = createBackStackEntry() | ||
var localSavedStateRegistryOwner: SavedStateRegistryOwner? = null | ||
|
||
composeTestRule.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.LocalOwnersProvider(saveableStateHolder) { | ||
localSavedStateRegistryOwner = LocalSavedStateRegistryOwner.current | ||
} | ||
} | ||
|
||
assertWithMessage("LocalSavedStateRegistryOwner is provided by $backStackEntry") | ||
.that(localSavedStateRegistryOwner).isEqualTo(backStackEntry) | ||
} | ||
|
||
@Test | ||
fun testSaveableValueInContentIsSaved() { | ||
val backStackEntry = createBackStackEntry() | ||
val restorationTester = StateRestorationTester(composeTestRule) | ||
var array: IntArray? = null | ||
|
||
restorationTester.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.LocalOwnersProvider(saveableStateHolder) { | ||
array = rememberSaveable { | ||
intArrayOf(0) | ||
} | ||
} | ||
} | ||
|
||
assertThat(array).isEqualTo(intArrayOf(0)) | ||
|
||
composeTestRule.runOnUiThread { | ||
array!![0] = 1 | ||
// we null it to ensure recomposition happened | ||
array = null | ||
} | ||
|
||
restorationTester.emulateSavedInstanceStateRestore() | ||
|
||
assertThat(array).isEqualTo(intArrayOf(1)) | ||
} | ||
|
||
@Test | ||
fun testNonSaveableValueInContentIsNotSaved() { | ||
val backStackEntry = createBackStackEntry() | ||
val restorationTester = StateRestorationTester(composeTestRule) | ||
var nonSaveable: IntArray? = null | ||
val initialValue = intArrayOf(10) | ||
|
||
restorationTester.setContent { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
backStackEntry.LocalOwnersProvider(saveableStateHolder) { | ||
nonSaveable = remember { initialValue } | ||
} | ||
} | ||
|
||
assertThat(nonSaveable).isEqualTo(initialValue) | ||
|
||
composeTestRule.runOnUiThread { | ||
nonSaveable!![0] = 1 | ||
// we null it to ensure recomposition happened | ||
nonSaveable = null | ||
} | ||
|
||
restorationTester.emulateSavedInstanceStateRestore() | ||
|
||
assertThat(nonSaveable).isEqualTo(initialValue) | ||
} | ||
|
||
private fun createBackStackEntry(): NavBackStackEntry { | ||
val testNavigator = TestNavigator() | ||
val testNavigatorState = TestNavigatorState() | ||
testNavigator.onAttach(testNavigatorState) | ||
val backStackEntry = testNavigatorState.createBackStackEntry( | ||
testNavigator.createDestination(), | ||
null | ||
) | ||
// We navigate to move the NavBackStackEntry to the correct state | ||
testNavigator.navigate(listOf(backStackEntry), null, null) | ||
return backStackEntry | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...navigation-compose/src/main/java/androidx/navigation/compose/NavBackStackEntryProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2021 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.navigation.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.saveable.SaveableStateHolder | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.NavBackStackEntry | ||
import java.util.UUID | ||
|
||
/** | ||
* Provides [this] [NavBackStackEntry] as [LocalViewModelStoreOwner], [LocalLifecycleOwner] and | ||
* [LocalSavedStateRegistryOwner] to the [content] and saves the [content]'s saveable states with | ||
* the given [saveableStateHolder]. | ||
* | ||
* @param saveableStateHolder The [SaveableStateHolder] that holds the saved states. The same | ||
* holder should be used for all [NavBackStackEntry]s in the encapsulating [Composable] and the | ||
* holder should be hoisted. | ||
* @param content The content [Composable] | ||
*/ | ||
@Composable | ||
public fun NavBackStackEntry.LocalOwnersProvider( | ||
saveableStateHolder: SaveableStateHolder, | ||
content: @Composable () -> Unit | ||
) { | ||
CompositionLocalProvider( | ||
LocalViewModelStoreOwner provides this, | ||
LocalLifecycleOwner provides this, | ||
LocalSavedStateRegistryOwner provides this | ||
) { | ||
saveableStateHolder.SaveableStateProvider(content) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun SaveableStateHolder.SaveableStateProvider(content: @Composable () -> Unit) { | ||
val viewModel = viewModel<BackStackEntryIdViewModel>() | ||
viewModel.saveableStateHolder = this | ||
SaveableStateProvider(viewModel.id, content) | ||
} | ||
|
||
internal class BackStackEntryIdViewModel(handle: SavedStateHandle) : ViewModel() { | ||
|
||
private val IdKey = "SaveableStateHolder_BackStackEntryKey" | ||
|
||
// we create our own id for each back stack entry to support multiple entries of the same | ||
// destination. this id will be restored by SavedStateHandle | ||
val id: UUID = handle.get<UUID>(IdKey) ?: UUID.randomUUID().also { handle.set(IdKey, it) } | ||
|
||
var saveableStateHolder: SaveableStateHolder? = null | ||
|
||
// onCleared will be called on the entries removed from the back stack. here we notify | ||
// RestorableStateHolder that we shouldn't save the state for this id, so when we open this | ||
// destination again the state will not be restored. | ||
override fun onCleared() { | ||
super.onCleared() | ||
saveableStateHolder?.removeState(id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters