https://kotlinlang.org logo
#decompose
Title
# decompose
e

electrolobzik

03/19/2024, 5:18 PM
@Arkadii Ivanov Is there any simple solution for logging navigation changes in the component tree? Since we don’t have any centralised
router
which could make this logging, I am looking in direction of making a copy of
DefaultStackNavigation
but I really don’t like this solution much. Also it doesn’t cover slot navigation and has a lot of other cons.
a

Arkadii Ivanov

03/19/2024, 5:28 PM
Currently, I don't see any solution that could be applied globally. You can create a logging wrapper for childFactory, but that needs to be applied in every place.
Or wrap StackNavigation, but same cons here.
e

electrolobzik

03/19/2024, 5:33 PM
It would be cool to have the flexibility of current implementation plus features of centralised event-based things like MVI in one place.
😃
Indeed the navigation part of the component being in fact stateful can be implemented in theory as a MVI-like state machine with all it’s features like middleware and logging
Thanks for the feedback!
a

Arkadii Ivanov

03/19/2024, 5:53 PM
It would be possible if the
children
extension function would be part of the ComponentContext interface. In this case one could create a custom component context and wrap the children call. I will think about it, but currently this looks overcomplicated.
e

electrolobzik

03/19/2024, 6:00 PM
Maybe it’s possible to add a parameter to
childStack
method which will allow to pass a lambda for logging stack changes?
but this still is not a global setting
a

Arkadii Ivanov

03/19/2024, 6:06 PM
Maybe it’s possible to add a parameter to childStack method which will allow to pass a lambda for logging stack changes?
This is essentially equivalent to creating a custom wrapper for StackNavigation. This could be as simple as
Copy code
val nav = LoggingStackNavigation<Config>()
e

electrolobzik

03/19/2024, 6:07 PM
by “wrapper” do you mean creating another variant of
DefaultStackNavigation
?
a

Arkadii Ivanov

03/19/2024, 6:08 PM
Or
Copy code
val nav = StackNavigation<Config>()
val stack = childStack(source = nav.logged(), ...)
Yeah, create a custom implementation that delegates to the standard StackNavigation.
e

electrolobzik

03/19/2024, 6:10 PM
yeah, I was originally thinking in that direction. This may help if replace navigation with the wrapper everywhere. thanks!
👍 1
a

Arkadii Ivanov

03/19/2024, 7:08 PM
This is what I came up with.
Copy code
class LoggingStackNavigation<C : Any>(
    private val delegate: StackNavigation<C> = StackNavigation(),
) : StackNavigation<C>, StackNavigationSource<C> by delegate {
    override fun navigate(
        transformer: (stack: List<C>) -> List<C>,
        onComplete: (newStack: List<C>, oldStack: List<C>) -> Unit,
    ) {
        delegate.navigate(transformer) { newStack, oldStack ->
            println("Stack changed to $newStack. Old stack: $oldStack.")
            onComplete(newStack, oldStack)
        }
    }
}
👍 2
thank you color 1
2 Views