-
Notifications
You must be signed in to change notification settings - Fork 174
Dagger2Homework #107
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?
Dagger2Homework #107
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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention(AnnotationRetention.RUNTIME) | ||
|
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. Можно убрать |
||
| annotation class AppScope() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,31 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import dagger.Module | ||
| import javax.inject.Singleton | ||
|
|
||
| @AppScope | ||
| @Component(modules = [ViewModelModule::class]) | ||
| interface ApplicationComponent { | ||
|
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. Нужно добавить ActivityComponent |
||
|
|
||
| fun inject(activity: MainActivity) | ||
|
|
||
|
|
||
| fun provideMyViewModel(): MyViewModel | ||
|
|
||
|
|
||
| fun provideColorGenerator(): ColorGenerator | ||
|
|
||
|
|
||
| fun provideContext(): Context | ||
|
|
||
| @Component.Factory | ||
| interface AppComponentFactory { | ||
|
|
||
| fun create( | ||
| @BindsInstance context: Context | ||
| ): ApplicationComponent | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,27 @@ | ||
| 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 javax.inject.Inject | ||
|
|
||
| class FragmentProducer : Fragment() { | ||
|
|
||
| private val subcomponent by lazy { | ||
|
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. Удалить |
||
| FragmentProducerComponent.getFragmentProducerComponent((requireContext() as App).component) | ||
| } | ||
|
|
||
| @Inject | ||
| lateinit var viewModelProducer: ViewModelProducer | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
| subcomponent.inject(this) | ||
| } | ||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
|
|
@@ -20,7 +33,7 @@ class FragmentProducer : Fragment() { | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| view.findViewById<Button>(R.id.button).setOnClickListener { | ||
| //отправить результат через livedata в другой фрагмент | ||
| viewModelProducer.generateColor() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import dagger.Subcomponent | ||
|
|
||
| @MyScope | ||
| @Component(dependencies = [ApplicationComponent::class]) | ||
| interface FragmentProducerComponent { | ||
|
|
||
| fun inject(fragment: FragmentProducer) | ||
|
|
||
| companion object{ | ||
| fun getFragmentProducerComponent(applicationComponent: ApplicationComponent): FragmentProducerComponent { | ||
| return DaggerFragmentProducerComponent.builder().applicationComponent(applicationComponent).build() | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,31 @@ | ||
| 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 javax.inject.Inject | ||
|
|
||
| class FragmentReceiver : Fragment() { | ||
|
|
||
| private lateinit var frame: View | ||
|
|
||
| private val subcomponent by lazy { | ||
|
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. Нужно убрать |
||
| FragmentReceiverComponent.getFragmentReceiverComponent((requireContext() as App).component) | ||
| } | ||
|
|
||
| @Inject | ||
| lateinit var мiewModelReceiver: ViewModelReceiver | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
| subcomponent.inject(this) | ||
| } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
|
|
@@ -23,6 +37,9 @@ class FragmentReceiver : Fragment() { | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| frame = view.findViewById(R.id.frame) | ||
| мiewModelReceiver.colorLiveData.observe(viewLifecycleOwner){ | ||
| populateColor(it) | ||
| } | ||
| } | ||
|
|
||
| fun populateColor(@ColorInt color: Int) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
|
|
||
| import dagger.Component | ||
|
|
||
| @MyScope | ||
| @Component(dependencies = [ApplicationComponent::class]) | ||
| interface FragmentReceiverComponent { | ||
|
|
||
| fun inject(fragment: FragmentReceiver) | ||
|
|
||
| companion object{ | ||
| fun getFragmentReceiverComponent(applicationComponent: ApplicationComponent): FragmentReceiverComponent { | ||
| return DaggerFragmentReceiverComponent.builder().applicationComponent(applicationComponent).build() | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,26 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import android.os.Bundle | ||
| import android.widget.FrameLayout | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import javax.inject.Inject | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| private val component by lazy { | ||
| (application as App).component | ||
| } | ||
|
|
||
| @SuppressLint("MissingInflatedId") | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
|
|
||
| component.inject(this) | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention(AnnotationRetention.RUNTIME) | ||
| annotation class MyScope() | ||
|
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. Лучше переименовать |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import androidx.lifecycle.* | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.flow | ||
| import kotlinx.coroutines.flow.flowOf | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| class MyViewModel @Inject constructor() : ViewModel() { | ||
|
|
||
| private val _catsStateFlow = MutableStateFlow<Int>(-1) | ||
| val catsStateFlow: StateFlow<Int> = _catsStateFlow | ||
|
|
||
| fun sendEvent(color: Int){ | ||
| viewModelScope.launch(){ | ||
| _catsStateFlow.value = color | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class MyViewModelFactory @Inject constructor() : | ||
| ViewModelProvider.NewInstanceFactory() { | ||
| override fun <T : ViewModel> create(modelClass: Class<T>): T = | ||
| MyViewModel() as T | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import androidx.lifecycle.ViewModel | ||
| import dagger.Binds | ||
| import dagger.Module | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| interface ViewModelModule { | ||
|
|
||
|
|
||
| @AppScope | ||
| @Binds | ||
| fun bindMyViewModel(impl: MyViewModel): ViewModel | ||
|
|
||
|
|
||
| @Binds | ||
| fun bindColorGenerator(impl: ColorGeneratorImpl): ColorGenerator | ||
|
|
||
| } |
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.
Убрать либо заменить на несинхронизированный