InsertKoinIO / koin-annotations

Koin Annotations - About Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform insert-koin.io
https://insert-koin.io
Apache License 2.0
123 stars 30 forks source link

Add KOIN_VIEWMODEL_KMP for support KMP ViewModel #133

Open heesung6701 opened 3 weeks ago

heesung6701 commented 3 weeks ago

Summary

I added new KoinViewModel annotation for supporting KMP

Content

with koin-annotations:1.3.1 and lifecycle-viewmodel:2.0.0, generated kt file use org.koin.compose.viewmodel.dsl.viewModel rather than org.koin.androidx.viewmodel.dsl.viewModel.

because Koin-annotation compiler can't know version of lifecycle-viewmodel's version, I added org.koin.compose.viewmodel.dsl.viewModel for koinViewModel with KMP.

Implementation

I added KOIN_VIEWMODEL_KMP as new Definition Annotation

Example

below is exampled of generated Kotlin file

ASIS

package org.koin.ksp.generated

import org.koin.core.module.Module
import org.koin.dsl.*
import org.koin.androidx.viewmodel.dsl.viewModel

public val com_bravepeople_devevent_feature_bookmark_BookmarkModule : Module = module {
    viewModel() { com.bravepeople.devevent.feature.bookmark.BookmarkEventViewModel(getBookmarkEventUseCase=get(),unBookmarkEventUseCase=get(),deleteAllEventBookmarkUseCase=get()) } 
}
public val com.bravepeople.devevent.feature.bookmark.BookmarkModule.module : org.koin.core.module.Module get() = com_bravepeople_devevent_feature_bookmark_BookmarkModule

TOBE

package org.koin.ksp.generated

import org.koin.core.module.Module
import org.koin.dsl.*
import org.koin.compose.viewmodel.dsl.viewModel

public val com_bravepeople_devevent_feature_bookmark_BookmarkModule : Module = module {
    viewModel() { com.bravepeople.devevent.feature.bookmark.BookmarkEventViewModel(getBookmarkEventUseCase=get(),unBookmarkEventUseCase=get(),deleteAllEventBookmarkUseCase=get()) } 
}
public val com.bravepeople.devevent.feature.bookmark.BookmarkModule.module : org.koin.core.module.Module get() = com_bravepeople_devevent_feature_bookmark_BookmarkModule
arnaudgiuliani commented 6 days ago

Hello, thanks for your contribution.

What would be the need to make a difference, if at the end we use the same API to declare a ViewModel?

heesung6701 commented 6 days ago

Hello, thanks for your contribution.

What would be the need to make a difference, if at the end we use the same API to declare a ViewModel?

we just want to use koin-annotation with lifecycle-viewmodel:2.0.0. but now, we can't use because koin-annotation doesn't support new viewmodel that moved from androidMain to commonMain.

this PR is just my suggestion for support two kind of viewModel between androidMain(previous) and commonMain(new).

Support Two ViewModel (old and new)

we need to support two version of ViewModel. first one is ViewModel on AndroidMain, and second one is ViewModel on commonMain that released on latest version for support KMP.

I hoped to defined minimum version of lifecycle-viewmodel on koin-annotation, but we can't because we don't know which version used on host.

so I just make two definition and delegated to select via host

koin-annotation also need to support KMP

for support KMP, androidx's viewmodel moved from androidMain to commonMain. then host's viewmodel also moved to commonMain. but current version of koin-annotation doesn't support viewmodel on common main. they only support for VM on androidMain. so host who want to migrate to latest version of lifecycle-viewmode, they can't use KoinViewModel of koin-annotation.