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