-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathActivitiesViewModel.kt
More file actions
45 lines (40 loc) · 1.41 KB
/
ActivitiesViewModel.kt
File metadata and controls
45 lines (40 loc) · 1.41 KB
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
36
37
38
39
40
41
42
43
44
45
package com.flatstack.android.activities
import androidx.lifecycle.*
import androidx.paging.PagedList
import com.flatstack.android.model.entities.Resource
import com.flatstack.android.type.ActivityEvent
import com.flatstack.android.type.ActivityEvent.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
class ActivitiesViewModel(
private val activitiesRepository: ActivitiesRepository
) : ViewModel() {
private val events = MutableLiveData<List<ActivityEvent>>()
init {
events.postValue(
listOf(
RESET_PASSWORD_REQUESTED,
USER_LOGGED_IN,
USER_REGISTERED,
USER_RESET_PASSWORD,
USER_UPDATED
)
)
}
val activities: LiveData<Resource<PagedList<ActivitiesViewHolderModel?>>> =
events.switchMap { events ->
activitiesRepository.getPagedUserActivities(viewModelScope, events)
.flowOn(Dispatchers.IO)
.map { Resource.success(it) }
.onStart { emit(Resource.loading()) }
.catch { error ->
error.message?.let {
emit(Resource.error(it))
}
}
.asLiveData(viewModelScope.coroutineContext)
}
}