Last active
June 17, 2019 22:33
-
-
Save bolaware/ef80de69e370d3bb0e55fac53dfbff37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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