-
Notifications
You must be signed in to change notification settings - Fork 128
/
ReduceViewModel.kt
35 lines (28 loc) · 986 Bytes
/
ReduceViewModel.kt
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
package com.mindorks.kotlinFlow.learn.reduce
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mindorks.kotlinFlow.data.api.ApiHelper
import com.mindorks.kotlinFlow.data.local.DatabaseHelper
import com.mindorks.kotlinFlow.utils.Resource
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.reduce
import kotlinx.coroutines.launch
class ReduceViewModel(
val apiHelper: ApiHelper,
dbHelper: DatabaseHelper
) : ViewModel() {
private val status = MutableLiveData<Resource<String>>()
fun startReduceTask() {
viewModelScope.launch {
status.postValue(Resource.loading(null))
val result = (1..5).asFlow()
.reduce { a, b -> a + b }
status.postValue(Resource.success(result.toString()))
}
}
fun getStatus(): LiveData<Resource<String>> {
return status
}
}