-
Notifications
You must be signed in to change notification settings - Fork 173
[ДЗ | Dagger 2] - Егор Шкляревский #151
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,4 +1,17 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
|
|
||
| @Component | ||
| interface ApplicationComponent { | ||
|
|
||
| fun provideApplication(): Application | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
|
|
||
| fun create(@BindsInstance application: Application): ApplicationComponent | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,42 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| 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() { | ||
|
|
||
| @Inject | ||
| lateinit var application: Application | ||
|
|
||
| @Inject | ||
| lateinit var viewModelProducer: ViewModelProducer | ||
|
|
||
| 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) | ||
|
|
||
| DaggerFragmentProducerComponent | ||
| .builder() | ||
| .applicationComponent((requireActivity().application as App).getAppComponent()) | ||
| .mainActivityComponent((requireActivity() as MainActivity).getComponent()) | ||
| .build() | ||
| .inject(this) | ||
|
|
||
| view.findViewById<Button>(R.id.button).setOnClickListener { | ||
| //отправить результат через livedata в другой фрагмент | ||
| viewModelProducer.generateColor() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.* | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
|
|
||
| @Component( | ||
| modules = [ViewModelProducerModule::class], | ||
| dependencies = [ApplicationComponent::class, MainActivityComponent::class] | ||
| ) | ||
| interface FragmentProducerComponent { | ||
|
|
||
| fun inject(fragmentProducer: FragmentProducer) | ||
| } | ||
|
|
||
| @Module | ||
| object ViewModelProducerModule { | ||
|
|
||
| @Provides | ||
| fun provide(colorGenerator: ColorGenerator, context: Context, flow: MutableStateFlow<Int>): ViewModelProducer { | ||
| return ViewModelProducer(colorGenerator, context, flow) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,49 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.Button | ||
| import android.view.* | ||
| import androidx.annotation.ColorInt | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.lifecycle.* | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| class FragmentReceiver : Fragment() { | ||
|
|
||
| @Inject | ||
| lateinit var application: Application | ||
|
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. Зачем нужен этот инжект?
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. А, это я просто пробовал) уберу |
||
|
|
||
| @Inject | ||
| lateinit var viewModelReceiver: ViewModelReceiver | ||
|
|
||
| private lateinit var frame: View | ||
|
|
||
| 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) | ||
|
|
||
| DaggerFragmentReceiverComponent | ||
| .builder() | ||
| .applicationComponent((requireActivity().application as App).getAppComponent()) | ||
| .mainActivityComponent((requireActivity() as MainActivity).getComponent()) | ||
| .build() | ||
| .inject(this) | ||
|
|
||
| frame = view.findViewById(R.id.frame) | ||
|
|
||
| lifecycleScope.launch { | ||
| viewModelReceiver.observeColors().collect { color -> | ||
| color.takeIf { it != -1 }?.let(::populateColor) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun populateColor(@ColorInt color: Int) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| import dagger.* | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
|
|
||
| @Component( | ||
| modules = [ViewModelReceiverModule::class], | ||
| dependencies = [ApplicationComponent::class, MainActivityComponent::class] | ||
| ) | ||
| interface FragmentReceiverComponent { | ||
|
|
||
| fun inject(fragmentReceiver: FragmentReceiver) | ||
| } | ||
|
|
||
| @Module | ||
| object ViewModelReceiverModule { | ||
|
|
||
| @Provides | ||
|
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. Тоже можно поменять на binds
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. Немного не понимаю как, я же тут из параметров создаю вьюмодель. По мне биндс не подходит |
||
| fun provide(context: Application, flow: MutableStateFlow<Int>): ViewModelReceiver { | ||
| return ViewModelReceiver(context, flow) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.* | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
|
|
||
| @Component(modules = [ColorGeneratorModule::class]) | ||
| interface MainActivityComponent { | ||
|
|
||
| fun provideContext(): Context | ||
|
|
||
| fun provideFlow(): MutableStateFlow<Int> | ||
|
|
||
| fun provideColorGenerator(): ColorGenerator | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
|
|
||
| fun create( | ||
| @BindsInstance context: Context, | ||
| @BindsInstance flow: MutableStateFlow<Int> | ||
| ): MainActivityComponent | ||
| } | ||
| } | ||
|
|
||
| @Module | ||
| object ColorGeneratorModule { | ||
|
|
||
| @Provides | ||
|
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. Это нужно заменить на binds |
||
| fun provide(colorGeneratorImpl: ColorGeneratorImpl): ColorGenerator { | ||
| return colorGeneratorImpl | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,19 @@ package ru.otus.daggerhomework | |
| import android.app.Application | ||
| import android.content.Context | ||
| import android.widget.Toast | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import javax.inject.Inject | ||
|
|
||
| class ViewModelReceiver( | ||
| private val context: Context | ||
| class ViewModelReceiver @Inject constructor( | ||
| private val context: Context, | ||
| private val colorFlow: MutableStateFlow<Int> | ||
|
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. Здесь лучше инжектить иммутабельную версию |
||
| ) { | ||
|
|
||
| fun observeColors() { | ||
| fun observeColors(): Flow<Int> { | ||
| if (context !is Application) throw RuntimeException("Здесь нужен контекст апликейшена") | ||
| Toast.makeText(context, "Color received", Toast.LENGTH_LONG).show() | ||
|
|
||
| return colorFlow | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,8 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.constraintlayout.widget.ConstraintLayout | ||
| android:id="@+id/container" | ||
| xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:app="http://schemas.android.com/apk/res-auto" | ||
| xmlns:tools="http://schemas.android.com/tools" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| tools:context=".MainActivity"> | ||
|
|
||
| <TextView | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="Hello World!" | ||
| app:layout_constraintBottom_toBottomOf="parent" | ||
| app:layout_constraintLeft_toLeftOf="parent" | ||
| app:layout_constraintRight_toRightOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" /> | ||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||
| tools:context=".MainActivity" /> |
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.
А зачем тебе этот инжект?
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.
А, это я просто пробовал) уберу