Skip to content

Commit 724b5e6

Browse files
authored
update adaptive layouts samples to navigable scaffolds (#449)
* update adaptive layouts samples to navigable scaffolds * Apply Spotless * change back the region tags * fix the region tags issues * change the file name back to restore the region tags * change the list-detail pane file name back
1 parent 1eec3c8 commit 724b5e6

File tree

3 files changed

+163
-76
lines changed

3 files changed

+163
-76
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/adaptivelayouts/SampleListDetailPaneScaffold.kt

Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.example.compose.snippets.adaptivelayouts
1818

1919
import android.os.Parcelable
20-
import androidx.activity.compose.BackHandler
2120
import androidx.compose.foundation.background
2221
import androidx.compose.foundation.clickable
2322
import androidx.compose.foundation.layout.Column
@@ -33,30 +32,31 @@ import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
3332
import androidx.compose.material3.adaptive.layout.AnimatedPane
3433
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffold
3534
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
35+
import androidx.compose.material3.adaptive.navigation.BackNavigationBehavior
36+
import androidx.compose.material3.adaptive.navigation.NavigableListDetailPaneScaffold
37+
import androidx.compose.material3.adaptive.navigation.ThreePaneScaffoldPredictiveBackHandler
3638
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
3739
import androidx.compose.runtime.Composable
40+
import androidx.compose.runtime.rememberCoroutineScope
3841
import androidx.compose.ui.Modifier
3942
import androidx.compose.ui.graphics.Color
4043
import androidx.compose.ui.tooling.preview.Preview
4144
import androidx.compose.ui.unit.dp
4245
import androidx.compose.ui.unit.sp
46+
import kotlinx.coroutines.launch
4347
import kotlinx.parcelize.Parcelize
4448

4549
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
4650
@Composable
47-
fun SampleListDetailPaneScaffoldParts() {
51+
fun SampleNavigableListDetailPaneScaffoldParts() {
4852
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02]
49-
val navigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
50-
51-
BackHandler(navigator.canNavigateBack()) {
52-
navigator.navigateBack()
53-
}
53+
val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
54+
val scope = rememberCoroutineScope()
5455
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02]
5556

5657
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03]
57-
ListDetailPaneScaffold(
58-
directive = navigator.scaffoldDirective,
59-
value = navigator.scaffoldValue,
58+
NavigableListDetailPaneScaffold(
59+
navigator = scaffoldNavigator,
6060
// [START_EXCLUDE]
6161
listPane = {},
6262
detailPane = {},
@@ -65,16 +65,21 @@ fun SampleListDetailPaneScaffoldParts() {
6565
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03]
6666

6767
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04]
68-
ListDetailPaneScaffold(
69-
directive = navigator.scaffoldDirective,
70-
value = navigator.scaffoldValue,
68+
NavigableListDetailPaneScaffold(
69+
navigator = scaffoldNavigator,
7170
listPane = {
7271
AnimatedPane {
7372
MyList(
7473
onItemClick = { item ->
7574
// Navigate to the detail pane with the passed item
76-
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
77-
}
75+
scope.launch {
76+
scaffoldNavigator
77+
.navigateTo(
78+
ListDetailPaneScaffoldRole.Detail,
79+
item
80+
)
81+
}
82+
},
7883
)
7984
}
8085
},
@@ -85,16 +90,14 @@ fun SampleListDetailPaneScaffoldParts() {
8590
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04]
8691

8792
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part05]
88-
ListDetailPaneScaffold(
89-
directive = navigator.scaffoldDirective,
90-
value = navigator.scaffoldValue,
91-
listPane =
93+
NavigableListDetailPaneScaffold(
94+
navigator = scaffoldNavigator,
9295
// [START_EXCLUDE]
93-
{},
96+
listPane = {},
9497
// [END_EXCLUDE]
9598
detailPane = {
9699
AnimatedPane {
97-
navigator.currentDestination?.content?.let {
100+
scaffoldNavigator.currentDestination?.contentKey?.let {
98101
MyDetails(it)
99102
}
100103
}
@@ -106,37 +109,80 @@ fun SampleListDetailPaneScaffoldParts() {
106109
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
107110
@Preview
108111
@Composable
109-
fun SampleListDetailPaneScaffoldFull() {
110-
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
111-
val navigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
112+
fun SampleNavigableListDetailPaneScaffoldFull() {
113+
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
114+
val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
115+
val scope = rememberCoroutineScope()
112116

113-
BackHandler(navigator.canNavigateBack()) {
114-
navigator.navigateBack()
115-
}
117+
NavigableListDetailPaneScaffold(
118+
navigator = scaffoldNavigator,
119+
listPane = {
120+
AnimatedPane {
121+
MyList(
122+
onItemClick = { item ->
123+
// Navigate to the detail pane with the passed item
124+
scope.launch {
125+
scaffoldNavigator.navigateTo(
126+
ListDetailPaneScaffoldRole.Detail,
127+
item
128+
)
129+
}
130+
},
131+
)
132+
}
133+
},
134+
detailPane = {
135+
AnimatedPane {
136+
// Show the detail pane content if selected item is available
137+
scaffoldNavigator.currentDestination?.contentKey?.let {
138+
MyDetails(it)
139+
}
140+
}
141+
},
142+
)
143+
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
144+
}
145+
146+
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
147+
@Composable
148+
fun SampleListDetailPaneScaffoldWithPredictiveBackFull() {
149+
// [START android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_with_pb_full]
150+
val scaffoldNavigator = rememberListDetailPaneScaffoldNavigator<MyItem>()
151+
val scope = rememberCoroutineScope()
152+
153+
ThreePaneScaffoldPredictiveBackHandler(
154+
navigator = scaffoldNavigator,
155+
backBehavior = BackNavigationBehavior.PopUntilContentChange
156+
)
116157

117158
ListDetailPaneScaffold(
118-
directive = navigator.scaffoldDirective,
119-
value = navigator.scaffoldValue,
159+
directive = scaffoldNavigator.scaffoldDirective,
160+
scaffoldState = scaffoldNavigator.scaffoldState,
120161
listPane = {
121162
AnimatedPane {
122163
MyList(
123164
onItemClick = { item ->
124165
// Navigate to the detail pane with the passed item
125-
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, item)
166+
scope.launch {
167+
scaffoldNavigator.navigateTo(
168+
ListDetailPaneScaffoldRole.Detail,
169+
item
170+
)
171+
}
126172
},
127173
)
128174
}
129175
},
130176
detailPane = {
131177
AnimatedPane {
132178
// Show the detail pane content if selected item is available
133-
navigator.currentDestination?.content?.let {
179+
scaffoldNavigator.currentDestination?.contentKey?.let {
134180
MyDetails(it)
135181
}
136182
}
137183
},
138184
)
139-
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full]
185+
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_with_pb_full]
140186
}
141187

142188
@Composable

0 commit comments

Comments
 (0)