refactor: properly use TopLevelRoute

This commit is contained in:
Harsh Shandilya 2025-05-25 17:52:37 +05:30
parent e011870986
commit 9c76617700
2 changed files with 21 additions and 4 deletions

View file

@ -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 * 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]. * structure, many APIs in this class inverse of identically named functions on [List].
*/ */
class ClawBackStack<T : NavKey>(startRoute: T) { class ClawBackStack<T : NavKey>(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 interface TopLevelRoute
val backStack = mutableStateListOf(startRoute) 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) { fun add(route: T) {
logCurrentState("add") logCurrentState("add")
if (route is TopLevelRoute) {
backStack.clear()
if (route != startRoute) {
backStack.add(startRoute)
}
}
backStack.add(route) backStack.add(route)
} }

View file

@ -78,7 +78,6 @@ import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
import dev.msfjarvis.claw.common.user.UserProfile import dev.msfjarvis.claw.common.user.UserProfile
import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentMapOf import kotlinx.collections.immutable.persistentMapOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@ -131,7 +130,6 @@ fun Nav3Screen(
coroutineScope.launch { if (savedPosts.isNotEmpty()) savedListState.scrollToItem(0) } coroutineScope.launch { if (savedPosts.isNotEmpty()) savedListState.scrollToItem(0) }
}, },
) )
val navDestinations = navItems.map(NavigationItem::destination).toPersistentList()
// endregion // endregion
val postActions = remember { val postActions = remember {