Dagger2 ViewModel注入

时间:2018-04-18 08:14:18

标签: android kotlin dagger-2

我有使用viewmodels进行dagger2注入的工作设置,但是我试图稍微重构它并导致以下错误:

Projects/android-app/app/build/tmp/kapt3/stubs/debug/com/domain/app/dagger/AppComponent.java:8: error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
public abstract interface AppComponent extends dagger.android.AndroidInjector<com.domain.app.dagger.DomainApplication> {
                ^
      java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> is injected at
          com.domain.app.dagger.ViewModelFactory.<init>(creators)
      com.domain.app.dagger.ViewModelFactory is injected at
          com.domain.app.dagger.AppModule.bindViewModelFactory(factory)
      android.arch.lifecycle.ViewModelProvider.Factory is injected at
          com.domain.app.screens.access.login.LoginFragment.viewModelFactory
      com.domain.app.screens.access.login.LoginFragment is injected at
          dagger.android.AndroidInjector.inject(arg0)

我不明白匕首是如​​何在内部工作所以我不确定我想要达到的目标是否可能

这是我的设置:

// AppComponent.kt
@Singleton
@Component(modules = [AndroidSupportInjectionModule::class, AppModule::class, ContributorsModule::class])
interface AppComponent : AndroidInjector<CpmApplication> {
    @Component.Builder abstract class Builder : AndroidInjector.Builder<CpmApplication>()
}

// AppModule.kt
@Module
abstract class AppModule {
    @Binds abstract fun bindApplicationContext(app: CpmApplication): Context
}

// ContributorsModule.kt
@Module
abstract class ContributorsModule {
    @Binds abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory

    @ContributesAndroidInjector()
    abstract fun accessActivityInjector(): AccessActivity

    @ContributesAndroidInjector
    abstract fun accessLoginFragmentInjector(): LoginFragment

    @ContributesAndroidInjector
    abstract fun accessRegisterFragmentInjector(): RegisterFragment

    @Binds
    @IntoMap
    @ViewModelKey(LoginViewModel::class)
    internal abstract fun bindLoginViewModel(loginViewModel: LoginViewModel): ViewModel
}

但我想重构它就像这样

// ContributorsModule.kt
@Module
abstract class ContributorsModule {
    @Binds abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory

    @ActivityScope
    @ContributesAndroidInjector(modules = [AccessModule::class])
    abstract fun contributeAccessActivity(): AccessActivityl
}

// AccessModule.kt
@Module
abstract class AccessModule {   
    @FragmentScope
    @ContributesAndroidInjector(modules = [LoginModule::class])
    abstract fun contributeLoginFragment(): LoginFragment

    @FragmentScope
    @ContributesAndroidInjector(modules = [RegisterModule::class])
    abstract fun contributeRegisterFragment(): RegisterFragment   
}

// LoginModule.kt
@Module
abstract class LoginModule {
    @Binds
    @IntoMap
    @ViewModelKey(LoginViewModel::class)
    internal abstract fun bindLoginViewModel(loginViewModel: LoginViewModel): ViewModel
}

// RegisterModule.kt
@Module
abstract class LoginModule {
    @Binds
    @IntoMap
    @ViewModelKey(LoginViewModel::class)
    internal abstract fun bindRegisterViewModel(registerViewModel: RegisterViewModel): ViewModel
}

我认为我想在一个地方绑定ViewModelProvider.Factory并在不同的地方进行视图模型的实际映射这一事实,GitHubBrowserSample应用程序有这个文件: https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/di/ViewModelModule.java

与ViewModels相关的所有内容都在单个文件中。所以问题是:我正在做什么甚至可能?如果是这样我做错了什么?如果没有,那么为什么?

0 个答案:

没有答案