Skip to content

Commit cfce763

Browse files
Update Nav3 to beta01 (#672)
* Update Nav3 to beta01 * Apply Spotless
1 parent e799446 commit cfce763

File tree

6 files changed

+30
-37
lines changed

6 files changed

+30
-37
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/navigation3/animations/AnimationSnippets.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import androidx.compose.material3.Scaffold
3333
import androidx.compose.material3.Text
3434
import androidx.compose.ui.Modifier
3535
import androidx.navigation3.runtime.NavKey
36-
import androidx.navigation3.runtime.entry
3736
import androidx.navigation3.runtime.entryProvider
3837
import androidx.navigation3.runtime.rememberNavBackStack
3938
import androidx.navigation3.ui.NavDisplay

compose/snippets/src/main/java/com/example/compose/snippets/navigation3/basic/BasicSnippets.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import androidx.compose.runtime.Composable
2222
import androidx.compose.runtime.mutableStateListOf
2323
import androidx.compose.runtime.remember
2424
import androidx.navigation3.runtime.NavEntry
25-
import androidx.navigation3.runtime.entry
2625
import androidx.navigation3.runtime.entryProvider
2726
import androidx.navigation3.ui.NavDisplay
2827
import com.example.compose.snippets.navigation3.ContentBlue

compose/snippets/src/main/java/com/example/compose/snippets/navigation3/savingstate/SavingStateSnippets.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDe
2121
import androidx.navigation3.runtime.NavKey
2222
import androidx.navigation3.runtime.entryProvider
2323
import androidx.navigation3.runtime.rememberNavBackStack
24-
import androidx.navigation3.runtime.rememberSavedStateNavEntryDecorator
24+
import androidx.navigation3.runtime.rememberSaveableStateHolderNavEntryDecorator
2525
import androidx.navigation3.ui.NavDisplay
26-
import androidx.navigation3.ui.rememberSceneSetupNavEntryDecorator
2726
import kotlinx.serialization.Serializable
2827

2928
// [START android_compose_navigation3_savingstate_1]
@@ -45,8 +44,7 @@ fun ScopingViewModels() {
4544
NavDisplay(
4645
entryDecorators = listOf(
4746
// Add the default decorators for managing scenes and saving state
48-
rememberSceneSetupNavEntryDecorator(),
49-
rememberSavedStateNavEntryDecorator(),
47+
rememberSaveableStateHolderNavEntryDecorator(),
5048
// Then add the view model store decorator
5149
rememberViewModelStoreNavEntryDecorator()
5250
),

compose/snippets/src/main/java/com/example/compose/snippets/navigation3/scenes/ScenesSnippets.kt

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ import androidx.compose.foundation.layout.Row
2121
import androidx.compose.foundation.layout.fillMaxSize
2222
import androidx.compose.material3.Button
2323
import androidx.compose.material3.Text
24-
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
2524
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
26-
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
2725
import androidx.compose.runtime.Composable
26+
import androidx.compose.runtime.remember
2827
import androidx.compose.ui.Modifier
2928
import androidx.navigation3.runtime.NavEntry
3029
import androidx.navigation3.runtime.NavKey
31-
import androidx.navigation3.runtime.entry
3230
import androidx.navigation3.runtime.entryProvider
3331
import androidx.navigation3.runtime.rememberNavBackStack
32+
import androidx.navigation3.scene.Scene
33+
import androidx.navigation3.scene.SceneStrategy
34+
import androidx.navigation3.scene.SceneStrategyScope
3435
import androidx.navigation3.ui.NavDisplay
35-
import androidx.navigation3.ui.Scene
36-
import androidx.navigation3.ui.SceneStrategy
36+
import androidx.window.core.layout.WindowSizeClass
3737
import androidx.window.core.layout.WindowSizeClass.Companion.WIDTH_DP_MEDIUM_LOWER_BOUND
3838
import kotlinx.serialization.Serializable
3939

@@ -63,8 +63,7 @@ data class SinglePaneScene<T : Any>(
6363
* list.
6464
*/
6565
public class SinglePaneSceneStrategy<T : Any> : SceneStrategy<T> {
66-
@Composable
67-
override fun calculateScene(entries: List<NavEntry<T>>, onBack: (Int) -> Unit): Scene<T> =
66+
override fun SceneStrategyScope<T>.calculateScene(entries: List<NavEntry<T>>): Scene<T>? =
6867
SinglePaneScene(
6968
key = entries.last().contentKey,
7069
entry = entries.last(),
@@ -106,21 +105,22 @@ class TwoPaneScene<T : Any>(
106105
}
107106
}
108107

108+
@Composable
109+
fun <T : Any> rememberTwoPaneSceneStrategy(): TwoPaneSceneStrategy<T> {
110+
val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
111+
112+
return remember(windowSizeClass) {
113+
TwoPaneSceneStrategy(windowSizeClass)
114+
}
115+
}
116+
109117
// --- TwoPaneSceneStrategy ---
110118
/**
111119
* A [SceneStrategy] that activates a [TwoPaneScene] if the window is wide enough
112120
* and the top two back stack entries declare support for two-pane display.
113121
*/
114-
class TwoPaneSceneStrategy<T : Any> : SceneStrategy<T> {
115-
@OptIn(ExperimentalMaterial3AdaptiveApi::class, ExperimentalMaterial3WindowSizeClassApi::class)
116-
@Composable
117-
override fun calculateScene(
118-
entries: List<NavEntry<T>>,
119-
onBack: (Int) -> Unit
120-
): Scene<T>? {
121-
122-
val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
123-
122+
class TwoPaneSceneStrategy<T : Any>(val windowSizeClass: WindowSizeClass) : SceneStrategy<T> {
123+
override fun SceneStrategyScope<T>.calculateScene(entries: List<NavEntry<T>>): Scene<T>? {
124124
// Condition 1: Only return a Scene if the window is sufficiently wide to render two panes.
125125
// We use isWidthAtLeastBreakpoint with WIDTH_DP_MEDIUM_LOWER_BOUND (600dp).
126126
if (!windowSizeClass.isWidthAtLeastBreakpoint(WIDTH_DP_MEDIUM_LOWER_BOUND)) {
@@ -193,12 +193,10 @@ fun MyAppContent() {
193193
// ... other entries ...
194194
},
195195
// Simply provide your custom strategy. NavDisplay will fall back to SinglePaneSceneStrategy automatically.
196-
sceneStrategy = TwoPaneSceneStrategy<Any>(),
197-
onBack = { count ->
198-
repeat(count) {
199-
if (backStack.isNotEmpty()) {
200-
backStack.removeLastOrNull()
201-
}
196+
sceneStrategy = rememberTwoPaneSceneStrategy(),
197+
onBack = {
198+
if (backStack.isNotEmpty()) {
199+
backStack.removeLastOrNull()
202200
}
203201
}
204202
)

compose/snippets/src/main/java/com/example/compose/snippets/navigation3/scenes/material/MaterialScenesSnippets.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import androidx.compose.material3.adaptive.navigation3.rememberListDetailSceneSt
3131
import androidx.compose.ui.Alignment
3232
import androidx.compose.ui.Modifier
3333
import androidx.navigation3.runtime.NavKey
34-
import androidx.navigation3.runtime.entry
3534
import androidx.navigation3.runtime.entryProvider
3635
import androidx.navigation3.runtime.rememberNavBackStack
3736
import androidx.navigation3.ui.NavDisplay
@@ -61,12 +60,12 @@ class MaterialListDetailActivity : ComponentActivity() {
6160
setContent {
6261
Scaffold { paddingValues ->
6362
val backStack = rememberNavBackStack(ProductList)
64-
val listDetailStrategy = rememberListDetailSceneStrategy<Any>()
63+
val listDetailStrategy = rememberListDetailSceneStrategy<NavKey>()
6564

6665
NavDisplay(
6766
backStack = backStack,
6867
modifier = Modifier.padding(paddingValues),
69-
onBack = { keysToRemove -> repeat(keysToRemove) { backStack.removeLastOrNull() } },
68+
onBack = { backStack.removeLastOrNull() },
7069
sceneStrategy = listDetailStrategy,
7170
entryProvider = entryProvider {
7271
entry<ProductList>(

gradle/libs.versions.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ androidx-credentials-play-services-auth = "1.5.0"
1717
androidx-emoji2-views = "1.5.0"
1818
androidx-fragment-ktx = "1.8.9"
1919
androidx-glance-appwidget = "1.1.1"
20-
androidx-lifecycle-viewmodel-navigation3 = "1.0.0-alpha04"
21-
androidx-navigation3 = "1.0.0-alpha09"
22-
androidx-lifecycle-compose = "2.9.2"
23-
androidx-lifecycle-runtime-compose = "2.9.2"
20+
androidx-lifecycle-viewmodel-navigation3 = "2.10.0-beta01"
21+
androidx-navigation3 = "1.0.0-beta01"
22+
androidx-lifecycle-compose = "2.10.0-beta01"
23+
androidx-lifecycle-runtime-compose = "2.10.0-beta01"
2424
androidx-navigation = "2.9.3"
2525
androidx-paging = "3.3.6"
2626
androidx-startup-runtime = "1.2.0"
@@ -60,7 +60,7 @@ maps-compose = "6.7.2"
6060
material = "1.14.0-alpha03"
6161
material3-adaptive = "1.1.0"
6262
material3-adaptive-navigation-suite = "1.3.2"
63-
material3-adaptive-navigation3 = "1.0.0-alpha02"
63+
material3-adaptive-navigation3 = "1.3.0-alpha01"
6464
media3 = "1.8.0"
6565
# @keep
6666
minSdk = "35"

0 commit comments

Comments
 (0)