From 8f98055e9ec8a40a5d41bad3e02f409119b7abe8 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 27 Dec 2020 10:22:24 -0500 Subject: [PATCH] Refactor response parsing helper function --- .../tachiyomi/data/updater/github/GithubUpdateChecker.kt | 5 +++-- .../kanade/tachiyomi/extension/api/ExtensionGithubApi.kt | 7 +++---- .../java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt | 5 ++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt index 704c9ab7ce..fc8a4b06f0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt @@ -5,7 +5,7 @@ import eu.kanade.tachiyomi.data.updater.UpdateResult import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.await -import eu.kanade.tachiyomi.network.withResponse +import eu.kanade.tachiyomi.network.parseAs import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import uy.kohesive.injekt.injectLazy @@ -27,7 +27,8 @@ class GithubUpdateChecker { networkService.client .newCall(GET("https://api.github.com/repos/$repo/releases/latest")) .await() - .withResponse { + .parseAs() + .let { // Check if latest version is different from current version if (isNewVersion(it.version)) { GithubUpdateResult.NewUpdate(it) diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt index 02a2295a29..ad66da5217 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt @@ -8,7 +8,7 @@ import eu.kanade.tachiyomi.extension.util.ExtensionLoader import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.await -import eu.kanade.tachiyomi.network.withResponse +import eu.kanade.tachiyomi.network.parseAs import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.serialization.json.JsonArray @@ -28,9 +28,8 @@ internal class ExtensionGithubApi { networkService.client .newCall(GET("${REPO_URL_PREFIX}index.min.json")) .await() - .withResponse> { - parseResponse(it) - } + .parseAs() + .let { parseResponse(it) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt index 484ccdac52..f4edf40cc9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt @@ -110,10 +110,9 @@ fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListene return progressClient.newCall(request) } -inline fun Response.withResponse(block: (T) -> R): R { +inline fun Response.parseAs(): T { this.use { val responseBody = it.body?.string().orEmpty() - val response = Injekt.get().decodeFromString(responseBody) - return block(response) + return Injekt.get().decodeFromString(responseBody) } }