Dagger 2为ViewModel提供组件依赖性

时间:2018-08-09 11:14:49

标签: android dagger-2 android-viewmodel dagger

我正在使用带有多重绑定的Dagger 2提供ViewModel。这是我的代码:

interface AppProvider : MainToolsProvider

interface MainToolsProvider {
    fun provideAppContext(): Context
    fun provideSchedulers(): SchedulersProvider
    fun providerResourceManager(): ResourceManager
    fun provideViewModelFactory(): ViewModelProvider.Factory
    fun provideApi(): Api
}

@Singleton
@Component(modules = [ToolsModule::class, ViewModelModule::class])
interface MainToolsComponent : MainToolsProvider {

    @Component.Builder
    interface Builder {
        fun build(): MainToolsComponent

        @BindsInstance
        fun appContext(appContext: Context): Builder
    }

    class Initializer private constructor() {
        companion object {

            fun init(appContext: Context): MainToolsProvider =
                    DaggerMainToolsComponent.builder()
                            .appContext(appContext)
                            .build()
        }
    }
}

@Module
class ToolsModule {

    @Module
    companion object {
        @JvmStatic
        @Provides
        @Singleton
        fun provideSchedulers(): SchedulersProvider = AppSchedulers()

        @JvmStatic
        @Provides
        @Singleton
        fun provideResourceManager(appContext: Context) = ResourceManager(appContext)

        @JvmStatic
        @Provides
        @Singleton
        fun provideGson() = Gson()

        @JvmStatic
        @Provides
        @Singleton
        fun provideConverterFactory(gson: Gson): Converter.Factory = GsonConverterFactory.create(gson)

        @JvmStatic
        @Provides
        @Singleton
        fun provideAdapterFactory(): CallAdapter.Factory = RxJava2CallAdapterFactory.create()

        @JvmStatic
        @Provides
        @Singleton
        fun provideHttpClient() = OkHttpClient.Builder().build()

        @JvmStatic
        @Provides
        @Singleton
        fun provideRetrofit(httpClient: OkHttpClient, adapterFactory: CallAdapter.Factory, converterFactory: Converter.Factory) =
                Retrofit.Builder()
                        .baseUrl("https://api.clubinclub.com")
                        .addCallAdapterFactory(adapterFactory)
                        .addConverterFactory(converterFactory)
                        .client(httpClient)
                        .build()

        @JvmStatic
        @Provides
        @Singleton
        fun provideApi(retrofit: Retrofit) = retrofit.create(Api::class.java)

    }
}

@Module
abstract class ViewModelModule {
    @Binds
    abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}

@Component(
    dependencies = [MainToolsProvider::class]
)
@Singleton
interface AppComponent : AppProvider {

    fun inject(app: ClubInClubApp)

    class Initializer private constructor() {

        companion object {
            fun init(app: ClubInClubApp): AppComponent {
                val mainToolsProvider = MainToolsComponent.Initializer.init(app)
                return DaggerAppComponent.builder()
                        .mainToolsProvider(mainToolsProvider)
                        .build()
            }
        }
    }
}    

@ActivityScope
@Component(dependencies = [AppProvider::class], modules = [AuthModule::class])
interface AuthComponent {

    fun inject(activity: AuthActivity)
    fun inject(activity: InstagramAuthActivity)
    fun viewModelFactory(): ViewModelProvider.Factory

    class Initializer private constructor() {
        companion object {
            fun init(appProvider: AppProvider) =
                    DaggerAuthComponent.builder()
                            .appProvider(appProvider)
                            .build()
        }
    }
}

@Module
class AuthModule {

    @Provides
    @ActivityScope
    fun provideAuthRepository(api: Api): AuthRepository = AuthRepositoryImpl(api)

    @Provides
    @ActivityScope
    fun provideAuthInteractor(repository: AuthRepository, schedulers: SchedulersProvider): AuthInteractor = AuthInteractorImpl(repository, schedulers)
}

@FragmentScope
@Component(dependencies = [AuthComponent::class], modules = [SignUpSocialViewModelModule::class])
interface SignUpSocialComponent {

    fun inject(fragment: SignUpSocialFragment)

    class Initializer private constructor() {
        companion object {
            fun init(authComponent: AuthComponent) =
                    DaggerSignUpSocialComponent.builder()
                            .authComponent(authComponent)
                            .build()
        }
    }
}

@Module
abstract class SignUpSocialViewModelModule {

    @Binds
    @IntoMap
    @ViewModelKey(SignUpSocialViewModel::class)
    abstract fun signUpSocialViewModel(viewModel: SignUpSocialViewModel): ViewModel

}    

@Singleton
class ViewModelFactory @Inject constructor(
        val viewModels: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>
) : ViewModelProvider.Factory {

    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        val provider = viewModels[modelClass]
        if (provider != null) {
            return provider.get() as T
        }
        throw IllegalArgumentException()
    }
}

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

如您所见,我在ViewModelProvider.Factory中提供了ViewModelModule,它是使用组件依赖关系包含在MainToolsComponentAppComponent中的。另外,我的AuthComponentActivity依赖项,AppProviderSignUpSocialComponentAuthComponent依赖项。还有我的片段:

class SignUpSocialFragment : BaseBindingFragment<FragmentSignUpSocialBinding>() {

    override val layoutId = R.layout.fragment_sign_up_social

    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory
    private lateinit var viewModel: SignUpSocialViewModel

    private var callback: OnSocialButtonClickCallback? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = ViewModelProviders.of(this, viewModelFactory)[SignUpSocialViewModel::class.java]
    }

    override fun initComponent() {
        (activity as? AuthActivity)?.run {
            SignUpSocialComponent.Initializer.init(component).inject(this@SignUpSocialFragment)
        }
    }   
}

方法initComponent在父片段的onCreate方法中被调用。问题是当我尝试编译并启动应用程序时,出现错误:

error: [Dagger/MissingBinding] [com.social.connect.clubinclub.di.base.MainToolsProvider.provideViewModelFactory()] 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 MainToolsComponent extends com.social.connect.clubinclub.di.base.MainToolsProvider {
            ^
  java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> is injected at
      com.social.connect.clubinclub.presentation.base.ViewModelFactory.<init>(viewModels)
  com.social.connect.clubinclub.presentation.base.ViewModelFactory is injected at
     com.social.connect.clubinclub.di.app.ViewModelModule.bindViewModelFactory(factory)
  android.arch.lifecycle.ViewModelProvider.Factory is provided at
      com.social.connect.clubinclub.di.base.MainToolsProvider.provideViewModelFactory()

所以我不知道我在做什么错。请帮助

2 个答案:

答案 0 :(得分:0)

仔细查看您的错误消息:

无法在您的组件(MainToolsProvider.provideViewModelFactory())中提供Map<.., ViewModel>

  如果没有@Provides注释的方法,则无法提供

java.util.Map,javax.inject.Provider>。


检查您的代码我看不到您声明@Multibinds,所以Dagger会抱怨,因为地图是空的。

您唯一要填写地图的地方是SignUpSocialViewModelModule,然后将其添加到SignUpSocialComponent,但我们在MainToolsProvider或更确切地说MainToolsComponent中缺少地图如上所示。


要解决编译错误,您可以声明@Multibinds,以便至少有一个Dagger知道的空映射,但是在运行时您会收到有关该类缺少提供程序的异常。您可能应该将@Binds @IntoMap ... ViewModel位移到MainToolsComponent中。

答案 1 :(得分:0)

fun provideViewModelFactory(): ViewModelProvider.Factory中删除MainToolsProvider

@Inject lateinit var viewModelFactory: ViewModelProvider.Factory中删除viewModel = ViewModelProviders.of(this, viewModelFactory)[SignUpSocialViewModel::class.java]SignUpSocialFragment,而从@Inject lateinit var lateinit var viewModel: SignUpSocialViewModel