Kotlin + Dagger-为ViewModel工厂注入地图 [英] Kotlin + Dagger - inject Map for ViewModel factory
问题描述
我正在将新的Architecture Components与Dagger2一起使用,我想使用Factory类注入ViewModel. Factory类本身是可注入的.当用Java定义Factory类时,这一切都很好,但是当我将其转换为Kotlin时,Dagger2不知道如何为构造函数生成Map,而在Java中则知道如何这样做.我认为区别在于,转换后,Factory类使用的是kotlin包中的Map,而不是java.util.Map包中的Map.如何获取Dagger2为Factory构造函数生成地图?
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?
这是工厂课
@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)
}
}
}
这是错误
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()
}
推荐答案
我对您的ViewModelFactory
代码做了一些修改:
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)
}
}
}
您可以尝试这个吗?
我添加了@JvmSuppressWildcards
批注.
Can you try with this?
I added @JvmSuppressWildcards
annotation.
有关更多信息,您可以检查: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-suppress-wildcards/index.html
For more information you can check: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-suppress-wildcards/index.html
您可以从我的仓库中找到一个实时演示: https://github.com/savepopulation/dc-tracker
You can find a live demo from my repo: https://github.com/savepopulation/dc-tracker
这篇关于Kotlin + Dagger-为ViewModel工厂注入地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!