Using json instead gson in chaptercache

Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com>
This commit is contained in:
Jays2Kings 2021-04-22 20:49:44 -04:00
parent 8e7143e8b4
commit 421f42cd78
4 changed files with 16 additions and 20 deletions

View File

@ -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")

View File

@ -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<List<Page>> {
return Observable.fromCallable {
fun getPageListFromCache(chapter: Chapter): List<Page> {
// 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<List<Page>>(it.getString(0))
}
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<Page>) {
// 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

View File

@ -102,8 +102,7 @@ class HttpPageLoader(
* the local cache, otherwise fallbacks to network.
*/
override fun getPages(): Observable<List<ReaderPage>> {
return chapterCache
.getPageListFromCache(chapter.chapter)
return Observable.fromCallable { chapterCache.getPageListFromCache(chapter.chapter) }
.onErrorResumeNext { source.fetchPageList(chapter.chapter) }
.map { pages ->
pages.mapIndexed { index, page ->

View File

@ -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"