From 7a476abb53a4d3a36cf4f7618af83d984a78536b Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 11 Oct 2020 14:53:02 -0400 Subject: [PATCH] Migrate to kotlinx.serialization for extensions and update fetching --- app/build.gradle | 3 +++ .../data/updater/github/GithubRelease.kt | 13 ++++++----- .../data/updater/github/GithubService.kt | 6 +++-- .../extension/api/ExtensionGithubApi.kt | 22 ++++++++++--------- .../extension/api/ExtensionGithubService.kt | 8 ++++--- build.gradle.kts | 3 ++- 6 files changed, 34 insertions(+), 21 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index aa6281f4f..ae952f4e8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,6 +7,7 @@ apply plugin: 'com.mikepenz.aboutlibraries.plugin' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' +apply plugin: 'kotlinx-serialization' apply plugin: 'com.github.zellius.shortcut-helper' shortcutHelper.filePath = './shortcuts.xml' @@ -176,10 +177,12 @@ dependencies { // REST final retrofit_version = '2.9.0' implementation "com.squareup.retrofit2:retrofit:$retrofit_version" + implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0" implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" implementation "com.squareup.retrofit2:adapter-rxjava:$retrofit_version" // JSON + implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0" implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.github.salomonbrys.kotson:kotson:2.5.0' diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt index ec20699af..23c7ef6a3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubRelease.kt @@ -1,7 +1,8 @@ package eu.kanade.tachiyomi.data.updater.github -import com.google.gson.annotations.SerializedName import eu.kanade.tachiyomi.data.updater.Release +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable /** * Release object. @@ -11,10 +12,11 @@ import eu.kanade.tachiyomi.data.updater.Release * @param info log of latest release. * @param assets assets of latest release. */ +@Serializable class GithubRelease( - @SerializedName("tag_name") val version: String, - @SerializedName("body") override val info: String, - @SerializedName("assets") private val assets: List + @SerialName("tag_name") val version: String, + @SerialName("body") override val info: String, + @SerialName("assets") private val assets: List ) : Release { /** @@ -28,5 +30,6 @@ class GithubRelease( * Assets class containing download url. * @param downloadLink download url. */ - class Assets(@SerializedName("browser_download_url") val downloadLink: String) + @Serializable + class Assets(@SerialName("browser_download_url") val downloadLink: String) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt index e64a19d5e..3b702877d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubService.kt @@ -1,8 +1,10 @@ package eu.kanade.tachiyomi.data.updater.github +import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory import eu.kanade.tachiyomi.network.NetworkHelper +import kotlinx.serialization.json.Json +import okhttp3.MediaType.Companion.toMediaType import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.GET import retrofit2.http.Path import uy.kohesive.injekt.Injekt @@ -17,7 +19,7 @@ interface GithubService { fun create(): GithubService { val restAdapter = Retrofit.Builder() .baseUrl("https://api.github.com") - .addConverterFactory(GsonConverterFactory.create()) + .addConverterFactory(Json { ignoreUnknownKeys = true }.asConverterFactory("application/json".toMediaType())) .client(Injekt.get().client) .build() 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 6b991f2b9..941c6e6e2 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 @@ -3,14 +3,16 @@ package eu.kanade.tachiyomi.extension.api import android.content.Context import com.github.salomonbrys.kotson.get import com.github.salomonbrys.kotson.int -import com.github.salomonbrys.kotson.string -import com.google.gson.JsonArray import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.LoadResult import eu.kanade.tachiyomi.extension.util.ExtensionLoader import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.int +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive import uy.kohesive.injekt.injectLazy import java.util.Date @@ -53,18 +55,18 @@ internal class ExtensionGithubApi { private fun parseResponse(json: JsonArray): List { return json .filter { element -> - val versionName = element["version"].string + val versionName = element.jsonObject["version"]!!.jsonPrimitive.content val libVersion = versionName.substringBeforeLast('.').toDouble() libVersion >= ExtensionLoader.LIB_VERSION_MIN && libVersion <= ExtensionLoader.LIB_VERSION_MAX } .map { element -> - val name = element["name"].string.substringAfter("Tachiyomi: ") - val pkgName = element["pkg"].string - val apkName = element["apk"].string - val versionName = element["version"].string - val versionCode = element["code"].int - val lang = element["lang"].string - val nsfw = element["nsfw"].int == 1 + 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.int + 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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubService.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubService.kt index 052bf8d66..c8848b08c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubService.kt @@ -1,9 +1,11 @@ package eu.kanade.tachiyomi.extension.api -import com.google.gson.JsonArray +import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory import eu.kanade.tachiyomi.network.NetworkHelper +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonArray +import okhttp3.MediaType.Companion.toMediaType import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.GET import uy.kohesive.injekt.injectLazy @@ -29,7 +31,7 @@ interface ExtensionGithubService { fun create(): ExtensionGithubService { val adapter = Retrofit.Builder() .baseUrl(ExtensionGithubApi.BASE_URL) - .addConverterFactory(GsonConverterFactory.create()) + .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) .client(client) .build() diff --git a/build.gradle.kts b/build.gradle.kts index 8b8583660..243efc4a7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,7 +27,8 @@ buildscript { dependencies { classpath("com.github.zellius:android-shortcut-gradle-plugin:0.1.2") classpath("com.google.gms:google-services:4.3.3") - classpath("com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:8.4.2") + classpath("com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:8.3.0") + classpath(kotlin("serialization", version = BuildPluginsVersion.KOTLIN)) } repositories { google()