-
Notifications
You must be signed in to change notification settings - Fork 174
ДЗ Dagger 2 #160
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?
ДЗ Dagger 2 #160
Changes from 4 commits
02761f1
e0b56c1
9131d55
31e64d8
2fb4ac5
9b1587c
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,5 +1,11 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Application | ||
| import ru.otus.daggerhomework.di.ApplicationComponent | ||
| import ru.otus.daggerhomework.di.DaggerApplicationComponent | ||
|
|
||
| class App :Application() | ||
| class App : Application() { | ||
| val component = DaggerApplicationComponent.factory().build(this) | ||
| } | ||
|
|
||
| val Application.component: ApplicationComponent get() = (this as App).component |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import java.util.UUID | ||
|
|
||
| sealed class Event { | ||
| data class PopulateColor( | ||
| val color: Int | ||
| ) : Event() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,19 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.fragment.app.FragmentActivity | ||
| import ru.otus.daggerhomework.di.DaggerMainActivityComponent | ||
| import ru.otus.daggerhomework.di.MainActivityComponent | ||
|
|
||
| class MainActivity : FragmentActivity() { | ||
|
|
||
| lateinit var component: MainActivityComponent | ||
|
|
||
| class MainActivity : ComponentActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| component = DaggerMainActivityComponent.factory().build(this) | ||
| setContentView(R.layout.activity_main) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val FragmentActivity.component: MainActivityComponent get() = (this as MainActivity).component |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention(value = AnnotationRetention.RUNTIME) | ||
| annotation class ActivityScope() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| @Component | ||
| interface ApplicationComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build(@BindsInstance context: Context): ApplicationComponent | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| @Retention(value = AnnotationRetention.RUNTIME) | ||
| annotation class FragmentScope() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import ru.otus.daggerhomework.Event | ||
| import javax.inject.Named | ||
|
|
||
| @ActivityScope | ||
| @Component(modules = [MainActivityModule::class]) | ||
| interface MainActivityComponent { | ||
| @Component.Factory | ||
| interface Factory { | ||
| fun build(@BindsInstance activity: Activity): MainActivityComponent | ||
|
||
| } | ||
|
|
||
| fun event(): MutableStateFlow<Event> | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import dagger.Module | ||
| import dagger.Provides | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import ru.otus.daggerhomework.Event | ||
|
|
||
| @Module | ||
| class MainActivityModule { | ||
| @Provides | ||
| @ActivityScope | ||
| fun events(): MutableStateFlow<Event?> = MutableStateFlow(null) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import ru.otus.daggerhomework.ProducerViewModel | ||
|
|
||
| @Component( | ||
| dependencies = [MainActivityComponent::class], | ||
| modules = [ProducerModule::class] | ||
| ) | ||
| @FragmentScope | ||
| interface ProducerFragmentComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build( | ||
| mainActivityComponent: MainActivityComponent, | ||
| @BindsInstance context: Context | ||
|
||
| ): ProducerFragmentComponent | ||
| } | ||
|
|
||
| fun viewModel(): ProducerViewModel | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Context | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import ru.otus.daggerhomework.ColorGenerator | ||
| import ru.otus.daggerhomework.ColorGeneratorImpl | ||
| import ru.otus.daggerhomework.Event | ||
| import ru.otus.daggerhomework.ProducerViewModel | ||
| import javax.inject.Named | ||
|
|
||
|
|
||
| @Module | ||
| class ProducerModule { | ||
|
|
||
| @Provides | ||
| @FragmentScope | ||
| fun colorGenerator(): ColorGenerator = ColorGeneratorImpl() | ||
|
|
||
| @Provides | ||
| @FragmentScope | ||
| fun viewModel( | ||
| colorGenerator: ColorGenerator, | ||
| context: Context, | ||
| event: MutableStateFlow<Event?> | ||
| ): ProducerViewModel = ProducerViewModel(colorGenerator, context, event) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import ru.otus.daggerhomework.ReceiverViewModel | ||
|
|
||
| @Component( | ||
| dependencies = [MainActivityComponent::class], | ||
| modules = [ReceiverModule::class] | ||
| ) | ||
| @FragmentScope | ||
| interface ReceiverFragmentComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun build( | ||
| mainActivityComponent: MainActivityComponent, | ||
| @BindsInstance context: Context | ||
|
||
| ): ReceiverFragmentComponent | ||
| } | ||
|
|
||
| fun viewModel(): ReceiverViewModel | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package ru.otus.daggerhomework.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import ru.otus.daggerhomework.Event | ||
| import ru.otus.daggerhomework.ReceiverViewModel | ||
|
|
||
| @Module | ||
| class ReceiverModule { | ||
|
|
||
| @Provides | ||
| @FragmentScope | ||
| fun viewModel( | ||
| context: Context, | ||
| event: MutableStateFlow<Event?> | ||
| ): ReceiverViewModel = ReceiverViewModel(context, event) | ||
| } |
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.
Кажется лишний импорт тут