Created
July 13, 2020 14:28
-
-
Save sberoch/457a4550fa1bf1f2e8c0a057e48c87b5 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, A> performGetOperation(databaseQuery: () -> LiveData<T>, | |
networkCall: suspend () -> Resource<A>, | |
saveCallResult: suspend (A) -> Unit): LiveData<Resource<T>> = | |
liveData(Dispatchers.IO) { | |
emit(Resource.loading()) | |
val source = databaseQuery.invoke().map { Resource.success(it) } | |
emitSource(source) | |
val responseStatus = networkCall.invoke() | |
if (responseStatus.status == SUCCESS) { | |
saveCallResult(responseStatus.data!!) | |
} else if (responseStatus.status == ERROR) { | |
emit(Resource.error(responseStatus.message!!)) | |
emitSource(source) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to move this file to the data layer but it's impossible, because it's pure java, and LiveData can't be imported. Any solution?