Skip to content

Instantly share code, notes, and snippets.

@Rasalexman
Last active January 18, 2020 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rasalexman/7c6a0fe69067a16e8a597bd8d5a07749 to your computer and use it in GitHub Desktop.
Save Rasalexman/7c6a0fe69067a16e8a597bd8d5a07749 to your computer and use it in GitHub Desktop.
ViewModel with LiveData ViewEvent
class MyViewModel(
private val pagesRepository: IPagesRepository
) : ViewModel() {
val eventPageLiveData = MutableLiveData<ViewEvent<Int>>()
val resultLiveData: LiveData<Result<List<MyUIModel>>> by lazy {
eventPageLiveData.switchMap { pageEvent ->
liveData(context = viewModelScope.coroutineContext + Dispatchers.IO) {
emit(pagesRepository.getNextPageData(pageEvent.data))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment