sebaslogen / resaca

Compose Multiplatform library to scope ViewModels to a Composable, surviving configuration changes and navigation
MIT License
392 stars 9 forks source link

Koin is not supported #6

Closed OKatrych closed 1 year ago

OKatrych commented 1 year ago

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
)
sebaslogen commented 1 year ago

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.

OKatrych commented 1 year ago

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

sebaslogen commented 1 year ago

No problem, I'm glad it worked 🙂

sebaslogen commented 1 year ago

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

OKatrych commented 1 year ago

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