android: inline routes

This commit is contained in:
Harsh Shandilya 2022-03-02 13:39:31 +05:30
parent 9fa8775d4c
commit de28ae2d95
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
4 changed files with 12 additions and 18 deletions

View file

@ -84,12 +84,12 @@ fun LobstersApp(
listOf( listOf(
NavigationItem( NavigationItem(
label = "Hottest", label = "Hottest",
route = Destinations.Hottest.getRoute(), route = Destinations.Hottest.route,
icon = painterResource(R.drawable.ic_whatshot_24dp), icon = painterResource(R.drawable.ic_whatshot_24dp),
) { coroutineScope.launch { networkListState.animateScrollToItem(index = 0) } }, ) { coroutineScope.launch { networkListState.animateScrollToItem(index = 0) } },
NavigationItem( NavigationItem(
label = "Saved", label = "Saved",
route = Destinations.Saved.getRoute(), route = Destinations.Saved.route,
icon = painterResource(commonR.drawable.ic_favorite_24dp), icon = painterResource(commonR.drawable.ic_favorite_24dp),
) { coroutineScope.launch { savedListState.animateScrollToItem(index = 0) } }, ) { coroutineScope.launch { savedListState.animateScrollToItem(index = 0) } },
) )
@ -139,11 +139,11 @@ fun LobstersApp(
) { paddingValues -> ) { paddingValues ->
NavHost( NavHost(
navController, navController,
startDestination = Destinations.startDestination.getRoute(), startDestination = Destinations.startDestination.route,
) { ) {
val uri = LobstersApi.BASE_URL val uri = LobstersApi.BASE_URL
composable( composable(
route = Destinations.Hottest.getRoute(), route = Destinations.Hottest.route,
deepLinks = deepLinks =
listOf(navDeepLink { uriPattern = uri }, navDeepLink { uriPattern = "$uri/" }), listOf(navDeepLink { uriPattern = uri }, navDeepLink { uriPattern = "$uri/" }),
) { ) {
@ -157,7 +157,7 @@ fun LobstersApp(
modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()), modifier = Modifier.padding(bottom = paddingValues.calculateBottomPadding()),
) )
} }
composable(Destinations.Saved.getRoute()) { composable(Destinations.Saved.route) {
setWebUri(null) setWebUri(null)
DatabasePosts( DatabasePosts(
items = savedPosts, items = savedPosts,
@ -168,7 +168,7 @@ fun LobstersApp(
) )
} }
composable( composable(
route = Destinations.Comments.getRoute("{postId}"), route = Destinations.Comments.route,
arguments = listOf(navArgument("postId") { type = NavType.StringType }), arguments = listOf(navArgument("postId") { type = NavType.StringType }),
deepLinks = deepLinks =
listOf( listOf(

View file

@ -38,7 +38,7 @@ fun ClawNavigationBar(
return@NavigationBarItem return@NavigationBarItem
} }
navController.popBackStack(navController.graph.startDestinationRoute!!, false) navController.popBackStack(navController.graph.startDestinationRoute!!, false)
if (navItem.route != Destinations.startDestination.getRoute()) { if (navItem.route != Destinations.startDestination.route) {
navController.navigate(navItem.route) navController.navigate(navItem.route)
} }
} }

View file

@ -91,7 +91,7 @@ fun rememberPostActions(
} }
override fun viewComments(postId: String) { override fun viewComments(postId: String) {
navController.navigate(Destinations.Comments.getRoute(postId)) navController.navigate(Destinations.Comments.route)
} }
override fun viewCommentsPage(commentsUrl: String) { override fun viewCommentsPage(commentsUrl: String) {

View file

@ -1,17 +1,11 @@
package dev.msfjarvis.claw.android.ui.navigation package dev.msfjarvis.claw.android.ui.navigation
sealed class Destinations(internal val route: String) { sealed class Destinations(val route: String) {
object Hottest : Destinations("hottest") { object Hottest : Destinations("hottest")
fun getRoute() = route
}
object Saved : Destinations("saved") { object Saved : Destinations("saved")
fun getRoute() = route
}
object Comments : Destinations("comments/%s") { object Comments : Destinations("comments/{postId}")
fun getRoute(postId: String) = route.format(postId)
}
companion object { companion object {
val startDestination val startDestination