Kotlin + Dagger - inject Map for ViewModel factory

2019-01-17 13:09发布

问题:

I'm using the new Architecture Components with Dagger2 and I would like to inject my ViewModels using a Factory class. The Factory class is itself injectable. This all works well when the Factory class is defined in Java, but when I convert it to Kotlin, Dagger2 does not know how to generate the Map for the constructor, while in Java it knows how to do so. I presume the difference is that, after conversion, the Factory class uses the Map from the kotlin package, as opposed to from java.util.Map package. How can I get Dagger2 to generate the map for the Factory constructor?

Here's the Factory class

@ActivityScope
class MainActivityViewModelFactory @Inject
constructor(private val creators: Map<Class<out ViewModel>, Provider<ViewModel>>) : ViewModelProvider.Factory {

    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        var creator: Provider<out ViewModel>? = creators[modelClass]
        if (creator == null) {
            for ((key, value) in creators) {
                if (modelClass.isAssignableFrom(key)) {
                    creator = value
                    break
                }
            }
        }
        if (creator == null) {
            throw IllegalArgumentException("unknown model class " + modelClass)
        }
        try {
            return creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }

    }
}

and this is the error

Error:java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,? extends javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.

I tried creating a module to provide the map, but that didn't help.

@ActivityScope
@Module
class MapModule {
    @Provides
    fun provideMap(): Map<Class<out ViewModel>, Provider<ViewModel>> = mutableMapOf()
}

回答1:

I modified your ViewModelFactory code a bit:

@ActivityScope
class MainActivityViewModelFactory @Inject
constructor(private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>) : ViewModelProvider.Factory {

    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        var creator: Provider<out ViewModel>? = creators[modelClass]
        if (creator == null) {
            for ((key, value) in creators) {
                if (modelClass.isAssignableFrom(key)) {
                    creator = value
                    break
                }
            }
        }
        if (creator == null) {
            throw IllegalArgumentException("unknown model class " + modelClass)
        }
        try {
            return creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }

    }
}

Can you try with this? I added @JvmSuppressWildcards annotation.

For more information you can check: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-suppress-wildcards/index.html

Edit: You can find a live demo from my repo: https://github.com/savepopulation/dc-tracker