-
Notifications
You must be signed in to change notification settings - Fork 174
Hw4/dagger Хубулов Георгий #150
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
GeorgeBozon
wants to merge
3
commits into
Otus-Android:master
Choose a base branch
from
GeorgeBozon:HW4/Dagger
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
Changes from all commits
Commits
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
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
22 changes: 21 additions & 1 deletion
22
app/src/main/java/ru/otus/daggerhomework/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 |
|---|---|---|
| @@ -1,4 +1,24 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import javax.inject.Qualifier | ||
| import javax.inject.Singleton | ||
|
|
||
| @Component | ||
| @Singleton | ||
| interface ApplicationComponent { | ||
| } | ||
|
|
||
| @ApplicationContext | ||
| fun provideContext(): Context | ||
|
|
||
| @Component.Factory | ||
| interface Factory{ | ||
|
|
||
| fun newAppComponent(@ApplicationContext @BindsInstance context: Context): ApplicationComponent | ||
| } | ||
| } | ||
|
|
||
| @Qualifier | ||
| annotation class ApplicationContext |
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 | ||
|
|
||
| import kotlinx.coroutines.flow.Flow | ||
|
|
||
| interface EventsReader { | ||
| val eventsFlow: Flow<Int> | ||
| } |
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,5 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| interface EventsWriter { | ||
| fun writeEvent(color: Int) | ||
| } |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/ru/otus/daggerhomework/EventsWriterReader.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,17 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.asSharedFlow | ||
| import javax.inject.Inject | ||
|
|
||
| class EventsWriterReader @Inject constructor() : EventsReader, EventsWriter { | ||
| private val _eventsFlow = MutableSharedFlow<Int>(replay = 1) | ||
|
|
||
| override val eventsFlow: Flow<Int> | ||
| get() = _eventsFlow.asSharedFlow() | ||
|
|
||
| override fun writeEvent(color: Int) { | ||
| _eventsFlow.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
23 changes: 23 additions & 0 deletions
23
app/src/main/java/ru/otus/daggerhomework/FragmentProducerComponent.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,23 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import dagger.Component | ||
| import javax.inject.Scope | ||
|
|
||
| @FragmentScope | ||
| @Component( | ||
| dependencies = [MainActivityComponent::class] | ||
| ) | ||
| interface FragmentProducerComponent { | ||
| fun inject(fragmentProducer: FragmentProducer) | ||
|
|
||
| companion object { | ||
| fun getFragmentProducerComponent(mainActivityComponent: MainActivityComponent): FragmentProducerComponent { | ||
| return DaggerFragmentProducerComponent.builder() | ||
| .mainActivityComponent(mainActivityComponent).build() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Scope | ||
| @Retention(AnnotationRetention.RUNTIME) | ||
| annotation class FragmentScope |
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
19 changes: 19 additions & 0 deletions
19
app/src/main/java/ru/otus/daggerhomework/FragmentReceiverComponent.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,19 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import dagger.Component | ||
|
|
||
| @FragmentScope | ||
| @Component( | ||
| dependencies = [MainActivityComponent::class] | ||
| ) | ||
| interface FragmentReceiverComponent { | ||
|
|
||
| fun inject(fragmentReceiver: FragmentReceiver) | ||
|
|
||
| companion object { | ||
| fun getFragmentReceiverComponent(mainActivityComponent: MainActivityComponent): FragmentReceiverComponent { | ||
| return DaggerFragmentReceiverComponent.builder() | ||
| .mainActivityComponent(mainActivityComponent).build() | ||
| } | ||
| } | ||
| } |
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
40 changes: 40 additions & 0 deletions
40
app/src/main/java/ru/otus/daggerhomework/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,40 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import javax.inject.Qualifier | ||
| import javax.inject.Scope | ||
|
|
||
| @Component(dependencies = [ApplicationComponent::class], | ||
| modules = [MainActivityModule::class] | ||
| ) | ||
| @ActivityScope | ||
| interface MainActivityComponent { | ||
|
|
||
| fun getColorGenerator(): ColorGenerator | ||
|
|
||
| fun getEventReader(): EventsReader | ||
|
|
||
| fun getEventWriter(): EventsWriter | ||
|
|
||
| @ApplicationContext | ||
| fun provideContext(): Context | ||
|
|
||
| @ActivityContext | ||
| fun provideActivityContext(): Context | ||
|
|
||
| @Component.Factory | ||
| interface Factory{ | ||
| fun create( | ||
| @ActivityContext @BindsInstance context: Context, | ||
| applicationComponent: ApplicationComponent | ||
| ): MainActivityComponent | ||
| } | ||
| } | ||
|
|
||
| @Qualifier | ||
| annotation class ActivityContext | ||
|
|
||
| @Scope | ||
| annotation class ActivityScope |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/ru/otus/daggerhomework/MainActivityModule.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 | ||
|
|
||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.Provides | ||
|
|
||
| @Module | ||
| interface MainActivityModule { | ||
|
|
||
| @Binds | ||
| @ActivityScope | ||
| fun bindColorGenerator(colorGeneratorImpl: ColorGeneratorImpl): ColorGenerator | ||
|
|
||
| @Binds | ||
| @ActivityScope | ||
| fun bindEventWriter(eventsWriter: EventsWriterReader): EventsWriter | ||
|
|
||
| @Binds | ||
| @ActivityScope | ||
| fun bindEventReader(eventsReader: EventsWriterReader): EventsReader | ||
|
|
||
| companion object{ | ||
|
|
||
| @Provides | ||
| @ActivityScope | ||
| fun provideEventsReaderWriter() = EventsWriterReader() | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -3,14 +3,17 @@ package ru.otus.daggerhomework | |
| import android.content.Context | ||
| import android.widget.Toast | ||
| import androidx.fragment.app.FragmentActivity | ||
| import javax.inject.Inject | ||
|
|
||
| class ViewModelProducer( | ||
| class ViewModelProducer @Inject constructor( | ||
| private val colorGenerator: ColorGenerator, | ||
| private val context: Context | ||
| ) { | ||
| @ActivityContext private val context: 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. Здесь будет утечка памяти, нельзя делать наследование от androidx.lifecycle.ViewModel
Author
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. исправил |
||
| private val eventsWriter: EventsWriter | ||
| ){ | ||
|
|
||
| fun generateColor() { | ||
| if (context !is FragmentActivity) throw RuntimeException("Здесь нужен контекст активити") | ||
| Toast.makeText(context, "Color sent", Toast.LENGTH_LONG).show() | ||
| eventsWriter.writeEvent(colorGenerator.generateColor()) | ||
| } | ||
| } | ||
| } | ||
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.
А зачем тебе provides здесь. Достаточно Inject над конструктором
Uh oh!
There was an error while loading. Please reload this page.
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.
ну да, поправлю