17
17
package com.example.compose.snippets.adaptivelayouts
18
18
19
19
import android.os.Parcelable
20
- import androidx.activity.compose.BackHandler
21
20
import androidx.compose.foundation.background
22
21
import androidx.compose.foundation.clickable
23
22
import androidx.compose.foundation.layout.Column
@@ -33,30 +32,31 @@ import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
33
32
import androidx.compose.material3.adaptive.layout.AnimatedPane
34
33
import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffold
35
34
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
36
38
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
37
39
import androidx.compose.runtime.Composable
40
+ import androidx.compose.runtime.rememberCoroutineScope
38
41
import androidx.compose.ui.Modifier
39
42
import androidx.compose.ui.graphics.Color
40
43
import androidx.compose.ui.tooling.preview.Preview
41
44
import androidx.compose.ui.unit.dp
42
45
import androidx.compose.ui.unit.sp
46
+ import kotlinx.coroutines.launch
43
47
import kotlinx.parcelize.Parcelize
44
48
45
49
@OptIn(ExperimentalMaterial3AdaptiveApi ::class )
46
50
@Composable
47
- fun SampleListDetailPaneScaffoldParts () {
51
+ fun SampleNavigableListDetailPaneScaffoldParts () {
48
52
// [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()
54
55
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part02]
55
56
56
57
// [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,
60
60
// [START_EXCLUDE]
61
61
listPane = {},
62
62
detailPane = {},
@@ -65,16 +65,21 @@ fun SampleListDetailPaneScaffoldParts() {
65
65
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part03]
66
66
67
67
// [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,
71
70
listPane = {
72
71
AnimatedPane {
73
72
MyList (
74
73
onItemClick = { item ->
75
74
// 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
+ },
78
83
)
79
84
}
80
85
},
@@ -85,16 +90,14 @@ fun SampleListDetailPaneScaffoldParts() {
85
90
// [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_part04]
86
91
87
92
// [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,
92
95
// [START_EXCLUDE]
93
- {},
96
+ listPane = {},
94
97
// [END_EXCLUDE]
95
98
detailPane = {
96
99
AnimatedPane {
97
- navigator .currentDestination?.content ?.let {
100
+ scaffoldNavigator .currentDestination?.contentKey ?.let {
98
101
MyDetails (it)
99
102
}
100
103
}
@@ -106,37 +109,80 @@ fun SampleListDetailPaneScaffoldParts() {
106
109
@OptIn(ExperimentalMaterial3AdaptiveApi ::class )
107
110
@Preview
108
111
@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()
112
116
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
+ )
116
157
117
158
ListDetailPaneScaffold (
118
- directive = navigator .scaffoldDirective,
119
- value = navigator.scaffoldValue ,
159
+ directive = scaffoldNavigator .scaffoldDirective,
160
+ scaffoldState = scaffoldNavigator.scaffoldState ,
120
161
listPane = {
121
162
AnimatedPane {
122
163
MyList (
123
164
onItemClick = { item ->
124
165
// 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
+ }
126
172
},
127
173
)
128
174
}
129
175
},
130
176
detailPane = {
131
177
AnimatedPane {
132
178
// Show the detail pane content if selected item is available
133
- navigator .currentDestination?.content ?.let {
179
+ scaffoldNavigator .currentDestination?.contentKey ?.let {
134
180
MyDetails (it)
135
181
}
136
182
}
137
183
},
138
184
)
139
- // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_full ]
185
+ // [END android_compose_adaptivelayouts_sample_list_detail_pane_scaffold_with_pb_full ]
140
186
}
141
187
142
188
@Composable
0 commit comments