Skip to content
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

update adaptive layouts samples to navigable scaffolds #449

Merged
merged 7 commits into from
Feb 11, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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
import androidx.compose.foundation.layout.Column
Expand All @@ -33,30 +32,31 @@ import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
import androidx.compose.material3.adaptive.layout.AnimatedPane
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffold
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
import androidx.compose.material3.adaptive.navigation.BackNavigationBehavior
import androidx.compose.material3.adaptive.navigation.NavigableListDetailPaneScaffold
import androidx.compose.material3.adaptive.navigation.ThreePaneScaffoldPredictiveBackHandler
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
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.coroutines.launch
import kotlinx.parcelize.Parcelize

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

BackHandler(navigator.canNavigateBack()) {
navigator.navigateBack()
}
val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
val scope = rememberCoroutineScope()
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02]

// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03]
ListDetailPaneScaffold(
directive = navigator.scaffoldDirective,
value = navigator.scaffoldValue,
NavigableListDetailPaneScaffold(
navigator = scaffoldNavigator,
// [START_EXCLUDE]
listPane = {},
detailPane = {},
Expand All @@ -65,16 +65,21 @@ fun SampleListDetailPaneScaffoldParts() {
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03]

// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04]
ListDetailPaneScaffold(
directive = navigator.scaffoldDirective,
value = navigator.scaffoldValue,
NavigableListDetailPaneScaffold(
navigator = scaffoldNavigator,
listPane = {
AnimatedPane {
MyList(
onItemClick = { item ->
// Navigate to the detail pane with the passed item
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
}
scope.launch {
scaffoldNavigator
.navigateTo(
ListDetailPaneScaffoldRole.Detail,
item
)
}
},
)
}
},
Expand All @@ -85,16 +90,14 @@ fun SampleListDetailPaneScaffoldParts() {
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04]

// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part05]
ListDetailPaneScaffold(
directive = navigator.scaffoldDirective,
value = navigator.scaffoldValue,
listPane =
NavigableListDetailPaneScaffold(
navigator = scaffoldNavigator,
// [START_EXCLUDE]
{},
listPane = {},
// [END_EXCLUDE]
detailPane = {
AnimatedPane {
navigator.currentDestination?.content?.let {
scaffoldNavigator.currentDestination?.contentKey?.let {
MyDetails(it)
}
}
Expand All @@ -106,37 +109,80 @@ fun SampleListDetailPaneScaffoldParts() {
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Preview
@Composable
fun SampleListDetailPaneScaffoldFull() {
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
val navigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
fun SampleNavigableListDetailPaneScaffoldFull() {
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
val scope = rememberCoroutineScope()

BackHandler(navigator.canNavigateBack()) {
navigator.navigateBack()
}
NavigableListDetailPaneScaffold(
navigator = scaffoldNavigator,
listPane = {
AnimatedPane {
MyList(
onItemClick = { item ->
// Navigate to the detail pane with the passed item
scope.launch {
scaffoldNavigator.navigateTo(
ListDetailPaneScaffoldRole.Detail,
item
)
}
},
)
}
},
detailPane = {
AnimatedPane {
// Show the detail pane content if selected item is available
scaffoldNavigator.currentDestination?.contentKey?.let {
MyDetails(it)
}
}
},
)
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
}

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
fun SampleListDetailPaneScaffoldWithPredictiveBackFull() {
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_with_pb_full]
val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
val scope = rememberCoroutineScope()

ThreePaneScaffoldPredictiveBackHandler(
navigator = scaffoldNavigator,
backBehavior = BackNavigationBehavior.PopUntilContentChange
)

ListDetailPaneScaffold(
directive = navigator.scaffoldDirective,
value = navigator.scaffoldValue,
directive = scaffoldNavigator.scaffoldDirective,
scaffoldState = scaffoldNavigator.scaffoldState,
listPane = {
AnimatedPane {
MyList(
onItemClick = { item ->
// Navigate to the detail pane with the passed item
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
scope.launch {
scaffoldNavigator.navigateTo(
ListDetailPaneScaffoldRole.Detail,
item
)
}
},
)
}
},
detailPane = {
AnimatedPane {
// Show the detail pane content if selected item is available
navigator.currentDestination?.content?.let {
scaffoldNavigator.currentDestination?.contentKey?.let {
MyDetails(it)
}
}
},
)
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_with_pb_full]
}

@Composable
Expand Down
Loading