-
Notifications
You must be signed in to change notification settings - Fork 174
Dagger2 homework Белянин Роман #37
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
BelRom
wants to merge
3
commits into
Otus-Android:master
Choose a base branch
from
BelRom: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
Changes from 2 commits
Commits
Show all changes
3 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
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 javax.inject.Qualifier | ||
|
|
||
| @Qualifier | ||
| @Retention(AnnotationRetention.BINARY) | ||
| annotation class ActivityContext |
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 javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention | ||
| annotation class ActivityScope |
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
37 changes: 37 additions & 0 deletions
37
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,41 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| import android.content.Context | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import dagger.* | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| @Component(modules = [ApplicationModule::class]) | ||
| interface ApplicationComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun create( | ||
| @BindsInstance @ApplicationContext context: Context | ||
| ): ApplicationComponent | ||
| } | ||
|
|
||
| @ApplicationContext | ||
| fun provideApplicationContext(): Context | ||
|
|
||
| fun provideColorGenerator(): ColorGenerator | ||
|
|
||
| fun provideState(): MutableStateFlow<Int> | ||
|
||
| } | ||
|
|
||
| @Module | ||
| interface ApplicationModule { | ||
|
|
||
| companion object { | ||
| @Provides | ||
| @Singleton | ||
| fun provideState() = MutableStateFlow(0) | ||
| } | ||
|
|
||
| @Binds | ||
| @Singleton | ||
| fun bindColorGenerator(generator: ColorGeneratorImpl): ColorGenerator | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ru/otus/daggerhomework/ApplicationContext.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,7 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import javax.inject.Qualifier | ||
|
|
||
| @Qualifier | ||
| @Retention(AnnotationRetention.BINARY) | ||
| 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
19 changes: 17 additions & 2 deletions
19
app/src/main/java/ru/otus/daggerhomework/FragmentProducer.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,26 +1,41 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.Button | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.viewModels | ||
| import javax.inject.Inject | ||
|
|
||
| class FragmentProducer : Fragment() { | ||
|
|
||
|
|
||
| @Inject | ||
| lateinit var viewModelFactory: ProducerFactory | ||
| val viewModel: ViewModelProducer by viewModels {viewModelFactory} | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
| FragmentProducerComponent.crete( | ||
| (requireActivity() as MainActivity).activityComponent | ||
| ).inject(this) | ||
| } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View? { | ||
| return inflater.inflate(R.layout.fragment_a, container, true) | ||
| return inflater.inflate(R.layout.fragment_a, container, false) | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| view.findViewById<Button>(R.id.button).setOnClickListener { | ||
| //отправить результат через livedata в другой фрагмент | ||
| viewModel.generateColor() | ||
| } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import dagger.Component | ||
|
|
||
| @FragmentScope | ||
| @Component(dependencies = [MainActivityComponent::class]) | ||
| interface FragmentProducerComponent { | ||
|
|
||
| companion object { | ||
| fun crete( | ||
| activityComponent: MainActivityComponent | ||
| ) = DaggerFragmentProducerComponent.builder() | ||
| .mainActivityComponent(activityComponent) | ||
| .build() | ||
|
|
||
| } | ||
| fun inject(fragment: FragmentProducer) | ||
|
|
||
| } |
25 changes: 24 additions & 1 deletion
25
app/src/main/java/ru/otus/daggerhomework/FragmentReceiver.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,28 +1,51 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.Button | ||
| import androidx.annotation.ColorInt | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.viewModels | ||
| import kotlinx.coroutines.GlobalScope | ||
| import kotlinx.coroutines.flow.collect | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
| import javax.inject.Named | ||
|
|
||
| class FragmentReceiver : Fragment() { | ||
|
|
||
| @Inject | ||
| lateinit var viewModelFactory: ReceiverFactory | ||
| val viewModel: ViewModelReceiver by viewModels {viewModelFactory} | ||
|
|
||
| private lateinit var frame: View | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
| FragmentReceiverComponent | ||
| .create((requireContext().applicationContext as App).appComponent) | ||
| .inject(this) | ||
| } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View? { | ||
| return inflater.inflate(R.layout.fragment_b, container, true) | ||
| return inflater.inflate(R.layout.fragment_b, container, false) | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| frame = view.findViewById(R.id.frame) | ||
| GlobalScope.launch { | ||
|
||
| viewModel.stateFlow.collect { | ||
| populateColor(it) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun populateColor(@ColorInt color: Int) { | ||
|
|
||
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import dagger.Component | ||
|
|
||
| @FragmentScope | ||
| @Component(dependencies = [ApplicationComponent::class]) | ||
| interface FragmentReceiverComponent { | ||
| companion object { | ||
|
|
||
| fun create (component: ApplicationComponent) = | ||
| DaggerFragmentReceiverComponent | ||
| .builder() | ||
| .applicationComponent(component) | ||
| .build() | ||
| } | ||
| fun inject(fragment: FragmentReceiver) | ||
| } |
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 javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,27 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.fragment.app.FragmentTransaction | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
|
|
||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| lateinit var activityComponent: MainActivityComponent | ||
|
|
||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| activityComponent = DaggerMainActivityComponent.factory() | ||
| .crete(this, (this.applicationContext as App).appComponent) | ||
| activityComponent.inject(this) | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
|
|
||
| val manager = supportFragmentManager | ||
| val transaction: FragmentTransaction = manager.beginTransaction() | ||
| transaction.add(R.id.container_fragment, FragmentProducer()).commit() | ||
| val transaction2: FragmentTransaction = manager.beginTransaction() | ||
| transaction2.add(R.id.container_fragment2, FragmentReceiver()).commit() | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
|
|
||
| @ActivityScope | ||
| @Component(dependencies = [ApplicationComponent::class]) | ||
| interface MainActivityComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun crete( | ||
| @ActivityContext @BindsInstance context: Context, | ||
| applicationComponent: ApplicationComponent, | ||
| ): MainActivityComponent | ||
| } | ||
|
|
||
|
|
||
| fun inject(activity: MainActivity) | ||
|
|
||
| @ApplicationContext | ||
| fun provideApplicationContext(): Context | ||
|
|
||
| @ActivityContext | ||
| fun provideActivityContext(): Context | ||
|
|
||
| fun provideColorGenerator(): ColorGenerator | ||
|
|
||
| fun provideState(): MutableStateFlow<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
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.
ColorGenerator не нужен на уровне всего Application, по хорошему он должен быть в модуле у ProducerFragmentComponent