我无法提供ViewModel

时间:2019-09-23 23:43:26

标签: java android kotlin dagger-2

当我在@ContributesAndroidInjector(modules = [MainFragmentBuilderModule::class, LoginViewModelProviderModule::class])中包含LoginViewModelProviderModule时,我无法使用LoginViewModelProviderModule提供LoginViewModel,但是当我将其包含在ActivityBuilderModule类的@Module批注中时,它将起作用。

BaseApplicationComponent

@Suppress("unused")
@Singleton
@Component(
    modules = [AndroidInjectionModule::class,
        ActivityBuilderModule::class,
        ViewModelFactoryModule::class]
)
interface BaseApplicationComponent : AndroidInjector<BaseApplicationClass> {

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder

        fun build(): BaseApplicationComponent
    }
}

ActivityBuilderModule

@Module
abstract class ActivityBuilderModule {

    @LoginScope
    @ContributesAndroidInjector(modules = [MainFragmentBuilderModule::class, LoginViewModelProviderModule::class])
    abstract fun contributeMainActivity(): MainActivity
}

MainFragmentBuilderModule

@Module
abstract class MainFragmentBuilderModule {

    @ContributesAndroidInjector
    abstract fun contributesLoginFragment(): LoginFragment
}

LoginViewModelProviderModule

@Suppress("unused")
@Module
abstract class LoginViewModelProviderModule {
    @Binds
    @IntoMap
    @ViewModelKey(LoginViewModel::class)
    abstract fun bindLoginViewModel(viewModel: LoginViewModel): ViewModel
}

ViewModelFactoryModule

@Module
abstract class ViewModelFactoryModule {

    @Binds
    abstract fun bindViewModelFactory(viewModelProviderFactory: ViewModelProviderFactory): ViewModelProvider.Factory
}

ViewModelProviderFactory

@Singleton
class ViewModelProviderFactory @Inject constructor(
    private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        val creator = creators[modelClass] ?: creators.entries.firstOrNull {
            modelClass.isAssignableFrom(it.key)
        }?.value ?: throw IllegalArgumentException("unknown model class $modelClass") as Throwable
        try {
            @Suppress("UNCHECKED_CAST")
            return creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }
}

LoginScope

@Scope
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
annotation class LoginScope 

ViewModelKey

@MustBeDocumented
@Target(
    AnnotationTarget.FUNCTION,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.PROPERTY_SETTER
)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)

这是我看到的错误:

错误:[Dagger / MissingBinding]如果没有@Provides注释的方法,则无法提供java.util.Map,javax.inject.Provider>。 公共抽象接口BaseApplicationComponent扩展了dagger.android.AndroidInjector {                 ^       java.util.Map,javax.inject.Provider>在以下位置注入           com.example.mydemoapplication.viewmodel_factory.ViewModelProviderFactory(创建者)       com.example.mydemoapplication.viewmodel_factory.ViewModelProviderFactory在以下位置注入           com.example.mydemoapplication.LoginFragment.viewModelProviderFactory       com.example.mydemoapplication.LoginFragment注入在           dagger.android.AndroidInjector.inject(T)[com.example.mydemoapplication.dagger.BaseApplicationComponent吗? com.example.mydemoapplication.dagger.ActivityBuilderModule_ContributeMainActivity.MainActivitySubcomponent吗? com.example.mydemoapplication.dagger.MainFragmentBuilderModule_ContributesLoginFragment.LoginFragmentSubcomponent]

1 个答案:

答案 0 :(得分:0)

您只能在应用程序中创建一个绑定所有viewmodel的模块。您可以在示例应用程序中查看详细信息: sample-moviedb-app

BaseApplicationComponent:

@Suppress("unused")
@Singleton
@Component(
    modules = [AndroidInjectionModule::class,
        ActivityBuilderModule::class,
        ViewModelFactoryModule::class,
        ViewModelModule::class]

)
interface BaseApplicationComponent : AndroidInjector<BaseApplicationClass> {
    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder

        fun build(): BaseApplicationComponent
    }
}

ViewModelModule:

@Suppress("unused")
@Module
abstract class ViewModelProviderModule {
    @Binds
    @IntoMap
    @ViewModelKey(LoginViewModel::class)
    abstract fun bindLoginViewModel(viewModel: LoginViewModel): ViewModel

//other viewmodels in application
   @Binds
   abstract fun bindViewModelFactory(viewModelProviderFactory:ViewModelProviderFactory): ViewModelProvider.Factory
}

MainModule:

@Module
class MainModule {
    @MainActivityScope
    @Provides
    fun provideViewModel(mainActivity: MainActivity, viewModelFactory: ViewModelProviderFactory) =
        ViewModelProviders.of(mainActivity, viewModelFactory).get(LoginViewModel::class.java)
}

LoginViewModel:

class LoginViewModel @Inject constructor(
) : ViewModel() {

}