Невозможно внедрить класс с использованием SubComponent в Dagger 2 - PullRequest
0 голосов
/ 28 октября 2019

Я пытаюсь добавить Dagger 2 в один проект. Я создал 3 компонента

AppComponent (the primary one)
RetrofitComponent (Dependent component works fine)
ControllerComponent (Subcomponent, not injecting properly)

это мой AppComponent

@ApplicationScope
@Component(modules = [ApplicationModule::class, PrefModule::class])
interface AppComponent {

fun inject(instance: AppInstance)

fun getAppPref(): AppPreference

fun newControllerComponent(controllerModule: ControllerModule): ControllerComponent
}

это мой ControllerComponent

@UIScope
@Subcomponent(modules = [ControllerModule::class])
interface ControllerComponent {

fun injectActivity(activity: BaseActivity)

fun injectDialog(dialog: BaseDialog)
}

ControllerModule

@Module
class ControllerModule(activity: FragmentActivity) {
var mActivity: FragmentActivity

init {
    mActivity = activity
}

@Provides
fun getContext(): Context {
    return mActivity
}

@Provides
fun getActivity(): Activity {
    return mActivity
}

@Provides
fun getFragmentManager(): FragmentManager {
    return mActivity.supportFragmentManager
}

@Provides
fun getDialogManager(fragmentManager: FragmentManager): DialogsManager 
{
    return DialogsManager(fragmentManager)
}

@Provides
fun getDialogsFactory(): DialogsFactory {
    return DialogsFactory()
}

}

Инъекция активности работает нормально, но когда я пытаюсь ввести Диалог, она никогда не впрыскивает

 controller.injectDialog(singleDialog)

Весь мой код здесь Ссылка Github . Нужна помощь. Что я не делаю или делаю неправильно, что не могу ввести из подкомпонента.

Ответы [ 2 ]

1 голос
/ 31 октября 2019

Чтобы использовать внедрение поля для данного класса, вам нужно указать конкретный тип, а не его суперкласс.

@UIScope
@Subcomponent(modules = [ControllerModule::class])
interface ControllerComponent {

    //fun injectActivity(activity: BaseActivity)

    //fun injectDialog(dialog: BaseDialog)

    fun inject(activity: SomeSpecificActivity)

    fun inject(activity: SomeOtherActivity)

    fun inject(dialog: SomeSpecificDialog)

    fun inject(dialog: SomeOtherDialog)
}
0 голосов
/ 29 октября 2019

Инъекция работает, но вы ничего не внедряете внутри класса SingleClickDialog, помните, что метод inject (class: Class) предназначен для того, чтобы сообщить кинжалу, что класс будет внедрен, но вам нужно поместить нужные элементывнутри этого класса с аннотацией @Inject и теми элементами, которые вам также необходимо добавить в провайдера ControllerModule

...