@@ -3,13 +3,25 @@ package xyz.teamgravity.composenestednavigation
33import android.os.Bundle
44import androidx.activity.ComponentActivity
55import androidx.activity.compose.setContent
6+ import androidx.compose.foundation.layout.Arrangement
7+ import androidx.compose.foundation.layout.Column
68import androidx.compose.foundation.layout.fillMaxSize
9+ import androidx.compose.material3.Button
710import androidx.compose.material3.MaterialTheme
811import androidx.compose.material3.Surface
912import androidx.compose.material3.Text
1013import androidx.compose.runtime.Composable
14+ import androidx.compose.runtime.remember
15+ import androidx.compose.ui.Alignment
1116import androidx.compose.ui.Modifier
12- import androidx.compose.ui.tooling.preview.Preview
17+ import androidx.lifecycle.ViewModel
18+ import androidx.lifecycle.viewmodel.compose.viewModel
19+ import androidx.navigation.NavBackStackEntry
20+ import androidx.navigation.NavController
21+ import androidx.navigation.compose.NavHost
22+ import androidx.navigation.compose.composable
23+ import androidx.navigation.compose.rememberNavController
24+ import androidx.navigation.navigation
1325import xyz.teamgravity.composenestednavigation.ui.theme.ComposeNestedNavigationTheme
1426
1527class MainActivity : ComponentActivity () {
@@ -21,8 +33,112 @@ class MainActivity : ComponentActivity() {
2133 modifier = Modifier .fillMaxSize(),
2234 color = MaterialTheme .colorScheme.background
2335 ) {
36+ val controller = rememberNavController()
37+
38+ NavHost (
39+ navController = controller,
40+ startDestination = " auth"
41+ ) {
42+ navigation(
43+ route = " auth" ,
44+ startDestination = " login"
45+ ) {
46+ composable(route = " login" ) { entry ->
47+ val viewmodel = entry.sharedViewModel<AuthViewModel >(controller)
48+ GenericScreen (
49+ text = " Login" ,
50+ onClick = {
51+ controller.navigate(
52+ route = " profile" ,
53+ builder = {
54+ popUpTo(
55+ route = " auth" ,
56+ popUpToBuilder = {
57+ inclusive = true
58+ }
59+ )
60+ }
61+ )
62+ }
63+ )
64+ }
65+ composable(route = " register" ) { entry ->
66+ val viewmodel = entry.sharedViewModel<AuthViewModel >(controller)
67+ GenericScreen (
68+ text = " Register" ,
69+ onClick = {
70+
71+ }
72+ )
73+ }
74+ composable(route = " forgot_password" ) { entry ->
75+ val viewmodel = entry.sharedViewModel<AuthViewModel >(controller)
76+ GenericScreen (
77+ text = " Forgot Password" ,
78+ onClick = {
79+ controller.navigate(" about" )
80+ }
81+ )
82+ }
83+ }
84+ navigation(
85+ route = " profile" ,
86+ startDestination = " settings"
87+ ) {
88+ composable(route = " settings" ) {
89+ GenericScreen (
90+ text = " Settings" ,
91+ onClick = {
92+ controller.navigate(" forgot_password" )
93+ }
94+ )
95+ }
96+ composable(route = " goals" ) {
97+ GenericScreen (
98+ text = " Goals" ,
99+ onClick = {
100+
101+ }
102+ )
103+ }
104+ }
105+ composable(route = " about" ) {
106+ GenericScreen (
107+ text = " About" ,
108+ onClick = {
109+
110+ }
111+ )
112+ }
113+ }
24114 }
25115 }
26116 }
27117 }
28- }
118+ }
119+
120+ @Composable
121+ fun GenericScreen (
122+ text : String ,
123+ onClick : () -> Unit
124+ ) {
125+ Column (
126+ horizontalAlignment = Alignment .CenterHorizontally ,
127+ verticalArrangement = Arrangement .Center ,
128+ modifier = Modifier .fillMaxSize()
129+ ) {
130+ Text (text = text)
131+ Button (onClick = onClick) {
132+ Text (text = " Click me" )
133+ }
134+ }
135+ }
136+
137+ @Composable
138+ inline fun <reified T : ViewModel > NavBackStackEntry.sharedViewModel (controller : NavController ): T {
139+ val route = destination.parent?.route ? : return viewModel()
140+ val entry = remember(this ) { controller.getBackStackEntry(route) }
141+ return viewModel(viewModelStoreOwner = entry)
142+ }
143+
144+ class AuthViewModel : ViewModel ()
0 commit comments