Skip to content

Instantly share code, notes, and snippets.

@ChristopherME
Last active June 25, 2020 03:37
Show Gist options
  • Save ChristopherME/4049fe1e5d95284ec2b0b97e9c3dff0d to your computer and use it in GitHub Desktop.
Save ChristopherME/4049fe1e5d95284ec2b0b97e9c3dff0d to your computer and use it in GitHub Desktop.
class SomeViewModel(
private val getUsersUseCase: GetUserUseCase,
private val mapper: PresentationMapper
) : ViewModel() {
// Only your viewmodel should update the value.
private val _domainUsers: MutableLiveData<List<DomainUser>> = MutableLiveData()
val users: LiveData<List<PresentationUser>> = _domainUsers.switchMap { domainUsers ->
liveData {
emit(mapper.mapDomainUsersToPresentation(domainUsers))
}
}
// You can do what ever you want once you have the value.
val showEmptyView: LiveData<Boolean> = _domainUsers.map { domainUsers -> domainUsers.isNullOrEmpty() }
...
private fun executeUseCase() {
viewModelScope.launch {
_domainUsers.value = getUserUseCase.someComputation()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment