Skip to content

Commit 70c66b3

Browse files
committed
added mock flows/suspend for prototyping in viewmodels
1 parent ebe6962 commit 70c66b3

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

app/src/main/java/com/monstarlab/features/sample/SampleFragment.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.monstarlab.base.BaseFragment
77
import com.monstarlab.databinding.FragmentSampleBinding
88
import com.monstarlab.extensions.collectFlow
99
import com.monstarlab.extensions.viewBinding
10+
import kotlinx.coroutines.flow.collect
11+
import kotlinx.coroutines.flow.combine
1012

1113
class 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
}
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
package com.monstarlab.features.sample
22

33
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
512
import javax.inject.Inject
613

714
class 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
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

app/src/main/res/layout/fragment_sample.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@
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>

0 commit comments

Comments
 (0)