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

ViewModel KMP support #130

Closed
frankois944 opened this issue May 20, 2024 · 8 comments
Closed

ViewModel KMP support #130

frankois944 opened this issue May 20, 2024 · 8 comments
Labels
question Further information is requested status:checking Ticket is currently being checked
Milestone

Comments

@frankois944
Copy link

frankois944 commented May 20, 2024

Hello,

As the InsertKoinIO/koin#1826 is now implemented.

I'm using koin annotation in a KMP project with viewmodel.

I'm using the annotation @KoinViewModel in the Shared project.

The issue is on iOS target :

The generate code is

import org.koin.androidx.viewmodel.dsl.viewModel 
internal val mypackage_SharedModule : Module = module {
	viewModel() { mypackage.MainScreenViewModel() } bind(org.koin.core.component.KoinComponent::class)
}

the viewmodel is missing as org.koin.androidx.viewmodel.dsl.viewModel is not founded.

For the iOS target, i'm using the dependancies : "koin-core" and "koin-compose".

I'm missing something ?

Thanks

@frankois944 frankois944 changed the title Viewmodel model support Viewmodel KMP support May 20, 2024
@frankois944 frankois944 changed the title Viewmodel KMP support ViewModel KMP support May 20, 2024
@frankois944
Copy link
Author

There is a workaround for now,
it's manually declaring the viewmodel on the Android app source instead using annotation.

Like:

@Module
internal class AndroidModule {
    @KoinViewModel
    fun mainScreenViewModel(parameters: String? = null): MainScreenViewModel {
        return MainScreenViewModel(param1 = parameters)
    }
}

@frankois944
Copy link
Author

@arnaudgiuliani some news about Koin annotation and KMP ViewModel?

@arnaudgiuliani
Copy link
Member

KMP ViewModel for Compose has landed in Koin 3.6. We should be able to generate ViewModel for compose at least.

@arnaudgiuliani arnaudgiuliani added this to the 1.4.0 milestone Jun 4, 2024
@frankois944
Copy link
Author

frankois944 commented Jun 4, 2024

KMP ViewModel for Compose has landed in Koin 3.6. We should be able to generate ViewModel for compose at least.

On the commonMain, the generated code is working if I'm targetting Android (and has the correct dependancy in the androidMain) but not for iOS, it doesn't have the deps.

@arnaudgiuliani
Copy link
Member

but not for iOS, it doesn't have the deps.

Compose MP ViewModel is available on common sources

@arnaudgiuliani arnaudgiuliani added question Further information is requested status:checking Ticket is currently being checked and removed type:improvement labels Jun 6, 2024
@frankois944
Copy link
Author

A PR about the issue : #133

@arnaudgiuliani
Copy link
Member

let's follow up on PR

@heesung6701
Copy link

@frankois944 with our case, we fixed this issue with below method temporary.

made below file on commonMain for making generated file reference it.

package org.koin.androidx.viewmodel.dsl

import androidx.lifecycle.ViewModel
import org.koin.core.definition.Definition
import org.koin.core.definition.KoinDefinition
import org.koin.core.module.Module
import org.koin.core.qualifier.Qualifier

public inline fun <reified T : ViewModel> Module.viewModel(
    qualifier: Qualifier? = null,
    noinline definition: Definition<T>
): KoinDefinition<T> {
    return factory(qualifier, definition)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested status:checking Ticket is currently being checked
Projects
None yet
Development

No branches or pull requests

3 participants