koin icon indicating copy to clipboard operation
koin copied to clipboard

Support androidx viewmodel for kotlin multiplatform

Open Vivecstel opened this issue 1 year ago • 33 comments

https://developer.android.com/jetpack/androidx/releases/lifecycle#2.8.0-alpha03 added view model support for kotlin multiplatform. Ideally koin should support this also. Thanks

Vivecstel avatar Mar 21 '24 08:03 Vivecstel

Can you explain how to add viewmodel library in kmm

fluttertutorialin avatar Mar 22 '24 05:03 fluttertutorialin

Hello, some reference here

  • https://touchlab.co/kmp-viewmodel

A full demo how to use viewmodel here :

  • https://github.com/joreilly/FantasyPremierLeague

It should be OK that koin move the viewmodel support to KMP.

frankois1234 avatar Apr 03 '24 12:04 frankois1234

The is the official repo used in google codelabe https://github.com/MatkovIvan/nav_cupcake

ghost avatar Apr 04 '24 10:04 ghost

@frankois1234 it doesn't work with koin .... iam getting this error

Uncaught Kotlin exception: org.koin.core.error.InstanceCreationException: Could not create instance for '[Factory:'viewModel class Name']' at 0 shared

also I am creating this viewModel by factory or single ..... I think koin should support the viewModel{} at the common main

AbdelrahmanEsam avatar Apr 08 '24 10:04 AbdelrahmanEsam

@AbdelrahmanEsam

Inspired by https://github.com/MatkovIvan/nav_cupcake/blob/master/composeApp/src/commonMain/kotlin/com/matkovivan/nav_cupcake/ViewModels.kt which uses androidx viewmodel. I'm using the following code to make it work with Koin:

ViewModels.kt in commonMain:

internal expect fun <VM : ViewModel> viewModel(
    modelClass: KClass<VM>,
    factory: ViewModelProvider.Factory = rememberViewModelFactory()
): VM

// Can't be private as it causes Exception during IR lowering
@Composable
internal fun rememberViewModelFactory(): ViewModelProvider.Factory {
    val koin = getKoin()
    return remember {
        viewModelFactory {
            initializer { MyViewModel(myDependency = koin.get()) }
            ... // more VM initializers
        }
    }
}

ViewModels.android.kt in androidMain:

@Composable
internal actual fun <VM : ViewModel> viewModel(
    modelClass: KClass<VM>,
    factory: ViewModelProvider.Factory
): VM = androidx.lifecycle.viewmodel.compose.viewModel(modelClass.java, factory = factory)

ViewModels.ios.kt in iosMain:

@Composable
internal actual fun <VM : ViewModel> viewModel(
    modelClass: KClass<VM>,
    factory: ViewModelProvider.Factory
): VM = androidx.lifecycle.viewmodel.compose.viewModel(modelClass, factory = factory)

And wiring it in the Composables:

@Composable
fun MyScreen(
    myViewModel: MyViewModel = viewModel(MyViewModel::class)
) {

}

I doesn't guarantee that it's the most correct approach, but it works and can be a workaround until Koin officially suports it.

Leedwon avatar Apr 09 '24 10:04 Leedwon

@Leedwon the problem is on the ios side not the android one .... android is working perfectly

class DIHelper : KoinComponent { private val notesViewModel: NotesViewModel by inject() private val noteDetailsViewModel: NoteDetailsViewModel by inject()

fun getNotesViewModel(): NotesViewModel = notesViewModel
fun getNoteDetailsViewModel(): NoteDetailsViewModel = noteDetailsViewModel

}

this is my DIHelper in the iosMain to access the viewModels in the ios app

AbdelrahmanEsam avatar Apr 09 '24 11:04 AbdelrahmanEsam

@AbdelrahmanEsam I see, I'm using Compose Multiplatform, so for me the setup above does the job on both platforms. But if you are having the setup with separate UIs for iOS and Android, then I think you can take a look at joreilly FantasyPremierLeague, he's using VMs with Koin. I haven't checked the whole thing, but this PR might be helpful.

Leedwon avatar Apr 09 '24 11:04 Leedwon

@Leedwon unfortunately his viewModels doesn't have any other injected dependencies (usecases /repositories) so he just take object from it at the view in ios side

AbdelrahmanEsam avatar Apr 09 '24 14:04 AbdelrahmanEsam

The ViewModel in the common Main is more for the Compose Multiplatform than SwiftUI.

Nothing will change for iOS UIKit/SwiftUI.

The best example is https://github.com/joreilly/FantasyPremierLeague.

Also, the usage of Koin viewmodel in the commonMain must be also for Koin annotation.

frankois1234 avatar Apr 09 '24 14:04 frankois1234

@frankois1234 that really doesn't work .... I am using the helper in the ios main module and for the android side everything working perfectly .... but when I am trying to run the ios app koin can't inject the viewModel with the following error

org.koin.core.error.InstanceCreationException: Could not create instance for '[Factory:

AbdelrahmanEsam avatar Apr 09 '24 14:04 AbdelrahmanEsam

This issue is for demanding the implementation of koin ViewModel support in kmp commonMain project, not how to use it.

As the import org.koin.androidx.viewmodel.dsl.viewModel is not available in commonMain project but the import of androidx.lifecycle.ViewModel is now possible with the latest androidx.lifecycle:lifecycle-viewmodel dependancies

frankois1234 avatar Apr 10 '24 09:04 frankois1234

This issue is for demanding the implementation of koin ViewModel support in kmp commonMain project, not how to use it.

As the import org.koin.androidx.viewmodel.dsl.viewModel is not available in commonMain project but the import of androidx.lifecycle.ViewModel is now possible with the latest androidx.lifecycle:lifecycle-viewmodel dependancies

exactly

Vivecstel avatar Apr 10 '24 09:04 Vivecstel

@frankois1234 I didn't said that I am using it with koin viewModel ..... actually I am creating it with factory or single in koin .... but koin failed to give me my instance

AbdelrahmanEsam avatar Apr 10 '24 17:04 AbdelrahmanEsam

@frankois1234 this happen in the ios side only for some reason I really don't know .... in the android side everything working perfectly

AbdelrahmanEsam avatar Apr 10 '24 17:04 AbdelrahmanEsam

@AbdelrahmanEsam Please make another issue, it's not the target of the current one

frankois1234 avatar Apr 11 '24 08:04 frankois1234

https://github.com/hoc081098/kmp-viewmodel/blob/master/viewmodel-koin-compose/src/commonMain/kotlin/com/hoc081098/kmp/viewmodel/koin/compose/koinKmpViewModel.kt

I did it before AndroidX. Hope it is useful

hoc081098 avatar Apr 14 '24 14:04 hoc081098

@arnaudgiuliani, now that JetBrains has published their artifact (which includes JS and WASM support), we should be able to add official ViewModel KMP support to Koin.

Note that the new artifact is still experimental, so we might want to wait for it to reach a more stable stage, but we can start exploring it and reporting any challenge we may find to JB.

Please see: https://github.com/JetBrains/compose-multiplatform/releases/tag/v1.6.10-beta01

marcellogalhardo avatar Apr 17 '24 19:04 marcellogalhardo

Experimental support for koin viewmodel multiplatform would be helpful!

PMARZV avatar Apr 17 '24 21:04 PMARZV

Great, let's add it for one of next milestone quickly 💪 thanks @marcellogalhardo

arnaudgiuliani avatar Apr 19 '24 09:04 arnaudgiuliani

@arnaudgiuliani koin annotations will have the support?

frankois1234 avatar Apr 19 '24 09:04 frankois1234

it should be possible yes 👍

arnaudgiuliani avatar Apr 19 '24 09:04 arnaudgiuliani

Following this!

ronjunevaldoz avatar May 02 '24 12:05 ronjunevaldoz

Work in progress: https://github.com/InsertKoinIO/koin/pull/1875

arnaudgiuliani avatar May 15 '24 15:05 arnaudgiuliani

Release in 3.6.0-Beta4 🎉

arnaudgiuliani avatar May 17 '24 16:05 arnaudgiuliani

❤️

Sent from Proton Mail Android

-------- Original Message -------- On 5/17/24 12:22 PM, Arnaud Giuliani wrote:

Release in 3.6.0-Beta4 🎉

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

ColtonIdle avatar May 17 '24 16:05 ColtonIdle

Hello @arnaudgiuliani i tried 3.6.0-Beta4

factory {
    ListViewModel(get(), get(named(IODispatcher)))
}

I was creating my viewmodel like this previously. Now if i use viewModelOf(::ListViewModel) how can i specify the dispatcher parameter to use named(IODispatcher)?

agabeyalioglu avatar May 19 '24 11:05 agabeyalioglu

Hi, can I get and use viewModelOf in commonMain if we use SwiftUI on iOS side? What import/imports is needed for this? I have 3.6.0-Beta4 version.

DavidGrygar avatar May 29 '24 15:05 DavidGrygar

Hello @arnaudgiuliani i tried 3.6.0-Beta4

factory {
    ListViewModel(get(), get(named(IODispatcher)))
}

I was creating my viewmodel like this previously. Now if i use viewModelOf(::ListViewModel) how can i specify the dispatcher parameter to use named(IODispatcher)?

you need manual dsl then to describe your qualifier and use named.

arnaudgiuliani avatar May 31 '24 16:05 arnaudgiuliani

Hi, can I get and use viewModelOf in commonMain if we use SwiftUI on iOS side? What import/imports is needed for this? I have 3.6.0-Beta4 version.

For compose MP for now. Still need a bit of work for Swift integration

arnaudgiuliani avatar May 31 '24 16:05 arnaudgiuliani

Hi, can I get and use viewModelOf in commonMain if we use SwiftUI on iOS side? What import/imports is needed for this? I have 3.6.0-Beta4 version.

@DavidGrygar It's not that simple, take a look at https://github.com/joreilly/FantasyPremierLeague and https://github.com/joreilly/FantasyPremierLeague/issues/231.

You can use a Android ViewModel almost like a SwiftUI ViewModel but you need to manage the lifecycle yourself.

frankois944 avatar May 31 '24 17:05 frankois944