Skip to content

Instantly share code, notes, and snippets.

@lucanicoletti
Last active May 31, 2019 17:22
Show Gist options
  • Save lucanicoletti/d72da121d9f6d748828fc9af8d309391 to your computer and use it in GitHub Desktop.
Save lucanicoletti/d72da121d9f6d748828fc9af8d309391 to your computer and use it in GitHub Desktop.
class MVIJetpackComposeViewModel : ViewModel() {
private val _jetpackComposeViewState = MutableLiveData<MVIJetpackComposeViewState>()
private val useCase = UseCase()
val jetpackComposeViewState: LiveData<MVIJetpackComposeViewState>
get() = _jetpackComposeViewState
init {
loadData()
}
fun loadData() {
// fetch data from wherever you want
useCase.loadData()
.doOnSubscribe {
_jetpackComposeViewState.postValue(MyViewState.Loading)
}
.observe(
{ response ->
_jetpackComposeViewState.postValue(MyViewState.Success(response.title))
},
{ error ->
_jetpackComposeViewState.postValue(
MyViewState.Error(error.reason, ::loadData)
)
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment