Skip to content

Instantly share code, notes, and snippets.

@bolaware
Last active June 17, 2019 22:33
Show Gist options
  • Save bolaware/ef80de69e370d3bb0e55fac53dfbff37 to your computer and use it in GitHub Desktop.
Save bolaware/ef80de69e370d3bb0e55fac53dfbff37 to your computer and use it in GitHub Desktop.
fun <T : Any> makeCall(call: suspend () -> Response<T>): LiveData<Result<T>> {
val result = MutableLiveData<Result<T>>()
GlobalScope.launch(Dispatchers.Main) {
try {
val response = withContext(Dispatchers.IO) { call.invoke() }
if (response.isSuccessful) {
result.postValue(Result(Status.SUCCESS, response.body(), response.message()))
} else {
result.postValue(Result(Status.ERROR, response.body(), response.errorBody()?.string(), response.code()))
}
result.postValue(makeCall(response))
} catch (e: Exception) {
result.postValue(Result(Status.FAILURE, null, e.localizedMessage))
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment