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

migrating koin version from 2.1.6 to 3.1.5 broke our generic ViewModel delegation structure #1323

Closed
aytajde opened this issue Mar 29, 2022 · 7 comments

Comments

@aytajde
Copy link

aytajde commented Mar 29, 2022

Describe the bug
We've been using generic type ViewModel in our project and initialising it like below in a BaseFragment

protected abstract val vmClazz: KClass<VM> val viewModel: VM by lazy { getViewModel(vmClazz) { parametersOf(arguments) } }

Note: getViewModel method is from the package org.koin.androidx.viewmodel.ext.android

But, after migrating the koin version to the latest (3.1.5) it doesn't compile anymore and showing the below error:

Cannot use 'VM' as reified type parameter. Use a class instead.
Property delegate must have a 'getValue(BaseFragment<X, X, VM, X>, KProperty<*>)' method. None of the following functions is suitable: public inline operator fun <T> Lazy<ViewModel>.getValue(thisRef: Any?, property: KProperty<*>): ViewModel defined in kotlin

Also, in a singleton definition when we're trying to use the getProperty() from the package org.koin.core.scope
file, there is another error in a module file, which is saying,
Not enough information to infer type variable T

As I understood, we can't use the generic type ViewModel using the KClass features as it can't infer its type as a generic.

In conclusion, we cannot upgrade our koin version because of the feature misses in a new version.

Expected behavior
There are neither a single update info nor the change logs in a documentations. I cannot find any alternatives to accomplish my requirements.
Please, provide these missing features from the previous versions or provide an alternative way to achieve above requirements as we don't want to define ViewModel types explicitly.
Koin project used and used version (please complete the following information):
We are using implementation "io.insert-koin:koin-android:2.1.6" implementation "io.insert-koin:koin-androidx-viewmodel:2.1.6"

We want upgrade it to the     implementation   ##'io.insert-koin:koin-android:3.1.5'
@Vandalko
Copy link

Vandalko commented Apr 21, 2022

The only workaround (as of version 3.1.6) is to use java-compat module.
Honestly, there's no good reason why there's no direct Kotlin equivalent to pass ViewModel class.

@arnaudgiuliani
Copy link
Member

The ViewModel API is complex to maintain. I'm more thinking to have default APIs and have a kindof DSL to help specify the way you want to use it. We have so much combination of arguments, that it become really hard

@arnaudgiuliani arnaudgiuliani added this to the 3.2.1 milestone May 18, 2022
@Zikstar
Copy link

Zikstar commented Jun 14, 2022

The only workaround (as of version 3.1.6) is to use java-compat module. Honestly, there's no good reason why there's no direct Kotlin equivalent to pass ViewModel class.

Hi, I have this same issue. How do I use the java-compat module. So I just import this dependencyand it should work as before?
"implementation "io.insert-koin:koin-android-compat:$koinVersion".
Thats the dependency I found for Java compatiblity in the docs

@Nemo996
Copy link

Nemo996 commented Jun 18, 2022

Maybe I misunderstood the question, but I solved a similar problem in the following way
abstract class BaseActivity<VM : BaseViewModel, VB : ViewBinding> : AppCompatActivity(),
AndroidScopeComponent {
final override val scope : Scope by activityScope()
protected val viewModel: VM by viewModel(scope,this,getViewModelClass())
Hope this helps you

@arnaudgiuliani arnaudgiuliani modified the milestones: 3.2.1, 3.3.0 Jun 27, 2022
@rpavliuk
Copy link

rpavliuk commented Jul 1, 2022

@Nemo996 Hi, I've tried your solution, but with custom scope and it's not working.
Because 1st parameter in by viewModel is Qualifier not a scope.
And I don't understand where u get the getViewModelClass method.
Can you provide more info here.

Thank you

@arnaudgiuliani
Copy link
Member

still need some clearer API 👍 Next patch will help

@arnaudgiuliani
Copy link
Member

Generic access to ViewModel API is available in koin-android 3.3.0 👍
#1459

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

No branches or pull requests

6 participants