Как внедрить DialogFragment с помощью Dagger? - PullRequest
0 голосов
/ 21 ноября 2019

пожалуйста, я использую dagger 2 для DI, как вставить фрагмент диалога в мою деятельность и как использовать класс DaggerDialogFragment, предоставленный dagger

я создаю свой DialogFragmentFactory

 class DialogFragmentFactory @Inject constructor(
private val providers: Map<Class<out Fragment>, @JvmSuppressWildcards 
 Provider<DialogFragment>>
 ) : FragmentFactory() {

override fun instantiate(classLoader: ClassLoader, className: String): 
Fragment {
    // loadFragmentClass is a static method of FragmentFactory
    // and will return the Class of the fragment
    val fragmentClass = loadFragmentClass(classLoader, className)

    // we will then be able to use fragmentClass to get the provider
    // of the Fragment from the providers map
    val provider = providers[fragmentClass]

    if (provider != null) {
        return provider.get()
    }

    // The provider for the fragment could be null
    // if the Fragment class is not binded to the Daggers graph
    // in this case, we will default to the default implementation
    // which will attempt to instantiate the Fragment
    // through the no-arg constructor
    return super.instantiate(classLoader, className)
   }
}

а затем я создаю свой модуль DialogFragment

  @Module
 class DialogFragmentsModule {

  @Singleton
  @Provides
  fun myDialogFragment() = MainAlertDialogFragment()

 }

я создаю эту аннотацию

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

я внедряю AndroidInjectionModule и AndroidSupportInjectionModule, ApplicationComponent, DialogFragmentsModule

это мой фрагмент диалогазаголовок

 class MainAlertDialogFragment @Inject constructor() : 
 DaggerDialogFragment(), HasAndroidInjector {

и я хочу вставить это так

      @Inject
lateinit var dialog: MainAlertDialogFragment
...