sebaslogen / resaca

Android library to scope ViewModels to a Composable, surviving configuration changes and navigation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Koin is not supported

OKatrych opened this issue · comments

When trying to use the viewModelScoped with Koin's koinInject() the compilation throws the error: "Composable calls are not allowed inside the builder".
Please note that koinInject() is a @Composable function and it's usage is not allowed inside the non-composable noinline builder: @DisallowComposableCalls () -> T.

Example:

@Composable
@Preview
private fun DialogContent(
    modifier: Modifier = Modifier,
    viewModel: SubscriptionViewModel = viewModelScoped { koinInject() }, // Error
)

Have you tried with the get() function from Koin?

https://insert-koin.io/docs/reference/koin-android/get-instances/

I assume the one you mentioned koinInject() tries to automatically manage the lifecycle of your view model through the composable. Since you're using resaca to handle the lifecycle of the view model, the Koin library doesn't need to take that responsibility anymore.

My bad, totally forgot that this can be retrieved from the activity.

No problem, I'm glad it worked 🙂

FYI: I have just added more documentation and examples on how to use resaca with Koin, plus a new extension library to support ViewModels with SavedStateHandle using Koin and resaca https://github.com/sebaslogen/resaca/blob/main/resacakoin/Readme.md

@sebaslogen this is 🥇. Thank you a lot 🙂.