From e9f474a1566eb4ceb2cf9bddb5fbb102c4682f35 Mon Sep 17 00:00:00 2001 From: Ztiany Date: Mon, 18 Nov 2019 21:01:58 +0800 Subject: [PATCH] lib-network + coroutine --- lib_network/build.gradle | 3 +- .../com/android/sdk/net/coroutines/ApiCall.kt | 72 +++++++++++++++++++ .../net/errorhandler/ErrorMessageFactory.java | 2 +- 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 lib_network/src/main/java/com/android/sdk/net/coroutines/ApiCall.kt diff --git a/lib_network/build.gradle b/lib_network/build.gradle index 02eda63..1058196 100644 --- a/lib_network/build.gradle +++ b/lib_network/build.gradle @@ -46,5 +46,6 @@ dependencies { implementation thirdLibraries.gson implementation thirdLibraries.rxJava implementation thirdLibraries.retrofitRxJava2CallAdapter - compileOnly kotlinLibraries.kotlinStdlib + implementation kotlinLibraries.kotlinStdlib + implementation kotlinLibraries.kotlinCoroutines } diff --git a/lib_network/src/main/java/com/android/sdk/net/coroutines/ApiCall.kt b/lib_network/src/main/java/com/android/sdk/net/coroutines/ApiCall.kt new file mode 100644 index 0000000..8e1a263 --- /dev/null +++ b/lib_network/src/main/java/com/android/sdk/net/coroutines/ApiCall.kt @@ -0,0 +1,72 @@ +package com.android.sdk.net.coroutines + +import kotlinx.coroutines.delay + + +suspend fun apiCall(call: suspend () -> com.android.sdk.net.core.Result): Result { + val retryPostAction = retryPostAction() + + val result = realCall(call) + + if (result is Result.Error && retryPostAction.invoke(result.exception)) { + return realCall(call) + } + return result +} + +private suspend fun realCall(call: suspend () -> com.android.sdk.net.core.Result): Result { + return try { + val networkResult = call.invoke() + handleResult(networkResult) + } catch (e: Throwable) { + Result.Error(RuntimeException()) + } +} + +fun handleResult(result: com.android.sdk.net.core.Result): Result { + return Result.Error(RuntimeException()) +} + +/* +suspend fun apiCallChecker(call: suspend () -> Result): kotlin.Result<> { + try { + val result = call.invoke() + checkResult() + } catch (e: Throwable) { + + } +} +*/ + +private suspend fun retryRequest( + times: Int = 2, + delay: Long = 100, + block: suspend () -> T, + retry: suspend (Throwable) -> Unit): T { + repeat(times - 1) { + try { + return block() + } catch (throwable: Throwable) { + retry(throwable) + } + delay(delay) + } + return block() // last attempt +} + +private fun retryPostAction(): (suspend (Throwable) -> Boolean) { + return { false } +} + +sealed class Result { + + data class Success(val data: T) : Result() + data class Error(val exception: Exception) : Result() + + override fun toString(): String { + return when (this) { + is Success<*> -> "Success[data=$data]" + is Error -> "Error[exception=$exception]" + } + } +} diff --git a/lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java b/lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java index 00a77b4..87f214e 100644 --- a/lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java +++ b/lib_network/src/main/java/com/android/sdk/net/errorhandler/ErrorMessageFactory.java @@ -22,7 +22,7 @@ public class ErrorMessageFactory { public static CharSequence createMessage(Throwable exception) { ErrorMessage mErrorMessage = NetContext.get().netProvider().errorMessage(); - Timber.d("createMessage with:" + exception); + Timber.d("createMessage with:%s", exception.toString()); CharSequence message = null; //SocketTimeoutException android NetworkErrorException extends IOException