File tree Expand file tree Collapse file tree 5 files changed +78
-1
lines changed Expand file tree Collapse file tree 5 files changed +78
-1
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ import com.monstarlab.base.BaseFragment
7
7
import com.monstarlab.databinding.FragmentSampleBinding
8
8
import com.monstarlab.extensions.collectFlow
9
9
import com.monstarlab.extensions.viewBinding
10
+ import kotlinx.coroutines.flow.collect
11
+ import kotlinx.coroutines.flow.combine
10
12
11
13
class SampleFragment : BaseFragment (R .layout.fragment_sample) {
12
14
@@ -21,8 +23,14 @@ class SampleFragment : BaseFragment(R.layout.fragment_sample) {
21
23
binding.valueTextView.text = " Clicked $clicks times"
22
24
}
23
25
26
+ collectFlow(viewModel.textFlow) { newText ->
27
+ binding.asyncTextView.text = newText
28
+ }
29
+
24
30
binding.theButton.setOnClickListener {
25
31
viewModel.clickedButton()
26
32
}
33
+
34
+ viewModel.fetchString()
27
35
}
28
36
}
Original file line number Diff line number Diff line change 1
1
package com.monstarlab.features.sample
2
2
3
3
import androidx.lifecycle.ViewModel
4
- import kotlinx.coroutines.flow.MutableStateFlow
4
+ import androidx.lifecycle.viewModelScope
5
+ import com.monstarlab.extensions.combineFlows
6
+ import com.monstarlab.mock.MockFlows
7
+ import com.monstarlab.mock.MockSuspends
8
+ import kotlinx.coroutines.Dispatchers
9
+ import kotlinx.coroutines.flow.*
10
+ import kotlinx.coroutines.launch
11
+ import kotlinx.coroutines.withContext
5
12
import javax.inject.Inject
6
13
7
14
class SampleViewModel @Inject constructor(
8
15
9
16
): ViewModel() {
10
17
11
18
val clickFlow: MutableStateFlow <Int > = MutableStateFlow (0 )
19
+ val textFlow: MutableStateFlow <String > = MutableStateFlow (" Nothing yet" )
12
20
13
21
fun clickedButton () {
14
22
clickFlow.value++
15
23
}
16
24
25
+ fun fetchString () {
26
+ viewModelScope.combineFlows(MockFlows .mockString(), MockFlows .mockFlag()) { text, flag ->
27
+ textFlow.value = " text: $text - flag: $flag "
28
+ }
29
+
30
+
31
+ MockFlows .mockString()
32
+ .onEach { result -> textFlow.value = result }
33
+ .catch { _ -> /* Do something with the exception */ }
34
+ .launchIn(viewModelScope)
35
+
36
+ viewModelScope.launch {
37
+ val result = withContext(Dispatchers .IO ) { MockSuspends .fetchString() }
38
+ textFlow.value = result
39
+ }
40
+ }
41
+
17
42
}
Original file line number Diff line number Diff line change
1
+ package com.monstarlab.mock
2
+
3
+ import kotlinx.coroutines.delay
4
+ import kotlinx.coroutines.flow.Flow
5
+ import kotlinx.coroutines.flow.flow
6
+
7
+ object MockFlows {
8
+
9
+ fun mockString (): Flow <String > = flow {
10
+ delay(2000 )
11
+ emit(" Hello world from Flow" )
12
+ }
13
+
14
+ fun mockFlag (): Flow <Boolean > = flow {
15
+ delay(1000 )
16
+ emit(false )
17
+ delay(2000 )
18
+ emit(true )
19
+ }
20
+
21
+ }
Original file line number Diff line number Diff line change
1
+ package com.monstarlab.mock
2
+
3
+ import kotlinx.coroutines.delay
4
+
5
+ object MockSuspends {
6
+
7
+ suspend fun fetchString (): String {
8
+ delay(5000 )
9
+ return " Hello world from suspend"
10
+ }
11
+
12
+ }
Original file line number Diff line number Diff line change 40
40
android : layout_height =" wrap_content" />
41
41
42
42
43
+ <TextView
44
+ android : id =" @+id/asyncTextView"
45
+ app : layout_constraintTop_toBottomOf =" @id/theButton"
46
+ app : layout_constraintStart_toStartOf =" parent"
47
+ app : layout_constraintEnd_toEndOf =" parent"
48
+ android : layout_marginTop =" 48dp"
49
+ android : gravity =" center"
50
+ android : layout_width =" wrap_content"
51
+ android : layout_height =" wrap_content" />
52
+
53
+
43
54
</androidx .constraintlayout.widget.ConstraintLayout>
You can’t perform that action at this time.
0 commit comments