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

rememberViewModel throw NotFoundException when you pass Kotlin function as an args #435

Closed
GSculerlor opened this issue Jun 7, 2023 · 3 comments

Comments

@GSculerlor
Copy link

says we have a ViewModel and a function as the argument.

class SomeViewModel(private val fooArg: (Foo) -> Bar) : ViewModel()
val SomeViewModelModule: DI.Module
    get() = DI.Module(name = "module name") {
        bindFactory { fooArg: (Foo) -> Bar ->
            SomeViewModel(fooArg = fooArg)
        }
    }

when you try to retrieve the ViewModel via Kodein rememberViewModel it will throw NotFoundException due to it expects the args passed as a Function1<*, *>

org.kodein.di.DI$NotFoundException: No binding found for SomeViewModelModule, with argument Function1<*, *>
    at org.kodein.di.internal.DIContainerImpl.factory(DIContainerImpl.kt:208)
    at org.kodein.di.DIContainer$DefaultImpls.factory$default(DIContainer.kt:32)
    at org.kodein.di.internal.DirectDIBaseImpl.Factory(DirectDIImpl.kt:18)
    at org.kodein.di.compose.android.KodeinViewModelScopedFactory.create(KodeinViewModelScopedFactory.kt:18)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:53)
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:35)
...

we previously used our own simple wrapper to retrieve the ViewModel

remember {
    ViewModelLazy(
        viewModelClass = VM::class,
        storeProducer = { viewModelStoreOwner.viewModelStore },
        factoryProducer = {
            object : ViewModelProvider.Factory {
                override fun <T : ViewModel> create(modelClass: Class<T>): T {
                    @Suppress("UNCHECKED_CAST")
                    return direct.instance<A, VM>(tag = tag, arg = arg) as T
                }
            }
        },
    )
}
@GSculerlor
Copy link
Author

GSculerlor commented Jun 22, 2023

....and apparently, it doesn't work well with Pair and Triple either. am I missing something here? Is it expected to retrieve an arg with primitive type only?

@romainbsl
Copy link
Member

romainbsl commented Jun 26, 2023

I found the issue. It was a problem of generics erased while retrieving dependencies.
Version 7.20.2 should fix it.

@GSculerlor
Copy link
Author

thank you, tested on 7.20.2 and it does fix the issue.

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

No branches or pull requests

2 participants