Skip to content

Instantly share code, notes, and snippets.

@weverb2
Created January 23, 2019 23:04
Show Gist options
  • Save weverb2/d883db61112be78364da97cdd53e0a61 to your computer and use it in GitHub Desktop.
Save weverb2/d883db61112be78364da97cdd53e0a61 to your computer and use it in GitHub Desktop.
class MainViewModel : ViewModel() {
val api = NetworkConfiguration.getPlaceholderApi()
private val mutablePostData = MutableLiveData<List<Post>>()
val postData: LiveData<List<Post>>
get() = mutablePostData
fun fetchPosts() {
api.getPosts().enqueue(object : Callback<List<Post>> {
override fun onResponse(call: Call<List<Post>>, response: Response<List<Post>>) {
if (response.isSuccessful) {
mutablePostData.postValue(response.body())
} else {
// Handle Unsuccessful response
}
}
override fun onFailure(call: Call<List<Post>>, t: Throwable) {
// Handle error
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment