From 980feb6c96282e5b0b92a2889980ce3bd2197c37 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 11 Oct 2020 15:26:57 -0400 Subject: [PATCH] Migrate to kotlinx.serialization for download store and deleter --- .../data/download/DownloadPendingDeleter.kt | 21 +++++++++---------- .../tachiyomi/data/download/DownloadStore.kt | 15 +++++++------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt index cf30dfaa54..1da4813c88 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt @@ -2,11 +2,12 @@ package eu.kanade.tachiyomi.data.download import android.content.Context import androidx.core.content.edit -import com.github.salomonbrys.kotson.fromJson -import com.google.gson.Gson import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga -import uy.kohesive.injekt.injectLazy +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json /** * Class used to keep a list of chapters for future deletion. @@ -15,11 +16,6 @@ import uy.kohesive.injekt.injectLazy */ class DownloadPendingDeleter(context: Context) { - /** - * Gson instance to encode and decode chapters. - */ - private val gson by injectLazy() - /** * Preferences used to store the list of chapters to delete. */ @@ -53,7 +49,7 @@ class DownloadPendingDeleter(context: Context) { val existingEntry = preferences.getString(manga.id!!.toString(), null) if (existingEntry != null) { // Existing entry found on preferences, decode json and add the new chapter - val savedEntry = gson.fromJson(existingEntry) + val savedEntry = Json.decodeFromString(existingEntry) // Append new chapters val newChapters = savedEntry.chapters.addUniqueById(chapters) @@ -69,7 +65,7 @@ class DownloadPendingDeleter(context: Context) { } // Save current state - val json = gson.toJson(newEntry) + val json = Json.encodeToString(newEntry) preferences.edit { putString(newEntry.manga.id.toString(), json) } @@ -101,7 +97,7 @@ class DownloadPendingDeleter(context: Context) { private fun decodeAll(): List { return preferences.all.values.mapNotNull { rawEntry -> try { - (rawEntry as? String)?.let { gson.fromJson(it) } + (rawEntry as? String)?.let { Json.decodeFromString(it) } } catch (e: Exception) { null } @@ -124,6 +120,7 @@ class DownloadPendingDeleter(context: Context) { /** * Class used to save an entry of chapters with their manga into preferences. */ + @Serializable private data class Entry( val chapters: List, val manga: MangaEntry @@ -132,6 +129,7 @@ class DownloadPendingDeleter(context: Context) { /** * Class used to save an entry for a chapter into preferences. */ + @Serializable private data class ChapterEntry( val id: Long, val url: String, @@ -142,6 +140,7 @@ class DownloadPendingDeleter(context: Context) { /** * Class used to save an entry for a manga into preferences. */ + @Serializable private data class MangaEntry( val id: Long, val url: String, diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadStore.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadStore.kt index 42a55bb0db..d885fb6f1b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadStore.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadStore.kt @@ -2,12 +2,15 @@ package eu.kanade.tachiyomi.data.download import android.content.Context import androidx.core.content.edit -import com.google.gson.Gson import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.online.HttpSource +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json import uy.kohesive.injekt.injectLazy /** @@ -25,11 +28,6 @@ class DownloadStore( */ private val preferences = context.getSharedPreferences("active_downloads", Context.MODE_PRIVATE) - /** - * Gson instance to serialize/deserialize downloads. - */ - private val gson: Gson by injectLazy() - private val db: DatabaseHelper by injectLazy() /** @@ -111,7 +109,7 @@ class DownloadStore( */ private fun serialize(download: Download): String { val obj = DownloadObject(download.manga.id!!, download.chapter.id!!, counter++) - return gson.toJson(obj) + return Json.encodeToString(obj) } /** @@ -121,7 +119,7 @@ class DownloadStore( */ private fun deserialize(string: String): DownloadObject? { return try { - gson.fromJson(string, DownloadObject::class.java) + Json.decodeFromString(string) } catch (e: Exception) { null } @@ -134,5 +132,6 @@ class DownloadStore( * @param chapterId the id of the chapter. * @param order the order of the download in the queue. */ + @Serializable data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int) }