Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I integrate koin in compose multiplatform with koin ViewModel?? #262

Open
shohrab-swl opened this issue Dec 18, 2023 · 8 comments
Open

Comments

@shohrab-swl
Copy link

shohrab-swl commented Dec 18, 2023

val viewmodel: MyViewModel= getViewModel(
    key = MyViewModel::class.getFullName(),
    factory = viewModelFactory { MyViewModel(userDataRepository) }

Is it possible to koinInject??

@NikhilBhutani
Copy link

@shohrab-swl Yes, you can.
Inject ViewModelFactory and access it an Koin Object.

And in your composable use your above code snippet and instead of declaring
viewModelFactory { MyViewModel(userDataRepository) }
Instead get your koin generated viewmodelfactory object.

I have created an example here - https://github.com/NikhilBhutani/ComposeMultiplatformNewsAppDemo/blob/main/shared/src/commonMain/kotlin/com/niko/kmm/newsappdemocomposemultiplatform/di/PresentationModule.kt

@NikhilBhutani
Copy link

Hi @Alex009 , Can we please mark this as resolved ?

@shohrab-swl
Copy link
Author

That code doesn't work when I create a new ViewModel.

@NikhilBhutani
Copy link

@shohrab-swl Can you please share your repo so that I can check whats not working for you? Or maybe you can clone my repo above and add api key to inspect how it's working. ? Let me know, glad to help out in any way.

@shohrab-swl
Copy link
Author

NewsAppDemoComposeMultiplatform.zip

fun presentationModule() = module {
single {
viewModelFactory { NewsHomeViewModel(get()) }
}
single {
viewModelFactory { NewsDetailsViewModel(get()) }
}
}

object ViewModelsFac : KoinComponent {
fun getNewsHomeViewModelFactory() = get<ViewModelFactory>()
fun getNewsDetailsViewModelFactory() = get<ViewModelFactory>()
}

I just changed a few lines.

and i get this error

java.lang.ClassCastException: com.niko.kmm.newsappdemocomposemultiplatform.presentation.details.NewsDetailsViewModel cannot be cast to com.niko.kmm.newsappdemocomposemultiplatform.presentation.NewsHomeViewModel

@NikhilBhutani
Copy link

Thanks for sharing this @shohrab-swl , Can you please also add an issue in the original repo, I'll fix this.

@NikhilBhutani
Copy link

NikhilBhutani commented Jan 17, 2024

@shohrab-swl You can do this.

object ViewModelsFac : KoinComponent { fun getNewsHomeViewModelFactory() = viewModelFactory { NewsHomeViewModel(get()) } fun getNewsDetailViewModelFactory() = viewModelFactory { NewsDetailsViewModel() } }

and access it with getViewModel in your composable.

@slikasgiedrius
Copy link

It's hard to understand the concept, but I made it work based on this conversation. I suggest you to add this case to the instructions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants