Use data class to parse extensions list

This commit is contained in:
arkon 2021-09-25 14:57:54 -04:00
parent 07771cb5e4
commit f754b081ce
2 changed files with 32 additions and 30 deletions

View File

@ -10,11 +10,7 @@ import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.await import eu.kanade.tachiyomi.network.await
import eu.kanade.tachiyomi.network.parseAs import eu.kanade.tachiyomi.network.parseAs
import eu.kanade.tachiyomi.util.lang.withIOContext import eu.kanade.tachiyomi.util.lang.withIOContext
import kotlinx.serialization.json.JsonArray import kotlinx.serialization.Serializable
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.long
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.util.Date import java.util.Date
@ -28,8 +24,8 @@ internal class ExtensionGithubApi {
networkService.client networkService.client
.newCall(GET("${REPO_URL_PREFIX}index.min.json")) .newCall(GET("${REPO_URL_PREFIX}index.min.json"))
.await() .await()
.parseAs<JsonArray>() .parseAs<List<ExtensionJsonObject>>()
.let { parseResponse(it) } .toExtensions()
} }
} }
@ -56,24 +52,23 @@ internal class ExtensionGithubApi {
return extensionsWithUpdate return extensionsWithUpdate
} }
private fun parseResponse(json: JsonArray): List<Extension.Available> { private fun List<ExtensionJsonObject>.toExtensions(): List<Extension.Available> {
return json return this
.filter { element -> .filter {
val versionName = element.jsonObject["version"]!!.jsonPrimitive.content val libVersion = it.version.substringBeforeLast('.').toDouble()
val libVersion = versionName.substringBeforeLast('.').toDouble()
libVersion >= ExtensionLoader.LIB_VERSION_MIN && libVersion <= ExtensionLoader.LIB_VERSION_MAX libVersion >= ExtensionLoader.LIB_VERSION_MIN && libVersion <= ExtensionLoader.LIB_VERSION_MAX
} }
.map { element -> .map {
val name = element.jsonObject["name"]!!.jsonPrimitive.content.substringAfter("Tachiyomi: ") Extension.Available(
val pkgName = element.jsonObject["pkg"]!!.jsonPrimitive.content name = it.name.substringAfter("Tachiyomi: "),
val apkName = element.jsonObject["apk"]!!.jsonPrimitive.content pkgName = it.pkg,
val versionName = element.jsonObject["version"]!!.jsonPrimitive.content versionName = it.version,
val versionCode = element.jsonObject["code"]!!.jsonPrimitive.long versionCode = it.code,
val lang = element.jsonObject["lang"]!!.jsonPrimitive.content lang = it.lang,
val nsfw = element.jsonObject["nsfw"]!!.jsonPrimitive.int == 1 isNsfw = it.nsfw == 1,
val icon = "${REPO_URL_PREFIX}icon/${apkName.replace(".apk", ".png")}" apkName = it.apk,
iconUrl = "${REPO_URL_PREFIX}icon/${it.apk.replace(".apk", ".png")}"
Extension.Available(name, pkgName, versionName, versionCode, lang, nsfw, apkName, icon) )
} }
} }
@ -83,3 +78,14 @@ internal class ExtensionGithubApi {
} }
private const val REPO_URL_PREFIX = "https://raw.githubusercontent.com/tachiyomiorg/tachiyomi-extensions/repo/" 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,
)

View File

@ -14,7 +14,7 @@ import rx.Observable
import rx.Producer import rx.Producer
import rx.Subscription import rx.Subscription
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.fullType import uy.kohesive.injekt.api.get
import java.io.IOException import java.io.IOException
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.resumeWithException import kotlin.coroutines.resumeWithException
@ -116,10 +116,6 @@ fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListene
} }
inline fun <reified T> Response.parseAs(): T { inline fun <reified T> Response.parseAs(): T {
// Avoiding Injekt.get<Json>() due to compiler issues val responseBody = this.body?.string().orEmpty()
val json = Injekt.getInstance<Json>(fullType<Json>().type) return Injekt.get<Json>().decodeFromString(responseBody)
this.use {
val responseBody = it.body?.string().orEmpty()
return json.decodeFromString(responseBody)
}
} }