1414 * limitations under the License.
1515 */
1616
17- package com.example.nav3recipes.migration.start
17+ package com.example.nav3recipes.migration.atomic.begin
1818
1919import android.os.Bundle
2020import androidx.activity.ComponentActivity
2121import androidx.activity.compose.setContent
2222import androidx.compose.foundation.background
23- import androidx.compose.foundation.layout.Column
2423import androidx.compose.foundation.layout.padding
25- import androidx.compose.material.icons.Icons
26- import androidx.compose.material.icons.filled.Camera
27- import androidx.compose.material.icons.filled.Face
28- import androidx.compose.material.icons.filled.Home
29- import androidx.compose.material3.Button
3024import androidx.compose.material3.Icon
3125import androidx.compose.material3.NavigationBar
3226import androidx.compose.material3.NavigationBarItem
3327import androidx.compose.material3.Scaffold
3428import androidx.compose.material3.Text
3529import androidx.compose.runtime.getValue
36- import androidx.compose.ui.Alignment
3730import androidx.compose.ui.Modifier
3831import androidx.compose.ui.graphics.Color
39- import androidx.compose.ui.graphics.vector.ImageVector
4032import androidx.navigation.NavDestination
4133import androidx.navigation.NavDestination.Companion.hasRoute
4234import androidx.navigation.NavDestination.Companion.hierarchy
@@ -49,67 +41,34 @@ import androidx.navigation.compose.navigation
4941import androidx.navigation.compose.rememberNavController
5042import androidx.navigation.navOptions
5143import androidx.navigation.toRoute
52- import com.example.nav3recipes.content.ContentBlue
53- import com.example.nav3recipes.content.ContentGreen
54- import com.example.nav3recipes.content.ContentMauve
55- import com.example.nav3recipes.content.ContentPink
56- import com.example.nav3recipes.content.ContentPurple
57- import com.example.nav3recipes.content.ContentRed
44+ import com.example.nav3recipes.migration.content.ScreenA
45+ import com.example.nav3recipes.migration.content.ScreenA1
46+ import com.example.nav3recipes.migration.content.ScreenB
47+ import com.example.nav3recipes.migration.content.ScreenB1
48+ import com.example.nav3recipes.migration.content.ScreenC
5849import com.example.nav3recipes.ui.setEdgeToEdgeConfig
59- import kotlinx.serialization.Serializable
6050import kotlin.reflect.KClass
6151
6252/* *
6353 * Basic Navigation2 example with the following navigation graph:
6454 *
65- * A -> A, A1, E
66- * B -> B, B1, E
67- * C -> C, E
55+ * A -> A, A1
56+ * B -> B, B1
57+ * C -> C
6858 * D
6959 *
7060 * - The starting destination (or home screen) is A.
7161 * - A, B and C are top level destinations that appear in a navigation bar.
7262 * - D is a dialog destination.
73- * - E is a shared destination that can appear under any of the top level destinations.
74- * - Navigating to a top level destination pops all other top level destinations off the stack,
63+ * - Navigating to a top level destination pops all other top level destinations off the stack,
7564 * except for the start destination.
7665 * - Navigating back from the start destination exits the app.
7766 *
7867 * This will be the starting point for migration to Navigation 3.
7968 *
80- * @see `MigrationActivityNavigationTest ` for instrumented tests that verify this behavior.
69+ * @see `AtomicMigrationTest ` for instrumented tests that verify this behavior.
8170 */
82-
83- // Feature module A
84- @Serializable private data object BaseRouteA
85- @Serializable private data object RouteA
86- @Serializable private data object RouteA1
87-
88- // Feature module B
89- @Serializable private data object BaseRouteB
90- @Serializable private data object RouteB
91- @Serializable private data class RouteB1 (val id : String )
92-
93- // Feature module C
94- @Serializable private data object BaseRouteC
95- @Serializable private data object RouteC
96-
97- // Common UI modules
98- @Serializable private data object RouteD
99- @Serializable private data object RouteE
100-
101- private val TOP_LEVEL_ROUTES = mapOf (
102- BaseRouteA to NavBarItem (icon = Icons .Default .Home , description = " Route A" ),
103- BaseRouteB to NavBarItem (icon = Icons .Default .Face , description = " Route B" ),
104- BaseRouteC to NavBarItem (icon = Icons .Default .Camera , description = " Route C" ),
105- )
106-
107- class NavBarItem (
108- val icon : ImageVector ,
109- val description : String
110- )
111-
112- class StartMigrationActivity : ComponentActivity () {
71+ class BeginAtomicMigrationActivity : ComponentActivity () {
11372
11473 override fun onCreate (savedInstanceState : Bundle ? ) {
11574 setEdgeToEdgeConfig()
@@ -150,16 +109,13 @@ class StartMigrationActivity : ComponentActivity() {
150109 featureASection(
151110 onSubRouteClick = { navController.navigate(RouteA1 ) },
152111 onDialogClick = { navController.navigate(RouteD ) },
153- onOtherClick = { navController.navigate(RouteE ) }
154112 )
155113 featureBSection(
156114 onDetailClick = { id -> navController.navigate(RouteB1 (id)) },
157115 onDialogClick = { navController.navigate(RouteD ) },
158- onOtherClick = { navController.navigate(RouteE ) }
159116 )
160117 featureCSection(
161118 onDialogClick = { navController.navigate(RouteD ) },
162- onOtherClick = { navController.navigate(RouteE ) }
163119 )
164120 dialog<RouteD > { key ->
165121 Text (modifier = Modifier .background(Color .White ), text = " Route D title (dialog)" )
@@ -173,78 +129,32 @@ class StartMigrationActivity : ComponentActivity() {
173129// Feature module A
174130private fun NavGraphBuilder.featureASection (
175131 onSubRouteClick : () -> Unit ,
176- onDialogClick : () -> Unit ,
177- onOtherClick : () -> Unit ,
132+ onDialogClick : () -> Unit
178133) {
179134 navigation<BaseRouteA >(startDestination = RouteA ) {
180- composable<RouteA > {
181- ContentRed (" Route A title" ) {
182- Column (horizontalAlignment = Alignment .CenterHorizontally ) {
183- Button (onClick = onSubRouteClick) {
184- Text (" Go to A1" )
185- }
186- Button (onClick = onDialogClick) {
187- Text (" Open dialog D" )
188- }
189- Button (onClick = onOtherClick) {
190- Text (" Go to E" )
191- }
192- }
193- }
194- }
195- composable<RouteA1 > { ContentPink (" Route A1 title" ) }
196- composable<RouteE > { ContentBlue (" Route E title" ) }
135+ composable<RouteA > { ScreenA (onSubRouteClick, onDialogClick) }
136+ composable<RouteA1 > { ScreenA1 () }
197137 }
198138}
199139
140+
200141// Feature module B
201142private fun NavGraphBuilder.featureBSection (
202143 onDetailClick : (id: String ) -> Unit ,
203- onDialogClick : () -> Unit ,
204- onOtherClick : () -> Unit
144+ onDialogClick : () -> Unit
205145) {
206146 navigation<BaseRouteB >(startDestination = RouteB ) {
207- composable<RouteB > {
208- ContentGreen (" Route B title" ) {
209- Column (horizontalAlignment = Alignment .CenterHorizontally ) {
210- Button (onClick = { onDetailClick(" ABC" ) }) {
211- Text (" Go to B1" )
212- }
213- Button (onClick = onDialogClick) {
214- Text (" Open dialog D" )
215- }
216- Button (onClick = onOtherClick) {
217- Text (" Go to E" )
218- }
219- }
220- }
221- }
222- composable<RouteB1 > { key ->
223- ContentPurple (" Route B1 title. ID: ${key.toRoute<RouteB1 >().id} " )
224- }
225- composable<RouteE > { ContentBlue (" Route E title" ) }
147+ composable<RouteB > { ScreenB (onDetailClick, onDialogClick) }
148+ composable<RouteB1 > { key -> ScreenB1 (id = key.toRoute<RouteB1 >().id) }
226149 }
227150}
228151
229152// Feature module C
230153private fun NavGraphBuilder.featureCSection (
231- onDialogClick : () -> Unit ,
232- onOtherClick : () -> Unit ,
154+ onDialogClick : () -> Unit
233155) {
234156 navigation<BaseRouteC >(startDestination = RouteC ) {
235- composable<RouteC > {
236- ContentMauve (" Route C title" ) {
237- Column (horizontalAlignment = Alignment .CenterHorizontally ) {
238- Button (onClick = onDialogClick) {
239- Text (" Open dialog D" )
240- }
241- Button (onClick = onOtherClick) {
242- Text (" Go to E" )
243- }
244- }
245- }
246- }
247- composable<RouteE > { ContentBlue (" Route E title" ) }
157+ composable<RouteC > { ScreenC (onDialogClick) }
248158 }
249159}
250160
0 commit comments