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

xxfast

10/01/2023, 8:59 PM
Hi @Arkadii Ivanov With Decompose, do you need to have a component for a screen to be a part of a navigation? i.e can simple screen exist without any instance attached to it? More context in the 🧵
Here, i have a simple screen
IslandScreen
which is just a wrapper around a simple view. However, in order to show this screen alongside with all the other screens, I need to declare an
IslandComponent
.
Copy code
@Composable
fun MainUi(component: MainComponent) {
  Children(..) {
    var numbers by remember { mutableStateOf(listOf(1, 2, 3)) }
    when(val instance: ComponentContext = it.instance){
      is IslandComponent -> IslandUi(instance)
      is ShipComponent -> ShipUi(instance)
      is DockComponent -> DockUi(instance)
    }
  }
}
This is because the
childFactory
of
MainComponent
here, requires you to provide a component
Copy code
val childStack: Value<ChildStack<Config, ComponentContext>> = childStack(
    source = navigation,
    ..
    childFactory = { configuration, componentContext ->
      when(configuration){
        Island -> IslandComponent(componentContext) { .. }

        Ship -> ShipComponent(
          componentContext = componentContext,
          ..
        )

        Dock ->  DockComponent(componentContext = componentContext) { .. }
      }
    }
  )
a

Arkadii Ivanov

10/01/2023, 9:05 PM
Any entity can be a component, there is no need to pass the child ComponentContext if it's unused.
x

xxfast

10/01/2023, 9:06 PM
but the
childFactory
would still require to create an instance for
Island
configuration, even though it is not used by the ui - right?
a

Arkadii Ivanov

10/01/2023, 9:09 PM
Configurations are not used by the UI, typically. Configurations are for the navigation to properly manage lifecycles, and for you to properly create child components. Also, childFactory doesn't create configurations.
👍 1
In childFactory you need to create an instance of a component, that you will use in the UI.
x

xxfast

10/01/2023, 9:12 PM
in the example above, i'm trying to get rid of the
IslandComponent
all-together because this particular screen doesn't have any logic - as it just basically shows a image. However, this won't be possible as
childFactory
will need an instance to be created and stored in the
childStack
- is that correct?
a

Arkadii Ivanov

10/01/2023, 9:15 PM
You need to return an instance of any type from childFactory. Your IslandComponent holds a callback, which is used by the UI. So it seems reasonable to wrap it with a class. Right? You could just return the callback itself, and then just pass the callback into IslandUi. But I find this confusing.
You can also return Unit, if you don't need anything at all.
x

xxfast

10/01/2023, 9:18 PM
ah right 👍 for some reason i assumed it has to return a
ComponentContext
but as you mentioned, it can return
Any
so a Unit makes more sense here
a

Arkadii Ivanov

10/01/2023, 9:20 PM
In your example, you are returning components wrapping ComponentContext, not ComponentContext itself. So yeah, any type can be returned.
x

xxfast

10/01/2023, 9:20 PM
thanks arkadii
👍 1
a

Arkadii Ivanov

10/01/2023, 9:23 PM
Btw, I thought you were using Decompose-Router for navigation. Did something change?
x

xxfast

10/01/2023, 9:48 PM
i still am. I'm also working on a my talk for devfest where i compare and contrast navigation libraries and their apis. Wanted to have set of slides where i showcase decompose by itself without the router-api
a

Arkadii Ivanov

10/01/2023, 9:50 PM
Got it. Thanks!
2 Views