From f754b081ce45b9c418986778e24b6a3265ba7735 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 25 Sep 2021 14:57:54 -0400 Subject: [PATCH] Use data class to parse extensions list --- .../extension/api/ExtensionGithubApi.kt | 52 +++++++++++-------- .../tachiyomi/network/OkHttpExtensions.kt | 10 ++-- 2 files changed, 32 insertions(+), 30 deletions(-) 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 e0eddbd7c..d712a564d 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 @@ -10,11 +10,7 @@ import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.await import eu.kanade.tachiyomi.network.parseAs import eu.kanade.tachiyomi.util.lang.withIOContext -import kotlinx.serialization.json.JsonArray -import kotlinx.serialization.json.int -import kotlinx.serialization.json.jsonObject -import kotlinx.serialization.json.jsonPrimitive -import kotlinx.serialization.json.long +import kotlinx.serialization.Serializable import uy.kohesive.injekt.injectLazy import java.util.Date @@ -28,8 +24,8 @@ internal class ExtensionGithubApi { networkService.client .newCall(GET("${REPO_URL_PREFIX}index.min.json")) .await() - .parseAs() - .let { parseResponse(it) } + .parseAs>() + .toExtensions() } } @@ -56,24 +52,23 @@ internal class ExtensionGithubApi { return extensionsWithUpdate } - private fun parseResponse(json: JsonArray): List { - return json - .filter { element -> - val versionName = element.jsonObject["version"]!!.jsonPrimitive.content - val libVersion = versionName.substringBeforeLast('.').toDouble() + private fun List.toExtensions(): List { + return this + .filter { + val libVersion = it.version.substringBeforeLast('.').toDouble() libVersion >= ExtensionLoader.LIB_VERSION_MIN && libVersion <= ExtensionLoader.LIB_VERSION_MAX } - .map { element -> - val name = element.jsonObject["name"]!!.jsonPrimitive.content.substringAfter("Tachiyomi: ") - val pkgName = element.jsonObject["pkg"]!!.jsonPrimitive.content - val apkName = element.jsonObject["apk"]!!.jsonPrimitive.content - val versionName = element.jsonObject["version"]!!.jsonPrimitive.content - val versionCode = element.jsonObject["code"]!!.jsonPrimitive.long - val lang = element.jsonObject["lang"]!!.jsonPrimitive.content - val nsfw = element.jsonObject["nsfw"]!!.jsonPrimitive.int == 1 - val icon = "${REPO_URL_PREFIX}icon/${apkName.replace(".apk", ".png")}" - - Extension.Available(name, pkgName, versionName, versionCode, lang, nsfw, apkName, icon) + .map { + Extension.Available( + name = it.name.substringAfter("Tachiyomi: "), + pkgName = it.pkg, + versionName = it.version, + versionCode = it.code, + lang = it.lang, + isNsfw = it.nsfw == 1, + apkName = it.apk, + iconUrl = "${REPO_URL_PREFIX}icon/${it.apk.replace(".apk", ".png")}" + ) } } @@ -83,3 +78,14 @@ internal class ExtensionGithubApi { } private const val REPO_URL_PREFIX = "https://raw.githubusercontent.com/tachiyomiorg/tachiyomi-extensions/repo/" + +@Serializable +private data class ExtensionJsonObject( + val name: String, + val pkg: String, + val apk: String, + val version: String, + val code: Long, + val lang: String, + val nsfw: Int, +) 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 71865f0ea..d78a0ee3c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt @@ -14,7 +14,7 @@ import rx.Observable import rx.Producer import rx.Subscription import uy.kohesive.injekt.Injekt -import uy.kohesive.injekt.api.fullType +import uy.kohesive.injekt.api.get import java.io.IOException import java.util.concurrent.atomic.AtomicBoolean import kotlin.coroutines.resumeWithException @@ -116,10 +116,6 @@ fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListene } inline fun Response.parseAs(): T { - // Avoiding Injekt.get() due to compiler issues - val json = Injekt.getInstance(fullType().type) - this.use { - val responseBody = it.body?.string().orEmpty() - return json.decodeFromString(responseBody) - } + val responseBody = this.body?.string().orEmpty() + return Injekt.get().decodeFromString(responseBody) }