Я не могу предоставить модель просмотра с кинжалом - PullRequest
0 голосов
/ 21 июня 2020

Dagger выдает следующую ошибку:

[Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.

Dagger version:

  def dagger_version = "2.28"
  implementation "com.google.dagger:dagger:$dagger_version"
  kapt "com.google.dagger:dagger-compiler:$dagger_version"

kotlin version: kotlin_version = '1.3.50'

gradle version classpath 'com.android.tools.build:gradle:3.5.0'

Я уже пробовал вставить kapt { correctErrorTypes = true} в (app)build.gradle файл.

Мой код:

@Singleton
@Component(modules = [DataModule::class, ViewModelBuilderModule::class, SubcomponentsModule::class])
interface ApplicationComponent {
    @Component.Factory
    interface Factory {
        fun create(@BindsInstance applicationContext: Context): ApplicationComponent
    }

    fun mainComponent(): MainComponent.Factory
}

@Module(subcomponents = [MainComponent::class])
object SubcomponentsModule

и

@Module
interface MainModule {

    @Binds
    @IntoMap
    @ViewModelKey(SearchViewModel::class)
    abstract fun bindSearchViewModel(searchViewModel: SearchViewModel): ViewModel
}

и

class TodoViewModelFactory @Inject constructor(
    private val creators:  @JvmSuppressWildcards 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 {
            @Suppress("UNCHECKED_CAST")
            return creator.get() as T
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
    }
}

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

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


Журнал:

error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
public abstract interface ApplicationComponent {
                ^
      java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
          com.movies.allmovies.di.TodoViewModelFactory(creators)
      com.movies.allmovies.di.TodoViewModelFactory is injected at
          com.movies.allmovies.di.ViewModelBuilderModule.bindViewModelFactory(factory)
      androidx.lifecycle.ViewModelProvider.Factory is injected at
          com.movies.allmovies.ui.search.SearchFragment.viewModelFactory
      com.movies.allmovies.ui.search.SearchFragment is injected at
          com.movies.allmovies.di_subcomponent.MainComponent.inject(com.movies.allmovies.ui.search.SearchFragment) [com.movies.allmovies.di.ApplicationComponent ? com.movies.allmovies.di_subcomponent.MainComponent]

1 Ответ

0 голосов
/ 25 июня 2020

Как указано IR42 в комментарии, добавление MainModule::class к ApplicationComponent modules решило проблему.

...