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
77import com.monstarlab.databinding.FragmentSampleBinding
88import com.monstarlab.extensions.collectFlow
99import com.monstarlab.extensions.viewBinding
10+ import kotlinx.coroutines.flow.collect
11+ import kotlinx.coroutines.flow.combine
1012
1113class SampleFragment : BaseFragment (R .layout.fragment_sample) {
1214
@@ -21,8 +23,14 @@ class SampleFragment : BaseFragment(R.layout.fragment_sample) {
2123 binding.valueTextView.text = " Clicked $clicks times"
2224 }
2325
26+ collectFlow(viewModel.textFlow) { newText ->
27+ binding.asyncTextView.text = newText
28+ }
29+
2430 binding.theButton.setOnClickListener {
2531 viewModel.clickedButton()
2632 }
33+
34+ viewModel.fetchString()
2735 }
2836}
Original file line number Diff line number Diff line change 11package com.monstarlab.features.sample
22
33import 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
512import javax.inject.Inject
613
714class SampleViewModel @Inject constructor(
815
916): ViewModel() {
1017
1118 val clickFlow: MutableStateFlow <Int > = MutableStateFlow (0 )
19+ val textFlow: MutableStateFlow <String > = MutableStateFlow (" Nothing yet" )
1220
1321 fun clickedButton () {
1422 clickFlow.value++
1523 }
1624
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+
1742}
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 4040 android : layout_height =" wrap_content" />
4141
4242
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+
4354</androidx .constraintlayout.widget.ConstraintLayout>
You can’t perform that action at this time.
0 commit comments