Skip to content

Updated ListDetailPaneScaffold use to alpha11 #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kapt) apply false
alias(libs.plugins.hilt) apply false
alias(libs.plugins.kotlin.parcelize) apply false
}

apply("${project.rootDir}/buildscripts/toml-updater-config.gradle")
Expand Down
1 change: 1 addition & 0 deletions compose/snippets/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kapt)
alias(libs.plugins.hilt)
alias(libs.plugins.kotlin.parcelize)
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.example.compose.snippets.adaptivelayouts

import android.os.Parcelable
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
Expand All @@ -35,33 +36,25 @@ import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
import androidx.compose.material3.adaptive.navigation.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
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.parcelize.Parcelize

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
fun SampleListDetailPaneScaffoldParts() {
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02]
val navigator = rememberListDetailPaneScaffoldNavigator<Nothing>()
val navigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this correctly save MyItem in saved instance state? We aren't using MyItem.Saver anywhere now, which seems suspicious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, we aren't actually passing the saver into the rememberX so I guess it has to be a Parcelable class to be safely used here.


BackHandler(navigator.canNavigateBack()) {
navigator.navigateBack()
}
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02]

// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part01]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Farewell part01, you will be missed 👋

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(
directive = navigator.scaffoldDirective,
Expand All @@ -78,13 +71,11 @@ fun SampleListDetailPaneScaffoldParts() {
directive = navigator.scaffoldDirective,
value = navigator.scaffoldValue,
listPane = {
AnimatedPane(Modifier) {
AnimatedPane {
MyList(
onItemClick = { id ->
// Set current item
selectedItem = id
// Switch focus to detail pane
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail)
onItemClick = { item ->
// Navigate to the detail pane with the passed item
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
}
)
}
Expand All @@ -104,9 +95,9 @@ fun SampleListDetailPaneScaffoldParts() {
{},
// [END_EXCLUDE]
detailPane = {
AnimatedPane(Modifier) {
selectedItem?.let { item ->
MyDetails(item)
AnimatedPane {
navigator.currentDestination?.content?.let {
MyDetails(it)
}
}
},
Expand All @@ -119,13 +110,7 @@ fun SampleListDetailPaneScaffoldParts() {
@Composable
fun SampleListDetailPaneScaffoldFull() {
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
// Currently selected item
var selectedItem: MyItem? by rememberSaveable(stateSaver = MyItem.Saver) {
mutableStateOf(null)
}

// Create the ListDetailPaneScaffoldState
val navigator = rememberListDetailPaneScaffoldNavigator<Nothing>()
val navigator = rememberListDetailPaneScaffoldNavigator<MyItem>()

BackHandler(navigator.canNavigateBack()) {
navigator.navigateBack()
Expand All @@ -135,22 +120,20 @@ fun SampleListDetailPaneScaffoldFull() {
directive = navigator.scaffoldDirective,
value = navigator.scaffoldValue,
listPane = {
AnimatedPane(Modifier) {
AnimatedPane {
MyList(
onItemClick = { id ->
// Set current item
selectedItem = id
// Display the detail pane
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail)
onItemClick = { item ->
// Navigate to the detail pane with the passed item
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
},
)
}
},
detailPane = {
AnimatedPane(Modifier) {
AnimatedPane {
// Show the detail pane content if selected item is available
selectedItem?.let { item ->
MyDetails(item)
navigator.currentDestination?.content?.let {
MyDetails(it)
}
}
},
Expand Down Expand Up @@ -206,19 +189,11 @@ fun MyDetails(item: MyItem) {
}

// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_myitem]
class MyItem(val id: Int) {
companion object {
val Saver: Saver<MyItem?, Int> = Saver(
{ it?.id },
::MyItem,
)
}
}
@Parcelize
class MyItem(val id: Int) : Parcelable
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_myitem]

val shortStrings = listOf(
"Android",
"Petit four",
"Cupcake",
"Donut",
"Eclair",
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ kotlin = "1.9.20"
ksp = "1.8.0-1.0.9"
maps-compose = "4.3.2"
material = "1.11.0"
material3-adaptive = "1.0.0-alpha08"
material3-adaptive = "1.0.0-alpha12"
material3-adaptive-navigation-suite = "1.0.0-alpha05"
media3 = "1.2.1"
# @keep
Expand Down Expand Up @@ -118,3 +118,4 @@ hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
version-catalog-update = { id = "nl.littlerobots.version-catalog-update", version.ref = "version-catalog-update" }
kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }