-
-
Notifications
You must be signed in to change notification settings - Fork 720
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
Support androidx viewmodel for kotlin multiplatform #1826
Comments
Can you explain how to add viewmodel library in kmm |
Hello, A full demo how to use viewmodel here : It should be OK that koin move the viewmodel support to KMP. |
The is the official repo used in google codelabe |
@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']' also I am creating this viewModel by factory or single ..... I think koin should support the viewModel{} at the common main |
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:
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
}
}
}
@Composable
internal actual fun <VM : ViewModel> viewModel(
modelClass: KClass<VM>,
factory: ViewModelProvider.Factory
): VM = androidx.lifecycle.viewmodel.compose.viewModel(modelClass.java, factory = factory)
@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 the problem is on the ios side not the android one .... android is working perfectly class DIHelper : KoinComponent {
} this is my DIHelper in the iosMain to access the viewModels in the ios app |
@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 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 |
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 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: |
This issue is for demanding the implementation of koin ViewModel support in kmp commonMain project, not how to use it. As the import |
exactly |
@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 |
@frankois1234 this happen in the ios side only for some reason I really don't know .... in the android side everything working perfectly |
@AbdelrahmanEsam Please make another issue, it's not the target of the current one |
I did it before AndroidX. Hope it is useful |
@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 |
Experimental support for koin viewmodel multiplatform would be helpful! |
Great, let's add it for one of next milestone quickly 💪 |
@arnaudgiuliani koin annotations will have the support? |
it should be possible yes 👍 |
Following this! |
Work in progress: #1875 |
Release in 3.6.0-Beta4 🎉 |
❤️
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](#1826 (comment)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/AHSHXZKO4SERB35MHUHPEJLZCYVFRAVCNFSM6AAAAABFA6YDZ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJXHE2DQNJVGY).
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
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 |
Hi, can I get and use |
you need manual dsl then to describe your qualifier and use |
For compose MP for now. Still need a bit of work for Swift integration |
@DavidGrygar You can use a Android ViewModel almost like a SwiftUI ViewModel but you need to manage the lifecycle yourself. |
@DavidGrygar @ColtonIdle |
Not sure if this is the best place to ask, but related: It seems to be working fine, but I'm afraid of the viewModel comments: Maybe I'm missing something important? Context: |
@romanatexn The most important thing is the respect of the lifecycle of the viewmodel (init()/onCleared()). |
I tried to use the viewModel from 3.6.0-Beta4, and everything seemed to be fine on Android. However, I noticed that whenever I navigate out and back to a specific screen, a new instance of that view model is created. Wasn't it supposed to reuse the viewModel as it was already created previously? Let me know if that's a different issue. Thank you... |
Hi, you need to wrap your KMP viewmodel inside an SwiftUI ObservableObject and use it from a @StateObject like an SwiftUI MVVM class. Take a look at my repo, https://github.com/frankois944/kmp-mvvm-exploration, you will find some inspiration. |
Hey @frankois944, thanks for sharing your repo... I'll look into it. |
Oh, I see. For Android, The |
kmp-viewmodel with SwiftUI without Compose Navigation |
@arnaudgiuliani In hiltViewModel() viewModel is attached with NavBackStatckEntry. When backStack is cleared onCleared method of VM is cleared. How this is going to taken care in koin? |
@farhazulMullick On iOS, Koin can't manage the lifecycle of the KMP ViewModel. Take a look at my playground, it should be inspiring. |
Is my understanding here is correct that ViewModel 2.8.x still not working with Koin? I tried Koin 4.0.0-RC1 and ViewModel 2.8.4 after moving the declaration of viewModelOf (now located in
I am working with View base project migrating to KMM. androidApp
shared
SharedModule.kt SharedModule.android.kt
SharedModule.ios.kt
It is pointing in one of my Fragment that do field injection.
|
Give us the full stack of the error, the real reason is not at the top, the somewhere down there. The Koin binding error reason are not at the top of the stack. |
Yes the error at top is usually not the actual cause, however in my case these are the only logs after crash.
|
Never mind I found the issue from this log |
following checks on KMP. But seems ok |
To follow on RC2 |
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
The text was updated successfully, but these errors were encountered: