diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Avatar.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Avatar.kt index b54f4ec02..d0c2ffa45 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Avatar.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Avatar.kt @@ -1,5 +1,8 @@ package eu.kanade.tachiyomi.data.track.bangumi +import kotlinx.serialization.Serializable + +@Serializable data class Avatar( val large: String? = "", val medium: String? = "", diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt index 99f1db581..a2a8a6c7f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt @@ -2,22 +2,21 @@ package eu.kanade.tachiyomi.data.track.bangumi import android.content.Context import android.graphics.Color -import com.google.gson.Gson import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.model.TrackSearch +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json import rx.Completable import rx.Observable -import uy.kohesive.injekt.injectLazy class Bangumi(private val context: Context, id: Int) : TrackService(id) { override val name = "Bangumi" - private val gson: Gson by injectLazy() - - private val interceptor by lazy { BangumiInterceptor(this, gson) } + private val interceptor by lazy { BangumiInterceptor(this) } private val api by lazy { BangumiApi(client, interceptor) } @@ -112,13 +111,12 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) { } fun saveToken(oauth: OAuth?) { - val json = gson.toJson(oauth) - preferences.trackToken(this).set(json) + preferences.trackToken(this).set(Json.encodeToString(oauth)) } fun restoreToken(): OAuth? { return try { - gson.fromJson(preferences.trackToken(this).get(), OAuth::class.java) + Json.decodeFromString(preferences.trackToken(this).get()) } catch (e: Exception) { null } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiApi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiApi.kt index fedb1a956..a9d7b3ee3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiApi.kt @@ -2,27 +2,28 @@ package eu.kanade.tachiyomi.data.track.bangumi import android.net.Uri import androidx.core.net.toUri -import com.github.salomonbrys.kotson.array -import com.github.salomonbrys.kotson.obj -import com.google.gson.Gson -import com.google.gson.JsonObject -import com.google.gson.JsonParser import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.model.TrackSearch import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.asObservableSuccess +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.float +import kotlinx.serialization.json.int +import kotlinx.serialization.json.jsonArray +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive import okhttp3.CacheControl import okhttp3.FormBody import okhttp3.OkHttpClient import okhttp3.Request import rx.Observable -import uy.kohesive.injekt.injectLazy import java.net.URLEncoder class BangumiApi(private val client: OkHttpClient, interceptor: BangumiInterceptor) { - private val gson: Gson by injectLazy() private val authClient = client.newBuilder().addInterceptor(interceptor).build() fun addLibManga(track: Track): Observable { @@ -92,36 +93,32 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept if (responseBody.contains("\"code\":404")) { responseBody = "{\"results\":0,\"list\":[]}" } - val response = JsonParser.parseString(responseBody).obj["list"]?.array - response?.filter { it.obj["type"].asInt == 1 }?.map { jsonToSearch(it.obj) } + val response = Json.decodeFromString(responseBody)["list"]?.jsonArray + response?.filter { it.jsonObject["type"]?.jsonPrimitive?.int == 1 }?.map { jsonToSearch(it.jsonObject) } } } private fun jsonToSearch(obj: JsonObject): TrackSearch { return TrackSearch.create(TrackManager.BANGUMI).apply { - media_id = obj["id"].asInt - title = obj["name_cn"].asString - cover_url = obj["images"].obj["common"].asString - summary = obj["name"].asString - tracking_url = obj["url"].asString + media_id = obj["id"]!!.jsonPrimitive.int + title = obj["name_cn"]!!.jsonPrimitive.content + cover_url = obj["images"]!!.jsonObject["common"]!!.jsonPrimitive.content + summary = obj["name"]!!.jsonPrimitive.content + tracking_url = obj["url"]!!.jsonPrimitive.content } } private fun jsonToTrack(mangas: JsonObject): Track { return Track.create(TrackManager.BANGUMI).apply { - title = mangas["name"].asString - media_id = mangas["id"].asInt - score = if (mangas["rating"] != null) { - if (mangas["rating"].isJsonObject) { - mangas["rating"].obj["score"].asFloat - } else { - 0f - } - } else { + title = mangas["name"]!!.jsonPrimitive.content + media_id = mangas["id"]!!.jsonPrimitive.int + score = try { + mangas["rating"]!!.jsonObject["score"]!!.jsonPrimitive.float + } catch (_: Exception) { 0f } status = Bangumi.DEFAULT_STATUS - tracking_url = mangas["url"].asString + tracking_url = mangas["url"]!!.jsonPrimitive.content } } @@ -137,7 +134,7 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept .map { netResponse -> // get comic info val responseBody = netResponse.body?.string().orEmpty() - jsonToTrack(JsonParser.parseString(responseBody).obj) + jsonToTrack(Json.decodeFromString(responseBody)) } } @@ -154,7 +151,7 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept .asObservableSuccess() .map { netResponse -> val resp = netResponse.body?.string() - val coll = gson.fromJson(resp, Collection::class.java) + val coll = Json.decodeFromString(resp!!) track.status = coll.status?.id!! track.last_chapter_read = coll.ep_status!! track @@ -167,7 +164,7 @@ class BangumiApi(private val client: OkHttpClient, interceptor: BangumiIntercept if (responseBody.isEmpty()) { throw Exception("Null Response") } - gson.fromJson(responseBody, OAuth::class.java) + Json.decodeFromString(responseBody) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiInterceptor.kt index 2ffa9ad0e..c9f314812 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiInterceptor.kt @@ -1,23 +1,24 @@ package eu.kanade.tachiyomi.data.track.bangumi -import com.google.gson.Gson +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json import okhttp3.FormBody import okhttp3.Interceptor import okhttp3.Response -class BangumiInterceptor(val bangumi: Bangumi, val gson: Gson) : Interceptor { +class BangumiInterceptor(val bangumi: Bangumi) : Interceptor { /** * OAuth object used for authenticated requests. */ private var oauth: OAuth? = bangumi.restoreToken() - fun addTocken(tocken: String, oidFormBody: FormBody): FormBody { + fun addToken(token: String, oidFormBody: FormBody): FormBody { val newFormBody = FormBody.Builder() for (i in 0 until oidFormBody.size) { newFormBody.add(oidFormBody.name(i), oidFormBody.value(i)) } - newFormBody.add("access_token", tocken) + newFormBody.add("access_token", token) return newFormBody.build() } @@ -29,7 +30,7 @@ class BangumiInterceptor(val bangumi: Bangumi, val gson: Gson) : Interceptor { if (currAuth.isExpired()) { val response = chain.proceed(BangumiApi.refreshTokenRequest(currAuth.refresh_token!!)) if (response.isSuccessful) { - newAuth(gson.fromJson(response.body!!.string(), OAuth::class.java)) + newAuth(Json.decodeFromString(response.body!!.string())) } else { response.close() } @@ -42,7 +43,7 @@ class BangumiInterceptor(val bangumi: Bangumi, val gson: Gson) : Interceptor { .addQueryParameter("access_token", currAuth.access_token).build() ) .build() else originalRequest.newBuilder() - .post(addTocken(currAuth.access_token, originalRequest.body as FormBody)) + .post(addToken(currAuth.access_token, originalRequest.body as FormBody)) .header("User-Agent", "Tachiyomi") .build() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Collection.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Collection.kt index d9e3f7642..68aba86a3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Collection.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Collection.kt @@ -1,5 +1,8 @@ package eu.kanade.tachiyomi.data.track.bangumi +import kotlinx.serialization.Serializable + +@Serializable data class Collection( val `private`: Int? = 0, val comment: String? = "", diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/OAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/OAuth.kt index d3dfda20f..912d5bb40 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/OAuth.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/OAuth.kt @@ -1,5 +1,8 @@ package eu.kanade.tachiyomi.data.track.bangumi +import kotlinx.serialization.Serializable + +@Serializable data class OAuth( val access_token: String, val token_type: String, diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Status.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Status.kt index fbae6a3dd..a75f46389 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Status.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Status.kt @@ -1,5 +1,8 @@ package eu.kanade.tachiyomi.data.track.bangumi +import kotlinx.serialization.Serializable + +@Serializable data class Status( val id: Int? = 0, val name: String? = "", diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/User.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/User.kt index 274bc3bcb..95c1f6dd2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/User.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/User.kt @@ -1,5 +1,8 @@ package eu.kanade.tachiyomi.data.track.bangumi +import kotlinx.serialization.Serializable + +@Serializable data class User( val avatar: Avatar? = Avatar(), val id: Int? = 0,