From 9c76617700d901b9fceaa80ae38bb8aeab99ffa9 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 25 May 2025 17:52:37 +0530 Subject: [PATCH] refactor: properly use TopLevelRoute --- .../android/ui/navigation/ClawBackStack.kt | 23 +++++++++++++++++-- .../claw/android/ui/screens/Nav3Screen.kt | 2 -- 2 files changed, 21 insertions(+), 4 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 d7b7db59..7f2870f0 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,14 +22,33 @@ 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(startRoute: T) { +class ClawBackStack(private val startRoute: T) { + /** + * Marker interface for routes 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. + */ interface TopLevelRoute val backStack = mutableStateListOf(startRoute) - /** Pushes a new destination onto the stack. */ + /** + * 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. + */ fun add(route: T) { logCurrentState("add") + if (route is TopLevelRoute) { + backStack.clear() + if (route != startRoute) { + backStack.add(startRoute) + } + } backStack.add(route) } 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 f4d5c418..12e35984 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 @@ -78,7 +78,6 @@ import dev.msfjarvis.claw.common.urllauncher.UrlLauncher import dev.msfjarvis.claw.common.user.UserProfile import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentMapOf -import kotlinx.collections.immutable.toPersistentList import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class) @@ -131,7 +130,6 @@ fun Nav3Screen( coroutineScope.launch { if (savedPosts.isNotEmpty()) savedListState.scrollToItem(0) } }, ) - val navDestinations = navItems.map(NavigationItem::destination).toPersistentList() // endregion val postActions = remember {