-
Notifications
You must be signed in to change notification settings - Fork 174
init commit #42
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?
init commit #42
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package otus.homework.dagger | ||
|
|
||
| import android.app.Application | ||
| import otus.homework.dagger.di.* | ||
|
|
||
| class App : Application() { | ||
|
|
||
| lateinit var appComponent: AppComponent | ||
|
|
||
| override fun onCreate() { | ||
| super.onCreate() | ||
| appComponent = DaggerAppComponent | ||
| .factory() | ||
| .create(applicationContext) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package otus.homework.dagger | ||
|
|
||
| import android.content.Context | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.annotation.ColorInt | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.viewModels | ||
| import otus.homework.dagger.di.DaggerFragmentReceiverComponent | ||
| import javax.inject.Inject | ||
|
|
||
| class FragmentReceiver : Fragment() { | ||
|
|
||
| private lateinit var frame: View | ||
|
|
||
| @Inject | ||
| lateinit var viewModelFactory: VMFactoryReceiver | ||
| private val viewModel: ViewModelReceiver by viewModels { viewModelFactory } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View? { | ||
| return inflater.inflate(R.layout.fragment_b, container, true) | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| frame = view.findViewById(R.id.frame) | ||
| viewModel.observer.observe(viewLifecycleOwner) { | ||
| populateColor(it) | ||
| viewModel.observeColors() | ||
| } | ||
| } | ||
|
|
||
| private fun populateColor(@ColorInt color: Int) { | ||
| frame.setBackgroundColor(color) | ||
| } | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
| DaggerFragmentReceiverComponent | ||
| .factory() | ||
| .create( | ||
| (requireActivity().applicationContext as App).appComponent, | ||
| (requireActivity() as MainActivity).activityComponent | ||
| ) | ||
| .inject(this) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package otus.homework.dagger | ||
|
|
||
| import android.content.Context | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import otus.homework.dagger.di.ActivityScope | ||
| import otus.homework.dagger.di.AppScope | ||
| import javax.inject.Inject | ||
|
|
||
| class VMFactoryProducer @Inject constructor( | ||
| private val colorGenerator: ColorGenerator, | ||
| @ActivityScope private val context: Context, | ||
| private val observer: MutableLiveData<Int> | ||
| ) : ViewModelProvider.Factory { | ||
|
|
||
| override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
| return if (modelClass.isAssignableFrom(ViewModelProducer::class.java)) { | ||
| ViewModelProducer(context = context, colorGenerator = colorGenerator, observer = observer) as T | ||
| } else { | ||
| throw IllegalArgumentException("ViewModel Not Found") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class VMFactoryReceiver @Inject constructor( | ||
| @AppScope private val context: Context, | ||
| private val observer: MutableLiveData<Int> | ||
| ) : ViewModelProvider.Factory { | ||
|
|
||
| override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
| return if (modelClass.isAssignableFrom(ViewModelReceiver::class.java)) { | ||
| ViewModelReceiver(context, observer = observer) as T | ||
| } else { | ||
| throw IllegalArgumentException("ViewModel Not Found") | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package otus.homework.dagger | ||
|
|
||
| import android.content.Context | ||
| import android.widget.Toast | ||
| import androidx.fragment.app.FragmentActivity | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import java.lang.RuntimeException | ||
| import javax.inject.Inject | ||
|
|
||
| class ViewModelProducer @Inject constructor ( | ||
| private val colorGenerator: ColorGenerator?, | ||
|
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 context: Context, | ||
| private val observer: MutableLiveData<Int> | ||
| ) : ViewModel() { | ||
|
|
||
| fun generateColor() { | ||
| if (context !is FragmentActivity) throw RuntimeException("Здесь нужен контекст активити") | ||
| observer.value = colorGenerator?.generateColor() | ||
| Toast.makeText(context, "Color sent", Toast.LENGTH_LONG).show() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| package ru.otus.daggerhomework | ||
| package otus.homework.dagger | ||
|
|
||
| import android.app.Application | ||
| import android.content.Context | ||
| import android.widget.Toast | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import javax.inject.Inject | ||
|
|
||
| class ViewModelReceiver( | ||
| private val context: Context | ||
| ) { | ||
| class ViewModelReceiver @Inject constructor( | ||
|
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 аннотация не нужна, ты же через фабрики создаешь |
||
| private val context: Context, | ||
| val observer: MutableLiveData<Int> | ||
| ) : ViewModel() { | ||
|
|
||
| fun observeColors() { | ||
| if (context !is Application) throw RuntimeException("Здесь нужен контекст апликейшена") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package otus.homework.dagger.di | ||
|
|
||
| import android.content.Context | ||
| import androidx.lifecycle.MutableLiveData | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import javax.inject.Scope | ||
|
|
||
| @ActivityScope | ||
| @Component(modules = [MainActivityModule::class]) | ||
| interface ActivityComponent { | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun create(@BindsInstance context: Context): ActivityComponent | ||
| } | ||
|
|
||
| fun activityContext(): Context | ||
|
|
||
| val observer: MutableLiveData<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. Правильнее делать именно методы
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. Тут есть проблема, у тебя сейчас в обоих фрагментах торчит MutableLiveData, то есть ты можешь заэмитить что-то в MutableLiveData даже из RecieverViewModel, который предполагается умеет только читать. Попробуй прокинуть туда экземпляр LiveData, а в ProducerViewModel прокинь MutableLiveData |
||
| } | ||
|
|
||
| @Module | ||
| class MainActivityModule { | ||
|
|
||
| @Provides | ||
| @ActivityScope | ||
| fun provideColorEvent(): MutableLiveData<Int> { | ||
| return MutableLiveData<Int>() | ||
| } | ||
| } | ||
|
|
||
| @Module | ||
| class ActivityModule { | ||
| @get: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. Этот модуль можно убрать. Добавь инжект аннотацию над ColorFlow |
||
| @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. Скоуп здесь не нужен |
||
| var observer = MutableLiveData<Int>() | ||
| } | ||
|
|
||
| @Scope | ||
| annotation class ActivityScope | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package otus.homework.dagger.di | ||
|
|
||
| import android.content.Context | ||
| import dagger.BindsInstance | ||
| import dagger.Component | ||
| import javax.inject.Qualifier | ||
|
|
||
| @AppScope | ||
| @Component | ||
| interface AppComponent { | ||
| @AppScope | ||
| fun applicationContext(): Context | ||
|
|
||
| @Component.Factory | ||
| interface Factory { | ||
| fun create(@AppScope @BindsInstance appContext: Context): AppComponent | ||
| } | ||
| } | ||
|
|
||
| @Qualifier | ||
| annotation class AppScope | ||
|
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. Мне кажется неправильно Qualifier аннотацию называть постфиксом Scope, это запутать может |
||
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.
Аннотация не нужна