Open ydhnwb opened 1 year ago
That is how I do it atm and it works just fine as long as you don't need to share instances. You're just passing in an instance via a constructor. Nothing special.
If you want to share instances you might want to use a CompositionLocalProvider or something. I recommend looking at the source code for Koin to get inspiration on how a DI framework does it. =)
The only think to watch out for is that you might need a unique screen id on the screen to tell Voyager that it's a different screen. Otherwise your model might be resued with the same id as you used in the constructor (someone please correct me if I'm wrong here).
Also, unless you need to use getItem
in more places (e.g. refresh), you could just fetch the item in the init function.
init {
coroutineScope.launch {
delay(1_000)
mutableState.value = State.Result("Item #$index")
}
}
I have ScreenModel and StateScreenModel like this:
I want my ScreenModel or StateScreenModel have an instance to NoteDataSource, like this:
But I don't know where to add. Is it safe to add the instance inside the Screen like this below?
Or might be there is a better solution?