Skip to content

Commit 2893c78

Browse files
committed
2. Оставшиеся два задания
1 parent 3e30a34 commit 2893c78

21 files changed

+215
-59
lines changed

app/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@ android {
3333
}
3434

3535
dependencies {
36+
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
37+
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.0"
38+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
39+
implementation 'androidx.activity:activity-ktx:1.2.3'
40+
3641
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
3742
implementation 'androidx.core:core-ktx:1.6.0'
3843
implementation 'androidx.appcompat:appcompat:1.3.1'
3944
implementation 'com.google.android.material:material:1.4.0'
4045
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
46+
47+
implementation "com.google.dagger:dagger-android:2.42"
48+
implementation "com.google.dagger:dagger-android-support:2.42"
49+
kapt "com.google.dagger:dagger-android-processor:2.42"
50+
4151
implementation 'com.google.dagger:dagger:2.42'
4252
kapt 'com.google.dagger:dagger-compiler:2.42'
4353
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package="ru.otus.daggerhomework">
55

66
<application
7+
android:name=".App"
78
android:allowBackup="true"
89
android:icon="@mipmap/ic_launcher"
910
android:label="@string/app_name"

app/src/main/java/ru/otus/daggerhomework/ApplicationComponent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import android.content.Context
44
import dagger.BindsInstance
55
import dagger.Component
66
import javax.inject.Qualifier
7+
import javax.inject.Singleton
78

89
@Component
10+
@Singleton
911
interface ApplicationComponent {
1012

1113
@ApplicationContext

app/src/main/java/ru/otus/daggerhomework/ColorGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package ru.otus.daggerhomework
22

33
import android.graphics.Color
44
import androidx.annotation.ColorInt
5-
import androidx.annotation.ColorRes
65
import java.util.*
6+
import javax.inject.Inject
77

88
interface ColorGenerator {
99

1010
@ColorInt
1111
fun generateColor(): Int
1212
}
1313

14-
class ColorGeneratorImpl : ColorGenerator {
14+
class ColorGeneratorImpl @Inject constructor(): ColorGenerator {
1515

1616
override fun generateColor(): Int {
1717
val rnd = Random()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.otus.daggerhomework
2+
3+
import kotlinx.coroutines.flow.Flow
4+
5+
interface EventsReader {
6+
val eventsFlow: Flow<Int>
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ru.otus.daggerhomework
2+
3+
interface EventsWriter {
4+
fun writeEvent(color: Int)
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ru.otus.daggerhomework
2+
3+
import kotlinx.coroutines.flow.Flow
4+
import kotlinx.coroutines.flow.MutableSharedFlow
5+
import kotlinx.coroutines.flow.asSharedFlow
6+
import javax.inject.Inject
7+
8+
class EventsWriterReader @Inject constructor() : EventsReader, EventsWriter {
9+
private val _eventsFlow = MutableSharedFlow<Int>(replay = 1)
10+
11+
override val eventsFlow: Flow<Int>
12+
get() = _eventsFlow.asSharedFlow()
13+
14+
override fun writeEvent(color: Int) {
15+
_eventsFlow.tryEmit(color)
16+
}
17+
}

app/src/main/java/ru/otus/daggerhomework/FragmentProducer.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,30 @@ import android.view.View
66
import android.view.ViewGroup
77
import android.widget.Button
88
import androidx.fragment.app.Fragment
9+
import androidx.lifecycle.ViewModelProvider
10+
import javax.inject.Inject
911

1012
class FragmentProducer : Fragment() {
13+
@Inject
14+
lateinit var viewModelFactory: ViewModelFactory
1115

16+
private val viewModel: ViewModelProducer by lazy {
17+
ViewModelProvider(this, viewModelFactory)[ViewModelProducer::class.java]
18+
}
1219
override fun onCreateView(
1320
inflater: LayoutInflater,
1421
container: ViewGroup?,
1522
savedInstanceState: Bundle?
1623
): View? {
17-
return inflater.inflate(R.layout.fragment_a, container, true)
24+
return inflater.inflate(R.layout.fragment_a, container, false)
1825
}
1926

2027
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
2128
super.onViewCreated(view, savedInstanceState)
29+
FragmentProducerComponent.getFragmentProducerComponent(
30+
(requireActivity() as MainActivity).mainActivityComponent).inject(this)
2231
view.findViewById<Button>(R.id.button).setOnClickListener {
23-
//отправить результат через livedata в другой фрагмент
32+
viewModel.generateColor()
2433
}
2534
}
2635
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package ru.otus.daggerhomework
22

33
import dagger.Component
4-
import javax.inject.Singleton
4+
import javax.inject.Scope
55

6-
@Singleton
6+
@FragmentScope
77
@Component(
8-
modules = [FragmentProducerModule::class],
9-
dependencies = [ApplicationComponent::class]
8+
dependencies = [MainActivityComponent::class]
109
)
1110
interface FragmentProducerComponent {
11+
fun inject(fragmentProducer: FragmentProducer)
1212

1313
companion object {
14-
fun getFragmentProducerComponent(applicationComponent: ApplicationComponent): FragmentProducerComponent {
14+
fun getFragmentProducerComponent(mainActivityComponent: MainActivityComponent): FragmentProducerComponent {
1515
return DaggerFragmentProducerComponent.builder()
16-
.applicationComponent(applicationComponent).build()
16+
.mainActivityComponent(mainActivityComponent).build()
1717
}
1818
}
1919
}
20+
21+
@Scope
22+
@Retention(AnnotationRetention.RUNTIME)
23+
annotation class FragmentScope

app/src/main/java/ru/otus/daggerhomework/FragmentProducerModule.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)