-
Notifications
You must be signed in to change notification settings - Fork 174
homework 4 #174
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?
homework 4 #174
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 |
|---|---|---|
| @@ -1,4 +1,21 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import javax.inject.Named | ||
| import javax.inject.Qualifier | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| @Component | ||
| interface ApplicationComponent { | ||
| } | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build(@BindsInstance app: App) : ApplicationComponent | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,38 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.graphics.Color | ||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.fragment.app.FragmentActivity | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention(AnnotationRetention.RUNTIME) | ||
| annotation class MainActivityScope() | ||
|
|
||
| class MainActivity() : FragmentActivity(), ColorSaver, ColorState | ||
| { | ||
| private val _state = MutableStateFlow(Color.BLACK) | ||
|
|
||
| override val color: StateFlow<Int> = _state | ||
|
|
||
| override fun saveColor(color: Int) { | ||
| _state.value = color | ||
| } | ||
|
|
||
| lateinit var component: MainActivityComponent | ||
|
|
||
| class MainActivity : ComponentActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
| component = DaggerMainActivityComponent.factory().build(applicationContext,this) | ||
| try { | ||
| setContentView(R.layout.activity_main) | ||
| } | ||
| catch (e: Exception) { | ||
| println(e) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val MainActivity.Component: MainActivityComponent get() = this.component | ||
|
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,57 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import javax.inject.Qualifier | ||
|
|
||
| @MainActivityScope | ||
| @Component( | ||
|
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. это должен быть |
||
| modules = [ColorModule::class, MainActivityModule::class]) | ||
| interface MainActivityComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build(@BindsInstance appContext: Context, | ||
|
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. тут не нужно прокидывать и |
||
| @BindsInstance activity: MainActivity): MainActivityComponent | ||
| } | ||
|
|
||
| fun producerComponent(): ProducerFragmentComponent.Factory | ||
|
|
||
| fun receiverComponent(): ReceiverFragmentComponent.Factory | ||
| } | ||
|
|
||
| @Qualifier | ||
| @Retention(AnnotationRetention.RUNTIME) | ||
| annotation class MainActivityContext | ||
|
|
||
| @Module | ||
| object MainActivityModule { | ||
|
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. этот модуль будет не нужен |
||
| @Provides | ||
| @MainActivityContext | ||
| fun getMainActivityContext(activity: MainActivity): Context = activity | ||
|
|
||
| } | ||
|
|
||
| @Module | ||
| class ColorModule { | ||
|
|
||
| @Provides | ||
| @MainActivityScope | ||
| fun colorState(activity: MainActivity) : ColorState = activity | ||
|
|
||
| @Provides | ||
| @MainActivityScope | ||
| fun colorSetter(activity: MainActivity) : ColorSaver = activity | ||
| } | ||
|
|
||
| interface ColorSaver{ | ||
| fun saveColor(color: Int) | ||
| } | ||
|
|
||
| interface ColorState{ | ||
| val color: StateFlow<Int> | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import dagger.Subcomponent | ||
|
|
||
| @ProducerFragmentScope | ||
| @Subcomponent( | ||
| modules = [ColorGenModule::class]) | ||
| interface ProducerFragmentComponent { | ||
|
|
||
| @Subcomponent.Factory | ||
| interface Factory { | ||
| fun create(): ProducerFragmentComponent | ||
| } | ||
|
|
||
| fun inject(fragment: ProducerFragment) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,17 @@ package ru.otus.daggerhomework | |
| import android.app.Activity | ||
| import android.content.Context | ||
| import android.widget.Toast | ||
| import javax.inject.Inject | ||
|
|
||
| class ProducerViewModel( | ||
| class ProducerViewModel @Inject constructor ( | ||
| private val color: ColorSaver, | ||
| private val colorGenerator: ColorGenerator, | ||
| private val context: Context | ||
| @param:MainActivityContext private val context: Context | ||
|
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 generateColor() { | ||
| if (context !is Activity) throw RuntimeException("Activity context is required") | ||
| color.saveColor(colorGenerator.generateColor()) | ||
| Toast.makeText(context, "Color sent", Toast.LENGTH_LONG).show() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import dagger.Subcomponent | ||
|
|
||
| @ReceiverFragmentScope | ||
| @Subcomponent() | ||
| interface ReceiverFragmentComponent { | ||
|
|
||
| @Subcomponent.Factory | ||
| interface Factory { | ||
| fun create(): ReceiverFragmentComponent | ||
| } | ||
|
|
||
| fun inject(fragment: ReceiverFragment) | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,17 @@ package ru.otus.daggerhomework | |
| import android.app.Application | ||
| import android.content.Context | ||
| import android.widget.Toast | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import javax.inject.Inject | ||
|
|
||
| class ReceiverViewModel( | ||
| class ReceiverViewModel @Inject constructor( | ||
| private val state: ColorState, | ||
| private val context: Context | ||
|
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(): StateFlow<Int> { | ||
| if (context !is Application) throw RuntimeException("Application context is required") | ||
| Toast.makeText(context, "Color received", Toast.LENGTH_LONG).show() | ||
| return state.color | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,26 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <androidx.constraintlayout.widget.ConstraintLayout | ||
| xmlns:android="http://schemas.android.com/apk/res/android" | ||
| <LinearLayout 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" | ||
| android:orientation="vertical" | ||
| 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.fragment.app.FragmentContainerView | ||
| android:id="@+id/fragment_container_2" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="0dp" | ||
| android:layout_weight="10" | ||
| android:name="ru.otus.daggerhomework.ProducerFragment" | ||
| tools:layout="@layout/fragment_producer" /> | ||
|
|
||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||
| <androidx.fragment.app.FragmentContainerView | ||
| android:id="@+id/fragment_container_1" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="0dp" | ||
| android:layout_weight="1" | ||
| android:name="ru.otus.daggerhomework.ReceiverFragment" | ||
| tools:layout="@layout/fragment_receiver" /> | ||
|
|
||
| </LinearLayout> |
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.
Factoryназываютcreate,buildиспользуют вBuilderСontext, а неApp@ApplicationContext, потому что он может бытьActivityContext