-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#552] Define a "mainNavGraph" in sample-compose
- Loading branch information
Showing
16 changed files
with
142 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppNavGraph.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package co.nimblehq.sample.compose.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.navigation.NavBackStackEntry | ||
import androidx.navigation.NavDeepLink | ||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import co.nimblehq.sample.compose.ui.screens.main.mainNavGraph | ||
|
||
@Composable | ||
fun AppNavigation( | ||
navController: NavHostController, | ||
) { | ||
NavHost( | ||
navController = navController, | ||
route = AppDestination.RootNavGraph.route, | ||
startDestination = AppDestination.MainNavGraph.destination | ||
) { | ||
mainNavGraph(navController = navController) | ||
} | ||
} | ||
|
||
fun NavGraphBuilder.composable( | ||
destination: AppDestination, | ||
deepLinks: List<NavDeepLink> = emptyList(), | ||
content: @Composable (NavBackStackEntry) -> Unit, | ||
) { | ||
composable( | ||
route = destination.route, | ||
arguments = destination.arguments, | ||
deepLinks = deepLinks, | ||
content = content | ||
) | ||
} | ||
|
||
/** | ||
* Navigate to provided [AppDestination] with a Pair of key value String and Data [parcel] | ||
* Caution to use this method. This method use savedStateHandle to store the Parcelable data. | ||
* When previousBackstackEntry is popped out from navigation stack, savedStateHandle will return null and cannot retrieve data. | ||
* eg.Login -> Home, the Login screen will be popped from the back-stack on logging in successfully. | ||
*/ | ||
fun NavHostController.navigate(appDestination: AppDestination, parcel: Pair<String, Any?>? = null) { | ||
when (appDestination) { | ||
is AppDestination.Up -> navigateUp() | ||
else -> { | ||
parcel?.let { (key, value) -> | ||
currentBackStackEntry?.savedStateHandle?.set(key, value) | ||
} | ||
navigate(route = appDestination.destination) | ||
} | ||
} | ||
} |
73 changes: 0 additions & 73 deletions
73
sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/AppNavigation.kt
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...blehq/sample/compose/ui/screens/AppBar.kt → ...mblehq/sample/compose/ui/common/AppBar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
sample-compose/app/src/main/java/co/nimblehq/sample/compose/ui/screens/main/MainNavGraph.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package co.nimblehq.sample.compose.ui.screens.main | ||
|
||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.navigation | ||
import co.nimblehq.sample.compose.model.UiModel | ||
import co.nimblehq.sample.compose.ui.AppDestination | ||
import co.nimblehq.sample.compose.ui.KeyId | ||
import co.nimblehq.sample.compose.ui.KeyModel | ||
import co.nimblehq.sample.compose.ui.composable | ||
import co.nimblehq.sample.compose.ui.navigate | ||
import co.nimblehq.sample.compose.ui.screens.main.home.HomeScreen | ||
import co.nimblehq.sample.compose.ui.screens.main.second.SecondScreen | ||
import co.nimblehq.sample.compose.ui.screens.main.third.ThirdScreen | ||
|
||
fun NavGraphBuilder.mainNavGraph( | ||
navController: NavHostController, | ||
) { | ||
navigation( | ||
route = AppDestination.MainNavGraph.route, | ||
startDestination = AppDestination.Home.destination | ||
) { | ||
composable(destination = AppDestination.Home) { | ||
HomeScreen( | ||
navigator = { destination -> | ||
navController.navigate(destination, destination.parcelableArgument) | ||
} | ||
) | ||
} | ||
|
||
composable(destination = AppDestination.Second) { backStackEntry -> | ||
SecondScreen( | ||
navigator = { destination -> navController.navigate(destination) }, | ||
id = backStackEntry.arguments?.getString(KeyId).orEmpty() | ||
) | ||
} | ||
|
||
composable(destination = AppDestination.Third) { | ||
ThirdScreen( | ||
navigator = { destination -> navController.navigate(destination) }, | ||
model = navController.previousBackStackEntry?.savedStateHandle?.get<UiModel>( | ||
KeyModel | ||
) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...hq/sample/compose/ui/screens/home/Item.kt → ...mple/compose/ui/screens/main/home/Item.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ample/compose/ui/screens/home/ItemList.kt → .../compose/ui/screens/main/home/ItemList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...pose/ui/screens/second/SecondViewModel.kt → ...ui/screens/main/second/SecondViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ompose/ui/screens/third/ThirdViewModel.kt → ...e/ui/screens/main/third/ThirdViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...compose/ui/screens/home/HomeScreenTest.kt → ...se/ui/screens/main/home/HomeScreenTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters