Skip to content

Commit

Permalink
Fix networkResource (for ony request without Room)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Oct 31, 2021
1 parent 747a029 commit 62724b0
Showing 1 changed file with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,25 @@ inline fun <ResultType, RequestType> networkBoundResource(

}

inline fun <ResultType, RequestType> networkBoundResource(
crossinline query: () -> Flow<ResultType>,
inline fun <RequestType> networkResource(
crossinline fetch: suspend () -> RequestType,
crossinline saveFetchResult: suspend (RequestType) -> Unit,
crossinline shouldFetch: () -> Boolean = { true }
) = flow {

emit(Resource.Loading())

val flow = if (shouldFetch()) {
if (shouldFetch()) {

try {
saveFetchResult(fetch())
query().map { Resource.Success(it) }
emit(Resource.Success(fetch()))
} catch (exception: IOException) {
query().map { Resource.Error(Failure.ServerError(errorCode = 0, errorMessage = exception.message.orEmpty())) }
emit(Resource.Error(Failure.NetworkConnection(errorMessage = exception.message.orEmpty())))
} catch (error: HttpException) {
query().map { Resource.Error(Failure.NetworkConnection(errorCode = error.code(), errorMessage = error.message.orEmpty())) }
emit(Resource.Error(Failure.NetworkConnection(errorMessage = error.message.orEmpty())))
} catch (exception: Exception) {
query().map { Resource.Error(Failure.ServerError(errorCode = 0, errorMessage = exception.message.orEmpty())) }
emit(Resource.Error(Failure.NetworkConnection(errorMessage = exception.message.orEmpty())))
}

} else query().map { Resource.Success(it) }

emitAll(flow)
} else emit(Resource.Error(Failure.NetworkConnection(errorMessage = "error. .orEmpty()")))

}

0 comments on commit 62724b0

Please sign in to comment.