-
Notifications
You must be signed in to change notification settings - Fork 173
Выполнение всех заданий #62
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 1 commit
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,17 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| import ru.otus.daggerhomework.di.components.ApplicationComponent | ||
| import ru.otus.daggerhomework.di.components.DaggerApplicationComponent | ||
|
|
||
| class App :Application() { | ||
| class App : Application() { | ||
| lateinit var appComponent: ApplicationComponent | ||
|
|
||
| override fun onCreate() { | ||
| super.onCreate() | ||
|
|
||
| appComponent = DaggerApplicationComponent | ||
| .factory() | ||
| .create(this) | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,62 @@ | ||
| 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.lifecycle.lifecycleScope | ||
| import kotlinx.coroutines.launch | ||
| import ru.otus.daggerhomework.di.ActivityContext | ||
| import ru.otus.daggerhomework.di.ApplicationContext | ||
| import ru.otus.daggerhomework.di.components.DaggerFragmentProducerComponent | ||
| import ru.otus.daggerhomework.di.components.FragmentProducerComponent | ||
| import javax.inject.Inject | ||
| import javax.inject.Provider | ||
|
|
||
| class FragmentProducer : Fragment() { | ||
|
|
||
| @Inject | ||
| lateinit var dataKeeper: Provider<IDataKeeper> | ||
|
|
||
| @Inject | ||
| @ActivityContext | ||
| lateinit var activityContext: Context | ||
|
|
||
| lateinit var fragmentProducerComponent: FragmentProducerComponent | ||
|
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. Зачем тебе ссылка на компонент на уровне класса? |
||
| private val colorGenerator = ColorGeneratorImpl() | ||
|
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. А его как раз нужно инжектить во вьюмодель |
||
| lateinit var viewModelProducer: ViewModelProducer | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View? { | ||
| return inflater.inflate(R.layout.fragment_a, container, true) | ||
| val activityComponent = (requireActivity() as MainActivity).activityComponent | ||
| fragmentProducerComponent = DaggerFragmentProducerComponent | ||
|
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. Лучше его создавать в onCreate/onAttach, сейчас он будет пересоздаваться каждый раз когда фрагмент будет класться/доставаться из бекстека |
||
| .builder() | ||
| .activityComponent(activityComponent) | ||
| .build() | ||
|
|
||
| fragmentProducerComponent.inject(this) | ||
| viewModelProducer = ViewModelProducer( | ||
| colorGenerator, | ||
| activityContext, | ||
| dataKeeper.get() | ||
| ) | ||
|
|
||
| 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 в другой фрагмент | ||
| lifecycleScope.launch { | ||
| //отправить результат через livedata в другой фрагмент | ||
| viewModelProducer.generateColor() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,72 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import android.os.Bundle | ||
| import android.util.Log | ||
| 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.lifecycle.lifecycleScope | ||
| import kotlinx.coroutines.flow.collect | ||
| import kotlinx.coroutines.launch | ||
| import ru.otus.daggerhomework.di.ActivityContext | ||
| import ru.otus.daggerhomework.di.ApplicationContext | ||
| import ru.otus.daggerhomework.di.components.DaggerFragmentReceiverComponent | ||
| import ru.otus.daggerhomework.di.components.FragmentReceiverComponent | ||
| import javax.inject.Inject | ||
| import javax.inject.Provider | ||
|
|
||
|
|
||
| class FragmentReceiver : Fragment() { | ||
|
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. Аналогичные замечания |
||
|
|
||
| @Inject | ||
| lateinit var dataKeeper: Provider<IDataKeeper> | ||
|
|
||
| @Inject | ||
| @ApplicationContext | ||
| lateinit var applicationContext: Context | ||
|
|
||
| private lateinit var frame: View | ||
| private lateinit var fragmentReceiverComponent: FragmentReceiverComponent | ||
| private lateinit var viewModelReceiver: ViewModelReceiver | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View? { | ||
| return inflater.inflate(R.layout.fragment_b, container, true) | ||
| val activityComponent = (requireActivity() as MainActivity).activityComponent | ||
| fragmentReceiverComponent = DaggerFragmentReceiverComponent | ||
| .builder() | ||
| .activityComponent(activityComponent) | ||
| .build() | ||
| fragmentReceiverComponent.inject(this) | ||
|
|
||
| viewModelReceiver = ViewModelReceiver( | ||
| applicationContext, | ||
| dataKeeper.get() | ||
| ) | ||
| lifecycleScope.launch { | ||
| viewModelReceiver.observeColors() | ||
| } | ||
|
|
||
| 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) | ||
|
|
||
| lifecycleScope.launch { | ||
| viewModelReceiver.colorData.collect { colorCode -> | ||
| populateColor(colorCode) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun populateColor(@ColorInt color: Int) { | ||
| private fun populateColor(@ColorInt color: Int) { | ||
| frame.setBackgroundColor(color) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import ru.otus.daggerhomework.di.ActivityScope | ||
| import javax.inject.Inject | ||
|
|
||
|
|
||
| @ActivityScope | ||
|
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. Здесь не нужен скоуп |
||
| interface IDataKeeper { | ||
|
|
||
| suspend fun saveData(newData: Int) | ||
|
|
||
| fun getFlow(): StateFlow<Int> | ||
| } | ||
|
|
||
| @ActivityScope | ||
|
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. Здесь тоже. Скоуп нужен если ты будешь инжектить конкретно этот тип, а ты инжектишь IDataKeeper и биндишь его помечая скоупом в даггеровском модуле |
||
| class DataKeeper @Inject constructor() : IDataKeeper { | ||
|
|
||
| private val _colorData = MutableStateFlow<Int>(0) | ||
| private val colorData: StateFlow<Int> = _colorData | ||
|
|
||
| override suspend fun saveData(newData: Int) { | ||
| _colorData.emit(newData) | ||
| } | ||
|
|
||
| override fun getFlow(): StateFlow<Int> { | ||
| return colorData | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,39 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.FragmentContainerView | ||
| import ru.otus.daggerhomework.di.components.ActivityComponent | ||
| import ru.otus.daggerhomework.di.components.DaggerActivityComponent | ||
| import javax.inject.Inject | ||
| import javax.inject.Provider | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| lateinit var activityComponent: ActivityComponent | ||
|
|
||
| @Inject | ||
| lateinit var dataKeeper: Provider<IDataKeeper> | ||
|
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. Разве он нужен здесь? |
||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| val appComponent = (application as App).appComponent | ||
| activityComponent = DaggerActivityComponent | ||
| .factory() | ||
| .create( | ||
| appComponent, | ||
| this | ||
| ) | ||
|
|
||
| setContentView(R.layout.activity_main) | ||
|
|
||
| supportFragmentManager | ||
| .beginTransaction() | ||
| .add(R.id.fragmentContainer, FragmentReceiver()) | ||
| .add(R.id.fragmentContainer, FragmentProducer()) | ||
| .commit() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,19 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import android.util.Log | ||
| import android.widget.Toast | ||
| import androidx.fragment.app.FragmentActivity | ||
|
|
||
| class ViewModelProducer( | ||
| private val colorGenerator: ColorGenerator, | ||
| private val context: Context | ||
| private val context: Context, | ||
| private val dataKeeper: IDataKeeper | ||
| ) { | ||
|
|
||
| fun generateColor() { | ||
| suspend fun generateColor() { | ||
| if (context !is FragmentActivity) throw RuntimeException("Здесь нужен контекст активити") | ||
| Toast.makeText(context, "Color sent", Toast.LENGTH_LONG).show() | ||
|
|
||
| dataKeeper.saveData(colorGenerator.generateColor()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import javax.inject.Qualifier | ||
|
|
||
| @Qualifier | ||
| annotation class ApplicationContext | ||
|
|
||
| @Qualifier | ||
| annotation class ActivityContext |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| annotation class ApplicationScope | ||
|
|
||
| @Scope | ||
| annotation class ActivityScope | ||
|
|
||
| @Scope | ||
| annotation class FragmentScope |
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.
Его можно через requireContext запросить, не вижу смысла инжектить