-
Notifications
You must be signed in to change notification settings - Fork 174
HW-dagger #166
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
Open
rstn
wants to merge
2
commits into
Otus-Android:master
Choose a base branch
from
rstn:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
HW-dagger #166
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| import ru.otus.daggerhomework.di.ApplicationComponent | ||
| import ru.otus.daggerhomework.di.DaggerApplicationComponent | ||
|
|
||
| class App :Application() | ||
| class App : Application() { | ||
|
|
||
| val component: ApplicationComponent = DaggerApplicationComponent.factory().build(this) | ||
| } | ||
|
|
||
| val Application.component: ApplicationComponent get() = (this as App).component |
4 changes: 0 additions & 4 deletions
4
app/src/main/java/ru/otus/daggerhomework/ApplicationComponent.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import androidx.annotation.ColorInt | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.asSharedFlow | ||
|
|
||
| interface ColorObserver { | ||
|
|
||
| val colorFlow: Flow<Int> | ||
|
|
||
| fun fireColorEvent(@ColorInt color: Int) | ||
|
|
||
| } | ||
|
|
||
| class ColorObserverImpl : ColorObserver { | ||
|
|
||
| private val _colorFlow = MutableSharedFlow<Int>( | ||
| extraBufferCapacity = 1, | ||
| replay = 1 | ||
| ) | ||
|
|
||
| override val colorFlow: Flow<Int> | ||
| get() = _colorFlow.asSharedFlow() | ||
|
|
||
| override fun fireColorEvent(color: Int) { | ||
| _colorFlow.tryEmit(color) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,25 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.fragment.app.FragmentActivity | ||
| import ru.otus.daggerhomework.di.DaggerMainActivityComponent | ||
| import ru.otus.daggerhomework.di.MainActivityComponent | ||
|
|
||
| class MainActivity : FragmentActivity(), MainActivityComponentProvider { | ||
| private lateinit var component: MainActivityComponent | ||
|
|
||
| class MainActivity : ComponentActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| setContentView(R.layout.activity_main) | ||
|
|
||
| component = DaggerMainActivityComponent.factory().build(application.component) | ||
|
|
||
| } | ||
|
|
||
| override fun provideMainActivityComponent(): MainActivityComponent = component | ||
| } | ||
|
|
||
| interface MainActivityComponentProvider { | ||
| fun provideMainActivityComponent(): MainActivityComponent | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention(value = AnnotationRetention.RUNTIME) | ||
| annotation class ActivityScope() |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/ru/otus/daggerhomework/di/ApplicationComponent.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| @Component | ||
| interface ApplicationComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build(@BindsInstance context: Context): ApplicationComponent | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/ru/otus/daggerhomework/di/ColorGeneratorFeatureModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import dagger.Module | ||
| import dagger.Provides | ||
| import ru.otus.daggerhomework.ColorObserver | ||
| import ru.otus.daggerhomework.ColorObserverImpl | ||
|
|
||
| @Module | ||
| class ColorGeneratorFeatureModule { | ||
|
|
||
| @Provides | ||
| @ActivityScope | ||
| fun colorObserver(): ColorObserver = ColorObserverImpl() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention(value = AnnotationRetention.RUNTIME) | ||
| annotation class FragmentScope |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/ru/otus/daggerhomework/di/MainActivityComponent.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import dagger.Component | ||
| import ru.otus.daggerhomework.ColorObserver | ||
|
|
||
|
|
||
| @ActivityScope | ||
| @Component( | ||
| modules = [ColorGeneratorFeatureModule::class], | ||
| dependencies = [ApplicationComponent::class] | ||
| ) | ||
| interface MainActivityComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build(appComponent: ApplicationComponent): MainActivityComponent | ||
| } | ||
|
|
||
| fun colorObserver(): ColorObserver | ||
| } |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/ru/otus/daggerhomework/di/ProducerFragmentComponent.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import ru.otus.daggerhomework.ColorGenerator | ||
| import ru.otus.daggerhomework.ProducerViewModel | ||
|
|
||
| @FragmentScope | ||
| @Component( | ||
| modules = [ProducerFragmentModule::class], | ||
| dependencies = [MainActivityComponent::class, ApplicationComponent::class] | ||
| ) | ||
| interface ProducerFragmentComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build( | ||
| @BindsInstance | ||
| contextActivity: Context, | ||
| appComponent: ApplicationComponent, | ||
| mainActivityComponent: MainActivityComponent, | ||
| ): ProducerFragmentComponent | ||
| } | ||
|
|
||
| fun colorGenerator(): ColorGenerator | ||
|
|
||
| fun producerVM(): ProducerViewModel | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
app/src/main/java/ru/otus/daggerhomework/di/ProducerFragmentModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import dagger.Module | ||
| import dagger.Provides | ||
| import ru.otus.daggerhomework.ColorGenerator | ||
| import ru.otus.daggerhomework.ColorGeneratorImpl | ||
|
|
||
| @Module | ||
| class ProducerFragmentModule { | ||
|
|
||
| @Provides | ||
| @FragmentScope | ||
| fun colorGenerator(): ColorGenerator = ColorGeneratorImpl() | ||
| } |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/ru/otus/daggerhomework/di/ReceiverFragmentComponent.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import ru.otus.daggerhomework.ReceiverFragment | ||
| import ru.otus.daggerhomework.ReceiverViewModel | ||
|
|
||
| @FragmentScope | ||
| @Component( | ||
| dependencies = [MainActivityComponent::class, ApplicationComponent::class] | ||
| ) | ||
| interface ReceiverFragmentComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build( | ||
| @BindsInstance | ||
| contextActivity: Context, | ||
| @BindsInstance | ||
| fragment: ReceiverFragment, | ||
| appComponent: ApplicationComponent, | ||
| mainActivityComponent: MainActivityComponent, | ||
| ): ReceiverFragmentComponent | ||
| } | ||
|
|
||
| fun producerVM(): ReceiverViewModel | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
В целом так сделать можно и оно будет работать.
Но чаще делают так:
Для ProducerFragmentComponent делают
interface ProducerFragmentComponentDependenciesКоторый определяет просто методы или val что нужно для
ProducerFragmentComponent.И потом, кто-то, реализует этот компонент. Его может реализовать или
MainActivityComponent, или это будет прямо новый отдельный класс, который создастMainActivityComponentвнутри себя.Если будете использовать даггер на курсовом проекте, то лучше именно так сделать. Если будут вопросы - можем на консультациях обсудить это.