Skip to content

Instantly share code, notes, and snippets.

@gastsail
Last active December 21, 2019 16:32
Show Gist options
  • Save gastsail/66a88c085d3d6567a9b231e1d455177a to your computer and use it in GitHub Desktop.
Save gastsail/66a88c085d3d6567a9b231e1d455177a to your computer and use it in GitHub Desktop.
private val viewModel by lazy { ViewModelProviders.of(this, EventsVMFactory(EventsImpl(EventsRepoImpl()))).get(EventsViewModel::class.java) }
...
override fun onCreate() {
viewModel.fetchEventList.observe(this, Observer {
when(it){
is Resource.Loading -> {
// Before try catch in viewmodel we can use emit(Resource.Loading()) to tell the view we started fetching results and this will be triggered
}
is Resource.Success -> {
// we get the list data with it.data
}
is Resource.Failure-> {
//Handle the failure
Toast.makeText(this,"An error has ocurred:${it.throwable.message}",Toast.LENGTH_SHORT).show()
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment