-
Notifications
You must be signed in to change notification settings - Fork 174
HW done #102
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
andreiRookie
wants to merge
8
commits into
Otus-Android:master
Choose a base branch
from
andreiRookie:solution
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
HW done #102
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b76dfd8
main tasks done; a problem w bunch of frags
andreiRookie 6a89764
viewModels injection via factory and providersMap
andreiRookie 43e403c
activity context in VM via weakRef property
andreiRookie 8c13301
minor changes
andreiRookie 4cc3f7b
simple VM classes instead of lifecycle-aware VMs
andreiRookie ee6294f
fixes according review
andreiRookie d7633be
major fixes; problem of incorrect VMReceiver work after rotation got …
andreiRookie af6d6ff
add disposeRes() to ViewModelMarkerInterface
andreiRookie 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 was deleted.
Oops, something went wrong.
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
27 changes: 25 additions & 2 deletions
27
app/src/main/java/ru/otus/daggerhomework/FragmentProducer.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,26 +1,49 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| 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 androidx.lifecycle.ViewModelProvider | ||
| import ru.otus.daggerhomework.di.activity.ActivityComponentHolder | ||
| import ru.otus.daggerhomework.di.app.appComponent | ||
| import ru.otus.daggerhomework.di.fragments.FragmentProducerComponent | ||
| import ru.otus.daggerhomework.di.fragments.ViewModelFactory | ||
| import javax.inject.Inject | ||
|
|
||
| class FragmentProducer : Fragment() { | ||
|
|
||
|
|
||
| @Inject | ||
| lateinit var vmFactory: ViewModelFactory | ||
| private val viewModel: ViewModelProducer by lazy { | ||
| ViewModelProvider(this, vmFactory)[ViewModelProducer::class.java] | ||
| } | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
| FragmentProducerComponent | ||
| .getFragComponent( | ||
| ActivityComponentHolder | ||
| .getActivityComponent(context.appComponent, requireActivity()) | ||
| ).inject(this) | ||
| } | ||
|
|
||
| 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) | ||
| view.findViewById<Button>(R.id.button).setOnClickListener { | ||
| //отправить результат через livedata в другой фрагмент | ||
| viewModel.generateColor() | ||
| } | ||
| } | ||
| } |
48 changes: 45 additions & 3 deletions
48
app/src/main/java/ru/otus/daggerhomework/FragmentReceiver.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,31 +1,73 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import android.content.Context | ||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.Button | ||
| import androidx.annotation.ColorInt | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import ru.otus.daggerhomework.di.activity.ActivityComponentHolder | ||
| import ru.otus.daggerhomework.di.app.appComponent | ||
| import ru.otus.daggerhomework.di.fragments.FragmentReceiverComponent | ||
| import ru.otus.daggerhomework.di.fragments.ViewModelFactory | ||
| import javax.inject.Inject | ||
|
|
||
| class FragmentReceiver : Fragment() { | ||
|
|
||
| private lateinit var frame: View | ||
|
|
||
| @Inject | ||
| lateinit var vmFactory: ViewModelFactory | ||
| private val viewModel: ViewModelReceiver by lazy { | ||
| ViewModelProvider(this, vmFactory)[ViewModelReceiver::class.java] | ||
| } | ||
|
|
||
| private var currentColor: Int = 0xFFFFFF | ||
|
|
||
| override fun onAttach(context: Context) { | ||
| super.onAttach(context) | ||
| FragmentReceiverComponent | ||
| .getFragComponent( | ||
| ActivityComponentHolder | ||
| .getActivityComponent(context.appComponent, requireActivity()) | ||
| ).inject(this) | ||
| } | ||
|
|
||
| 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) | ||
| frame = view.findViewById(R.id.frame) | ||
|
|
||
| viewModel.observeColors(::populateColor) | ||
| } | ||
|
|
||
| fun populateColor(@ColorInt color: Int) { | ||
| private fun populateColor(@ColorInt color: Int) { | ||
| currentColor = color | ||
| frame.setBackgroundColor(color) | ||
| } | ||
|
|
||
| override fun onSaveInstanceState(outState: Bundle) { | ||
| outState.putInt(COLOR_KEY, currentColor) | ||
| super.onSaveInstanceState(outState) | ||
| } | ||
|
|
||
| override fun onViewStateRestored(savedInstanceState: Bundle?) { | ||
| super.onViewStateRestored(savedInstanceState) | ||
| if (savedInstanceState != null) { | ||
| populateColor(savedInstanceState.getInt(COLOR_KEY, currentColor)) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val COLOR_KEY = "color" | ||
| } | ||
| } | ||
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,12 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import androidx.annotation.ColorInt | ||
|
|
||
| sealed interface LocalEvent { | ||
| data class ColorData( | ||
| @ColorInt val color: Int | ||
| ): LocalEvent | ||
| data class Error( | ||
| val e: Exception | ||
| ): LocalEvent | ||
| } |
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,12 +1,23 @@ | ||
| package ru.otus.daggerhomework | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import ru.otus.daggerhomework.di.activity.ActivityComponentHolder | ||
| import ru.otus.daggerhomework.di.app.appComponent | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
|
|
||
| // supportFragmentManager.beginTransaction() | ||
| // .add(R.id.fragment_producer_container, FragmentProducer()) | ||
| // .add(R.id.fragment_receiver_container, FragmentReceiver()) | ||
| // .commit() | ||
|
|
||
| ActivityComponentHolder | ||
| .getActivityComponent(application.appComponent, this) | ||
| .inject(this) | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -3,13 +3,36 @@ package ru.otus.daggerhomework | |
| import android.app.Application | ||
| import android.content.Context | ||
| import android.widget.Toast | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import kotlinx.coroutines.channels.Channel | ||
| import kotlinx.coroutines.flow.receiveAsFlow | ||
| import kotlinx.coroutines.launch | ||
| import ru.otus.daggerhomework.di.app.ApplicationContextQualifier | ||
| import javax.inject.Inject | ||
|
|
||
| class ViewModelReceiver( | ||
| class ViewModelReceiver @Inject constructor( | ||
| private val observer: Channel<LocalEvent>, | ||
|
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. Не очень правильно что ты можешь в приемники эмитить события в Channel, сделай здесь ReceiveChannel, а в другой ВМ SendChannel |
||
| @ApplicationContextQualifier | ||
| private val context: Context | ||
| ) { | ||
| ) : ViewModel() { | ||
|
|
||
| fun observeColors() { | ||
| fun observeColors(populate: (color: Int) -> Unit) { | ||
| if (context !is Application) throw RuntimeException("Здесь нужен контекст апликейшена") | ||
| Toast.makeText(context, "Color received", Toast.LENGTH_LONG).show() | ||
|
|
||
| viewModelScope.launch { | ||
| observer.receiveAsFlow().collect { event -> | ||
| when (event) { | ||
| is LocalEvent.ColorData -> { | ||
| populate(event.color) | ||
| } | ||
|
|
||
| is LocalEvent.Error -> { | ||
| Toast.makeText(context, "Error", Toast.LENGTH_LONG).show() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Не думаю что в lazy тем более потокобезопасном есть смысл