android: make navigation destinations more strongly typed

This commit is contained in:
Harsh Shandilya 2022-01-19 12:45:59 +05:30
parent 3590ce0b59
commit 7dbedf7e82
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 15 additions and 10 deletions

View file

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

View file

@ -51,7 +51,7 @@ fun LobstersApp(
val scaffoldState = rememberScaffoldState()
val listState = rememberLazyListState()
val navController = rememberNavController()
var currentDestination by remember { mutableStateOf(Destinations.Hottest) }
var currentDestination by remember { mutableStateOf(Destinations.Hottest.getRoute()) }
var isFabVisible by remember { mutableStateOf(false) }
val nestedScrollConnection = remember {
object : NestedScrollConnection {
@ -76,7 +76,7 @@ fun LobstersApp(
}
override fun viewComments(postId: String) {
navController.navigate(Destinations.Comments.format(postId))
navController.navigate(Destinations.Comments.getRoute(postId))
}
override fun viewCommentsPage(commentsUrl: String) {
@ -89,7 +89,7 @@ fun LobstersApp(
}
}
navController.addOnDestinationChangedListener { _, destination, _ ->
currentDestination = destination.route ?: Destinations.Hottest
currentDestination = destination.route ?: Destinations.Hottest.getRoute()
}
LobstersTheme(
providedValues =
@ -113,14 +113,14 @@ fun LobstersApp(
topBar = { ClawAppBar(modifier = Modifier.statusBarsPadding()) },
floatingActionButton = {
ClawFab(
isFabVisible = isFabVisible && currentDestination == Destinations.Hottest,
isFabVisible = isFabVisible && currentDestination == Destinations.Hottest.getRoute(),
listState = listState,
modifier = Modifier.navigationBarsPadding(),
)
},
) {
NavHost(navController, startDestination = Destinations.Hottest) {
composable(Destinations.Hottest) {
NavHost(navController, startDestination = Destinations.Hottest.getRoute()) {
composable(Destinations.Hottest.getRoute()) {
setWebUri("https://lobste.rs/")
HottestPosts(
items,
@ -131,7 +131,7 @@ fun LobstersApp(
Modifier.nestedScroll(nestedScrollConnection),
)
}
composable(Destinations.Comments.format("{postId}")) { backStackEntry ->
composable(Destinations.Comments.getRoute("{postId}")) { backStackEntry ->
val postId = requireNotNull(backStackEntry.arguments?.getString("postId"))
setWebUri("https://lobste.rs/s/$postId")
CommentsPage(