mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 14:37:04 +05:30
refactor: cleanup ClawBackStack implementation
This commit is contained in:
parent
fe5a6f0a93
commit
e92017173d
1 changed files with 4 additions and 36 deletions
|
@ -8,7 +8,6 @@ package dev.msfjarvis.claw.android.ui.navigation
|
||||||
|
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
import androidx.navigation3.runtime.NavKey
|
import androidx.navigation3.runtime.NavKey
|
||||||
import io.github.aakira.napier.Napier
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Naive implementation of a simple navigation back stack, backed by a
|
* Naive implementation of a simple navigation back stack, backed by a
|
||||||
|
@ -20,7 +19,7 @@ import io.github.aakira.napier.Napier
|
||||||
* Stacks function in a completely opposite order of lists, which means when you call
|
* Stacks function in a completely opposite order of lists, which means when you call
|
||||||
* first/firstOrNull on a stack, you expect to receive the item you added *last*, since stacks add
|
* first/firstOrNull on a stack, you expect to receive the item you added *last*, since stacks add
|
||||||
* 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 are the inverse of identically named functions on [List].
|
||||||
*/
|
*/
|
||||||
class ClawBackStack(private val initialDestination: NavKey) {
|
class ClawBackStack(private val initialDestination: NavKey) {
|
||||||
/**
|
/**
|
||||||
|
@ -42,7 +41,6 @@ class ClawBackStack(private val initialDestination: NavKey) {
|
||||||
* accessible that they have no reason to be on the stack.
|
* accessible that they have no reason to be on the stack.
|
||||||
*/
|
*/
|
||||||
fun add(destination: NavKey) {
|
fun add(destination: NavKey) {
|
||||||
logCurrentState("add")
|
|
||||||
if (destination is TopLevelDestination) {
|
if (destination is TopLevelDestination) {
|
||||||
backStack.clear()
|
backStack.clear()
|
||||||
if (destination != initialDestination) {
|
if (destination != initialDestination) {
|
||||||
|
@ -54,41 +52,11 @@ class ClawBackStack(private val initialDestination: NavKey) {
|
||||||
|
|
||||||
/** Checks if the "top" item in the back stack is an instance of [TopLevelDestination]. */
|
/** Checks if the "top" item in the back stack is an instance of [TopLevelDestination]. */
|
||||||
fun isOnTopLevelDestination(): Boolean {
|
fun isOnTopLevelDestination(): Boolean {
|
||||||
logCurrentState("isOnTopLevelDestination")
|
|
||||||
val top = firstOrNull()
|
val top = firstOrNull()
|
||||||
return (top != null && top is TopLevelDestination)
|
return top != null && top is TopLevelDestination
|
||||||
}
|
}
|
||||||
|
|
||||||
fun firstOrNull(): NavKey? {
|
fun firstOrNull() = backStack.lastOrNull()
|
||||||
logCurrentState("firstOrNull")
|
|
||||||
return backStack.lastOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun lastOrNull(): NavKey? {
|
|
||||||
logCurrentState("lastOrNull")
|
|
||||||
return backStack.firstOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeLastOrNull(): NavKey? {
|
|
||||||
logCurrentState("removeLastOrNull")
|
|
||||||
return backStack.removeLastOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(msfjarvis): Remove before shipping
|
|
||||||
private fun logCurrentState(methodName: String) {
|
|
||||||
val backStack = this.backStack
|
|
||||||
Napier.d(tag = LOG_TAG) {
|
|
||||||
buildString {
|
|
||||||
appendLine("State of ClawBackStack(${this@ClawBackStack})")
|
|
||||||
appendLine("Caller: $methodName")
|
|
||||||
appendLine("Top: ${backStack.firstOrNull()}")
|
|
||||||
appendLine("Bottom: ${backStack.lastOrNull()}")
|
|
||||||
appendLine("Current entries: ${backStack.joinToString(", ")}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private companion object {
|
fun removeLastOrNull() = backStack.removeLastOrNull()
|
||||||
private const val LOG_TAG = "ClawBackStack"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue