From beb1943ee6f9534b07de178c2f941a36d65f0b0b Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 25 May 2025 17:54:17 +0530 Subject: [PATCH] refactor: consistently use destination over route --- .../android/ui/navigation/ClawBackStack.kt | 36 +++++++++---------- .../claw/android/ui/navigation/Destination.kt | 7 ++-- .../claw/android/ui/screens/Nav3Screen.kt | 8 ++--- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/navigation/ClawBackStack.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/navigation/ClawBackStack.kt index 7f2870f0..8c683b6f 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/navigation/ClawBackStack.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/navigation/ClawBackStack.kt @@ -22,41 +22,41 @@ import io.github.aakira.napier.Napier * to the front while lists add behind. To counter these expectations with the actual backing data * structure, many APIs in this class inverse of identically named functions on [List]. */ -class ClawBackStack(private val startRoute: T) { +class ClawBackStack(private val initialDestination: T) { /** - * Marker interface for routes that occupy the "top" level of the back stack. + * Marker interface for destinations that occupy the "top" level of the back stack. * * This is used by [dev.msfjarvis.claw.android.ui.navigation.ClawBackStack.add] to ensure that - * duplicate instances of these routes do not end up in the back stack. + * duplicate instances of these destinations do not end up in the back stack. */ - interface TopLevelRoute + interface TopLevelDestination - val backStack = mutableStateListOf(startRoute) + val backStack = mutableStateListOf(initialDestination) /** * Pushes a new destination onto the stack. * - * Top level routes are specially handled to ensure that the distance between the incoming [route] - * and the [startRoute] cannot have anything in between. This prevents users from getting stuck in - * a frustratingly long stack of top level destinations that are so easily accessible that they - * have no reason to be on the stack. + * Top level destinations are specially handled to ensure that the distance between the incoming + * [destination] and the [initialDestination] cannot have anything in between. This prevents users + * from getting stuck in a frustratingly long stack of top level destinations that are so easily + * accessible that they have no reason to be on the stack. */ - fun add(route: T) { + fun add(destination: T) { logCurrentState("add") - if (route is TopLevelRoute) { + if (destination is TopLevelDestination) { backStack.clear() - if (route != startRoute) { - backStack.add(startRoute) + if (destination != initialDestination) { + backStack.add(initialDestination) } } - backStack.add(route) + backStack.add(destination) } - /** Checks if the "top" item in the back stack is an instance of [TopLevelRoute]. */ - fun isOnTopLevelRoute(): Boolean { - logCurrentState("hasTopLevelDestination") + /** Checks if the "top" item in the back stack is an instance of [TopLevelDestination]. */ + fun isOnTopLevelDestination(): Boolean { + logCurrentState("isOnTopLevelDestination") val top = firstOrNull() - return (top != null && top is TopLevelRoute) + return (top != null && top is TopLevelDestination) } fun firstOrNull(): T? { diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/navigation/Destination.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/navigation/Destination.kt index 62fe7d2a..5d04e602 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/navigation/Destination.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/navigation/Destination.kt @@ -16,16 +16,17 @@ import androidx.compose.material.icons.outlined.NewReleases import androidx.compose.material.icons.outlined.Whatshot import androidx.compose.ui.graphics.vector.ImageVector import androidx.navigation3.runtime.NavKey +import dev.msfjarvis.claw.android.ui.navigation.ClawBackStack.TopLevelDestination import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable sealed interface Destination : Parcelable, NavKey -@Parcelize @Serializable data object Hottest : Destination, ClawBackStack.TopLevelRoute +@Parcelize @Serializable data object Hottest : Destination, TopLevelDestination -@Parcelize @Serializable data object Newest : Destination, ClawBackStack.TopLevelRoute +@Parcelize @Serializable data object Newest : Destination, TopLevelDestination -@Parcelize @Serializable data object Saved : Destination, ClawBackStack.TopLevelRoute +@Parcelize @Serializable data object Saved : Destination, TopLevelDestination @Parcelize @Serializable data class Comments(val postId: String) : Destination diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/Nav3Screen.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/Nav3Screen.kt index 12e35984..04673940 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/Nav3Screen.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/Nav3Screen.kt @@ -141,7 +141,7 @@ fun Nav3Screen( TopAppBar( modifier = Modifier.shadow(8.dp), navigationIcon = { - if (!(clawBackStack.isOnTopLevelRoute())) { + if (!(clawBackStack.isOnTopLevelDestination())) { IconButton( onClick = { if (clawBackStack.removeLastOrNull() == null) activity?.finish() } ) { @@ -159,12 +159,12 @@ fun Nav3Screen( } }, title = { - if (clawBackStack.isOnTopLevelRoute()) { + if (clawBackStack.isOnTopLevelDestination()) { Text(text = stringResource(R.string.app_name), fontWeight = FontWeight.Bold) } }, actions = { - if (clawBackStack.isOnTopLevelRoute()) { + if (clawBackStack.isOnTopLevelDestination()) { IconButton(onClick = { clawBackStack.add(Search) }) { Icon(imageVector = Icons.Filled.Search, contentDescription = "Search posts") } @@ -180,7 +180,7 @@ fun Nav3Screen( ClawNavigationBar( clawBackStack, items = navItems, - isVisible = clawBackStack.isOnTopLevelRoute(), + isVisible = clawBackStack.isOnTopLevelDestination(), hazeState = hazeState, ) }