-
Notifications
You must be signed in to change notification settings - Fork 174
Dagger2 - дз Лисица #43
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
Open
lex090
wants to merge
7
commits into
Otus-Android:master
Choose a base branch
from
lex090:hw
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34c756e
Добавил AppComponent. Сделал верстку. Прокинулл applicationContext.
lex090 94c460d
Добавил AppComponent. Сделал верстку. Прокинулл applicationContext.
lex090 f576f28
Добавил AppComponent. Сделал верстку. Прокинулл applicationContext.
lex090 ad9342c
Реализовал структуру саб компонентов.
lex090 cf1278a
Реализовал структуру саб компонентов.
lex090 044ea59
Реализовал структуру саб компонентов.
lex090 31af38c
Доделал дз
lex090 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.app.ApplicationComponent | ||
| import ru.otus.daggerhomework.di.app.DaggerApplicationComponent | ||
|
|
||
| class App : Application() { | ||
|
|
||
| val componentInstance: ApplicationComponent by lazy { | ||
| createApplicationComponent() | ||
| } | ||
|
|
||
| private fun createApplicationComponent(): ApplicationComponent = DaggerApplicationComponent | ||
| .factory() | ||
| .create(context = this) | ||
|
|
||
| class App :Application() { | ||
| } |
4 changes: 0 additions & 4 deletions
4
app/src/main/java/ru/otus/daggerhomework/ApplicationComponent.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,34 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity | ||
| import android.content.Context | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import ru.otus.daggerhomework.di.activity.ActivityComponent | ||
| import ru.otus.daggerhomework.di.qualifiers.AppContext | ||
| import javax.inject.Inject | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| @AppContext | ||
| @Inject | ||
| lateinit var appContext: Context | ||
|
|
||
| val activityComponent: ActivityComponent by lazy { | ||
| createActivityComponent() | ||
| } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| injectDependencies() | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
| } | ||
|
|
||
| private fun injectDependencies() { | ||
| activityComponent.inject(this) | ||
| } | ||
|
|
||
| private fun createActivityComponent(): ActivityComponent = (application as App) | ||
| .componentInstance | ||
| .provideActivityComponentFactory() | ||
| .create(context = this) | ||
| } |
14 changes: 8 additions & 6 deletions
14
app/src/main/java/ru/otus/daggerhomework/ViewModelProducer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,21 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Context | ||
| import android.widget.Toast | ||
| import androidx.fragment.app.FragmentActivity | ||
| import androidx.lifecycle.ViewModel | ||
| import java.lang.RuntimeException | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import ru.otus.daggerhomework.di.qualifiers.ActivityContext | ||
| import javax.inject.Inject | ||
|
|
||
| class ViewModelProducer( | ||
| class ViewModelProducer @Inject constructor( | ||
| private val colorGenerator: ColorGenerator, | ||
| private val context: Context | ||
| ) : ViewModel() { | ||
| @ActivityContext private val context: Context, | ||
| private val stateFlow: MutableStateFlow<Int> | ||
| ) { | ||
|
|
||
| fun generateColor() { | ||
| if (context !is FragmentActivity) throw RuntimeException("Здесь нужен контекст активити") | ||
| Toast.makeText(context, "Color sent", Toast.LENGTH_LONG).show() | ||
| stateFlow.value = colorGenerator.generateColor() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/src/main/java/ru/otus/daggerhomework/di/activity/ActivityComponent.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package ru.otus.daggerhomework.di.activity | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.Subcomponent | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import ru.otus.daggerhomework.MainActivity | ||
| import ru.otus.daggerhomework.di.customscopes.ActivityScope | ||
| import ru.otus.daggerhomework.di.fragment.FragmentProducerComponent | ||
| import ru.otus.daggerhomework.di.fragment.FragmentReceiverComponent | ||
| import ru.otus.daggerhomework.di.qualifiers.ActivityContext | ||
|
|
||
| @ActivityScope | ||
| @Subcomponent(modules = [ActivityComponentModule::class]) | ||
| interface ActivityComponent { | ||
|
|
||
| fun provideFragmentProducerComponentFactory(): FragmentProducerComponent.Factory | ||
|
|
||
| fun provideFragmentReceiverComponentFactory(): FragmentReceiverComponent.Factory | ||
|
|
||
| fun inject(activity: MainActivity) | ||
|
|
||
| @Subcomponent.Factory | ||
| interface Factory { | ||
| fun create( | ||
| @ActivityContext | ||
| @BindsInstance | ||
| context: Context | ||
| ): ActivityComponent | ||
| } | ||
| } | ||
|
|
||
| @Module( | ||
| subcomponents = [ | ||
| FragmentProducerComponent::class, | ||
| FragmentProducerComponent::class | ||
| ] | ||
| ) | ||
| class ActivityComponentModule { | ||
|
|
||
| @ActivityScope | ||
| @Provides | ||
| fun provideObserver(): MutableStateFlow<Int> = MutableStateFlow(0) | ||
| } |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/ru/otus/daggerhomework/di/app/ApplicationComponent.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.otus.daggerhomework.di.app | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import dagger.Module | ||
| import ru.otus.daggerhomework.di.activity.ActivityComponent | ||
| import ru.otus.daggerhomework.di.qualifiers.AppContext | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| @Component(modules = [ApplicationComponentModule::class]) | ||
| interface ApplicationComponent { | ||
|
|
||
| fun provideActivityComponentFactory(): ActivityComponent.Factory | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
|
|
||
| fun create( | ||
| @AppContext | ||
| @BindsInstance | ||
| context: Context | ||
| ): ApplicationComponent | ||
| } | ||
| } | ||
|
|
||
| @Module(subcomponents = [ActivityComponent::class]) | ||
| class ApplicationComponentModule | ||
6 changes: 6 additions & 0 deletions
6
app/src/main/java/ru/otus/daggerhomework/di/customscopes/ActivityScope.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package ru.otus.daggerhomework.di.customscopes | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| annotation class ActivityScope |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/ru/otus/daggerhomework/di/customscopes/AppScope.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package ru.otus.daggerhomework.di.customscopes | ||
|
|
||
| import javax.inject.Scope | ||
|
|
||
| @Scope | ||
| annotation class AppScope |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Имхо не очень хорошая практика модули по именам привязывать к компонентам, у тебя же в принципе модули могут жить в разных компонентах, а конкретно этот вообще необходим только для того чтобы привязать сабкомпоненты, мб его так и назвать?