Joe Jensen
07/09/2020, 11:46 PMZach Klippenstein (he/him) [MOD]
07/09/2020, 11:47 PMJoost Klitsie
07/10/2020, 11:46 AMambient
that holds your DI object (the DI object in Kodein is basically a component in Dagger terms)
An ambient is passed on down the composable function tree, and you can Provide different values for it. So you can with that extend the component by making a subDI on the current DI object (so basically create sub components)
The code for making this happen is rather simple:
val DI = ambientOf { DI {} }
@Composable
inline fun <reified T : Any> instance(): DIProperty<T> = DI.current.instance()
@Composable
fun composeSubDI(
bind: DI.MainBuilder.() -> Unit,
content: @Composable () -> Unit
) = Providers(
DI provides subDI(DI.current, init = bind),
children = content
)
Then your app can look something like this:
```override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Providers(
DI provides di // the activity/application di object (aka component)
) {
someChild(1)
someChild(2)
someChild(3)
}
}
}
@Composable
fun someChild(argument: Int) {
composeSubDI(
{
import(SomeViewModule())
}
) {
// DI.current is now whatever sub component of the component above
val viewModel by DI.current.instance<MyViewModel>(argument)
Text(text = viewModel.state.asState().someText)
}
}
Now you have for example the main component, with 3 sub components next to each other (and their bindings will not conflict as they are separate sub components)Joost Klitsie
07/10/2020, 11:48 AMshikasd
07/10/2020, 12:58 PMZach Klippenstein (he/him) [MOD]
07/10/2020, 1:05 PMZach Klippenstein (he/him) [MOD]
07/10/2020, 1:09 PMJoost Klitsie
07/10/2020, 1:11 PMJoost Klitsie
07/10/2020, 1:12 PMZach Klippenstein (he/him) [MOD]
07/10/2020, 1:30 PMZach Klippenstein (he/him) [MOD]
07/10/2020, 1:32 PMJoost Klitsie
07/10/2020, 4:07 PM