From 421f42cd7890ea2884a0c56330e9c501942df7df Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Thu, 22 Apr 2021 20:49:44 -0400 Subject: [PATCH] Using json instead gson in chaptercache Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com> --- app/build.gradle.kts | 4 +-- .../tachiyomi/data/cache/ChapterCache.kt | 26 +++++++++---------- .../ui/reader/loader/HttpPageLoader.kt | 3 +-- buildSrc/src/main/kotlin/Dependencies.kt | 3 +-- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 206647ec6a..22460e48f0 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -161,8 +161,8 @@ dependencies { implementation(kotlin("reflect", version = Versions.kotlin)) // JSON - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.KOTLINSERIALIZATION}") - implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:${Versions.KOTLINSERIALIZATION}") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.kotlinSerialization}") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:${Versions.kotlinSerialization}") 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/cache/ChapterCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt index e6314d5ce8..c09028b7c7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt @@ -2,8 +2,6 @@ package eu.kanade.tachiyomi.data.cache import android.content.Context import android.text.format.Formatter -import com.github.salomonbrys.kotson.fromJson -import com.google.gson.Gson import com.jakewharton.disklrucache.DiskLruCache import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -16,10 +14,12 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json import okhttp3.Response import okio.buffer import okio.sink -import rx.Observable import uy.kohesive.injekt.injectLazy import java.io.File import java.io.IOException @@ -52,7 +52,7 @@ class ChapterCache(private val context: Context) { } /** Google Json class used for parsing JSON files. */ - private val gson: Gson by injectLazy() + private val json: Json by injectLazy() private val preferences: PreferencesHelper by injectLazy() @@ -127,17 +127,15 @@ class ChapterCache(private val context: Context) { * Get page list from cache. * * @param chapter the chapter. - * @return an observable of the list of pages. + * @return the list of pages. */ - fun getPageListFromCache(chapter: Chapter): Observable> { - return Observable.fromCallable { - // Get the key for the chapter. - val key = DiskUtil.hashKeyForDisk(getKey(chapter)) + fun getPageListFromCache(chapter: Chapter): List { + // Get the key for the chapter. + val key = DiskUtil.hashKeyForDisk(getKey(chapter)) - // Convert JSON string to list of objects. Throws an exception if snapshot is null - diskCache.get(key).use { - gson.fromJson>(it.getString(0)) - } + // Convert JSON string to list of objects. Throws an exception if snapshot is null + return diskCache.get(key).use { + json.decodeFromString(it.getString(0)) } } @@ -149,7 +147,7 @@ class ChapterCache(private val context: Context) { */ fun putPageListToCache(chapter: Chapter, pages: List) { // Convert list of pages to json string. - val cachedValue = gson.toJson(pages) + val cachedValue = json.encodeToString(pages) // Initialize the editor (edits the values for an entry). var editor: DiskLruCache.Editor? = null diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt index 6d6575e748..bc97aca2aa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt @@ -102,8 +102,7 @@ class HttpPageLoader( * the local cache, otherwise fallbacks to network. */ override fun getPages(): Observable> { - return chapterCache - .getPageListFromCache(chapter.chapter) + return Observable.fromCallable { chapterCache.getPageListFromCache(chapter.chapter) } .onErrorResumeNext { source.fetchPageList(chapter.chapter) } .map { pages -> pages.mapIndexed { index, page -> diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt index 12c665e2aa..e326dcf0e1 100644 --- a/buildSrc/src/main/kotlin/Dependencies.kt +++ b/buildSrc/src/main/kotlin/Dependencies.kt @@ -7,7 +7,6 @@ object Versions { const val NUCLEUS = "3.0.0" const val OSS_LICENSE = "17.0.0" const val RETROFIT = "2.7.2" - const val KOTLINSERIALIZATION = "1.0.1" const val ROBO_ELECTRIC = "3.1.4" const val RX_BINDING = "1.0.1" const val TIMBER = "4.7.1" @@ -53,7 +52,7 @@ object Versions { const val junit = "4.13" const val kotlin = "1.4.32" const val kotlinCoroutines = "1.3.9" - const val kotlinSerialization = "1.0.1" + const val kotlinSerialization = "1.1.0" const val kotson = "2.5.0" const val ktlint = "9.4.0" const val loadingButton = "2.2.0"