-
Notifications
You must be signed in to change notification settings - Fork 174
Dagger2: component dependencies #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,26 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| import android.content.Context | ||
| import ru.otus.daggerhomework.di.ApplicationComponent | ||
| import ru.otus.daggerhomework.di.DaggerApplicationComponent | ||
|
|
||
| class App :Application() { | ||
| /** | ||
| * `Custom application` приложения | ||
| */ | ||
| class App : Application() { | ||
|
|
||
| private lateinit var applicationComponent: ApplicationComponent | ||
|
|
||
| override fun onCreate() { | ||
| super.onCreate() | ||
| applicationComponent = DaggerApplicationComponent.factory().create(this) | ||
| } | ||
|
|
||
| companion object { | ||
|
|
||
| /** Получить [applicationComponent] на основе [context] */ | ||
| fun appComponent(context: Context) = | ||
| (context.applicationContext as App).applicationComponent | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import ru.otus.daggerhomework.di.utils.qualifiers.ApplicationContext | ||
|
|
||
| /** | ||
| * `Dagger` [Component] приложения | ||
| */ | ||
| @Component | ||
| interface ApplicationComponent { | ||
|
|
||
| /** `Application` контекст */ | ||
| @ApplicationContext | ||
| fun context(): Context | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun create(@BindsInstance @ApplicationContext context: Context): ApplicationComponent | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package ru.otus.daggerhomework.di.main | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import kotlinx.coroutines.channels.Channel | ||
| import ru.otus.daggerhomework.App | ||
| import ru.otus.daggerhomework.di.ApplicationComponent | ||
| import ru.otus.daggerhomework.di.utils.qualifiers.ActivityContext | ||
| import ru.otus.daggerhomework.di.utils.qualifiers.ApplicationContext | ||
| import ru.otus.daggerhomework.di.utils.scopes.ActivityScope | ||
| import ru.otus.daggerhomework.presentation.Activity | ||
| import ru.otus.daggerhomework.presentation.color.ColorGenerator | ||
|
|
||
| /** | ||
| * `Dagger` [Component] главного экрана | ||
| */ | ||
| @ActivityScope | ||
| @Component( | ||
| modules = [MainActivityModule::class], | ||
| dependencies = [ApplicationComponent::class] | ||
| ) | ||
| interface MainActivityComponent { | ||
|
|
||
| /** `Application` контекст */ | ||
| @get:ApplicationContext | ||
| val appContext: Context | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему проперти а не метод?) |
||
|
|
||
| /** `Activity` контекст */ | ||
| @get:ActivityContext | ||
| val activityContext: Context | ||
|
|
||
| /** Генератор цветов */ | ||
| val colorGenerator: ColorGenerator | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А этот класс нужен в MainActivityComponent? |
||
|
|
||
| /** Канал числовых значений в виде [Int] */ | ||
| val channel: Channel<Int> | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun create( | ||
| component: ApplicationComponent, | ||
| @BindsInstance @ActivityContext context: Context | ||
| ): MainActivityComponent | ||
| } | ||
|
|
||
| companion object { | ||
|
|
||
| /** Создать `Dagger` [Component] главного экрана на основе соответствующей [Activity] */ | ||
| fun create(activity: Activity): MainActivityComponent = | ||
| DaggerMainActivityComponent.factory() | ||
| .create( | ||
| component = App.appComponent(activity.applicationContext), | ||
| context = activity | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.otus.daggerhomework.di.main | ||
|
|
||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import kotlinx.coroutines.channels.Channel | ||
| import ru.otus.daggerhomework.di.utils.scopes.ActivityScope | ||
| import ru.otus.daggerhomework.presentation.color.ColorGenerator | ||
| import ru.otus.daggerhomework.presentation.color.ColorGeneratorImpl | ||
|
|
||
| /** | ||
| * `Dagger` [Module] главного экрана | ||
| */ | ||
| @Module | ||
| interface MainActivityModule { | ||
|
|
||
| @Binds | ||
| fun bindColorGenerator(colorGenerator: ColorGeneratorImpl): ColorGenerator | ||
|
|
||
| companion object { | ||
|
|
||
| @ActivityScope | ||
| @Provides | ||
| fun provideChannel(): Channel<Int> = Channel(Channel.CONFLATED) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.otus.daggerhomework.di.producer | ||
|
|
||
| import dagger.Component | ||
| import ru.otus.daggerhomework.di.main.MainActivityComponent | ||
| import ru.otus.daggerhomework.di.utils.scopes.FragmentScope | ||
| import ru.otus.daggerhomework.presentation.producer.FragmentProducer | ||
|
|
||
| /** | ||
| * `Dagger` [Component] `Fragment`-а поставления цветов | ||
| */ | ||
| @FragmentScope | ||
| @Component( | ||
| modules = [FragmentProducerModule::class], | ||
| dependencies = [MainActivityComponent::class] | ||
| ) | ||
| interface FragmentProducerComponent { | ||
|
|
||
| /** Внедрить зависимости [FragmentProducer]-а */ | ||
| fun inject(producer: FragmentProducer) | ||
|
|
||
| companion object { | ||
|
|
||
| /** Создать `Dagger` [Component] `Fragment`-а поставления цветов на основе [MainActivityComponent] */ | ||
| fun create(mainActivityComponent: MainActivityComponent): FragmentProducerComponent = | ||
| DaggerFragmentProducerComponent.builder() | ||
| .mainActivityComponent(mainActivityComponent) | ||
| .build() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ru.otus.daggerhomework.di.producer | ||
|
|
||
| import dagger.Binds | ||
| import dagger.Module | ||
| import kotlinx.coroutines.channels.Channel | ||
| import kotlinx.coroutines.channels.SendChannel | ||
|
|
||
| /** | ||
| * `Dagger` [Module] `Fragment`-а поставления цветов | ||
| */ | ||
| @Module | ||
| interface FragmentProducerModule { | ||
|
|
||
| @Binds | ||
| fun bindSendChannel(channel: Channel<Int>): SendChannel<Int> | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не понял зачем тут в параметрах контекст, у тебя же во монолит, доступ к App есть везде, просто можно геттер на ApplicationComponent написать