diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt b/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt index c4148352..84806e9e 100644 --- a/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt +++ b/compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt @@ -27,13 +27,15 @@ import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material3.Card import androidx.compose.material3.ListItem import androidx.compose.material3.Text +import androidx.compose.material3.adaptive.AnimatedPane import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi import androidx.compose.material3.adaptive.ListDetailPaneScaffold import androidx.compose.material3.adaptive.ListDetailPaneScaffoldRole -import androidx.compose.material3.adaptive.rememberListDetailPaneScaffoldState +import androidx.compose.material3.adaptive.rememberListDetailPaneScaffoldNavigator import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.Saver import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier @@ -46,16 +48,18 @@ import androidx.compose.ui.unit.sp @Composable fun SampleListDetailPaneScaffoldParts() { // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02] - val state = rememberListDetailPaneScaffoldState() + val navigator = rememberListDetailPaneScaffoldNavigator() // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02] // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part01] - var selectedItem: MyItem? by rememberSaveable { mutableStateOf(null) } + var selectedItem: MyItem? by rememberSaveable(stateSaver = MyItem.Saver) { + mutableStateOf(null) + } // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part01] // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03] ListDetailPaneScaffold( - scaffoldState = state, + scaffoldState = navigator.scaffoldState, // [START_EXCLUDE] listPane = {}, detailPane = {}, @@ -65,16 +69,18 @@ fun SampleListDetailPaneScaffoldParts() { // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04] ListDetailPaneScaffold( - scaffoldState = state, + scaffoldState = navigator.scaffoldState, listPane = { - MyList( - onItemClick = { id -> - // Set current item - selectedItem = id - // Switch focus to detail pane - state.navigateTo(ListDetailPaneScaffoldRole.Detail) - } - ) + AnimatedPane(Modifier) { + MyList( + onItemClick = { id -> + // Set current item + selectedItem = id + // Switch focus to detail pane + navigator.navigateTo(ListDetailPaneScaffoldRole.Detail) + } + ) + } }, // [START_EXCLUDE] detailPane = {}, @@ -84,14 +90,16 @@ fun SampleListDetailPaneScaffoldParts() { // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part05] ListDetailPaneScaffold( - scaffoldState = state, + scaffoldState = navigator.scaffoldState, listPane = // [START_EXCLUDE] {}, // [END_EXCLUDE] detailPane = { - selectedItem?.let { item -> - MyDetails(item) + AnimatedPane(Modifier) { + selectedItem?.let { item -> + MyDetails(item) + } } }, ) @@ -104,27 +112,33 @@ fun SampleListDetailPaneScaffoldParts() { fun SampleListDetailPaneScaffoldFull() { // [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full] // Create the ListDetailPaneScaffoldState - val state = rememberListDetailPaneScaffoldState() + val navigator = rememberListDetailPaneScaffoldNavigator() // Currently selected item - var selectedItem: MyItem? by rememberSaveable { mutableStateOf(null) } + var selectedItem: MyItem? by rememberSaveable(stateSaver = MyItem.Saver) { + mutableStateOf(null) + } ListDetailPaneScaffold( - scaffoldState = state, + scaffoldState = navigator.scaffoldState, listPane = { - MyList( - onItemClick = { id -> - // Set current item - selectedItem = id - // Display the detail pane - state.navigateTo(ListDetailPaneScaffoldRole.Detail) - }, - ) + AnimatedPane(Modifier) { + MyList( + onItemClick = { id -> + // Set current item + selectedItem = id + // Display the detail pane + navigator.navigateTo(ListDetailPaneScaffoldRole.Detail) + }, + ) + } }, detailPane = { - // Show the detail pane content if selected item is available - selectedItem?.let { item -> - MyDetails(item) + AnimatedPane(Modifier) { + // Show the detail pane content if selected item is available + selectedItem?.let { item -> + MyDetails(item) + } } }, ) @@ -178,7 +192,14 @@ fun MyDetails(item: MyItem) { } } -class MyItem(val id: Int) +class MyItem(val id: Int) { + companion object { + val Saver: Saver = Saver( + { it?.id }, + ::MyItem, + ) + } +} val shortStrings = listOf( "Android", diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 69d192ab..eaac4433 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -32,7 +32,8 @@ kotlin = "1.9.20" ksp = "1.8.0-1.0.9" maps-compose = "3.1.1" material = "1.11.0-beta01" -material3-adaptive = "1.0.0-alpha01" +material3-adaptive = "1.0.0-alpha03" +material3-adaptive-navigation-suite = "1.0.0-alpha02" # @keep minSdk = "21" recyclerview = "1.3.2" @@ -56,7 +57,7 @@ androidx-compose-material = { module = "androidx.compose.material:material" } androidx-compose-material-iconsExtended = { module = "androidx.compose.material:material-icons-extended" } androidx-compose-material3 = { module = "androidx.compose.material3:material3" } androidx-compose-material3-adaptive = { module = "androidx.compose.material3:material3-adaptive", version.ref = "material3-adaptive" } -androidx-compose-material3-adaptive-navigation-suite = { module = "androidx.compose.material3:material3-adaptive-navigation-suite", version.ref = "material3-adaptive" } +androidx-compose-material3-adaptive-navigation-suite = { module = "androidx.compose.material3:material3-adaptive-navigation-suite", version.ref = "material3-adaptive-navigation-suite" } androidx-compose-materialWindow = { module = "androidx.compose.material3:material3-window-size-class" } androidx-compose-runtime = { module = "androidx.compose.runtime:runtime" } androidx-compose-runtime-livedata = { module = "androidx.compose.runtime:runtime-livedata" }