-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathFragmentReceiver.kt
More file actions
58 lines (46 loc) · 1.53 KB
/
Copy pathFragmentReceiver.kt
File metadata and controls
58 lines (46 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ru.otus.daggerhomework
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.lifecycle.lifecycleScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject
class FragmentReceiver : Fragment() {
@Inject
lateinit var viewModelReceiver: ViewModelReceiver
private lateinit var frame: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
injectDependencies()
}
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)
viewLifecycleOwner.lifecycleScope.launch {
viewModelReceiver
.colorsFlow
.collect(this@FragmentReceiver::populateColor)
}
}
private fun populateColor(@ColorInt color: Int) {
frame.setBackgroundColor(color)
}
private fun injectDependencies() {
val component = (activity as MainActivity)
.activityComponent
.provideFragmentReceiverComponentFactory()
.create()
component.inject(this)
}
}