Migrate to kotlinx.serialization for download store and deleter
This commit is contained in:
parent
e7d6605490
commit
980feb6c96
@ -2,11 +2,12 @@ package eu.kanade.tachiyomi.data.download
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.core.content.edit
|
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.Chapter
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
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.
|
* Class used to keep a list of chapters for future deletion.
|
||||||
@ -15,11 +16,6 @@ import uy.kohesive.injekt.injectLazy
|
|||||||
*/
|
*/
|
||||||
class DownloadPendingDeleter(context: Context) {
|
class DownloadPendingDeleter(context: Context) {
|
||||||
|
|
||||||
/**
|
|
||||||
* Gson instance to encode and decode chapters.
|
|
||||||
*/
|
|
||||||
private val gson by injectLazy<Gson>()
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Preferences used to store the list of chapters to delete.
|
* 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)
|
val existingEntry = preferences.getString(manga.id!!.toString(), null)
|
||||||
if (existingEntry != null) {
|
if (existingEntry != null) {
|
||||||
// Existing entry found on preferences, decode json and add the new chapter
|
// Existing entry found on preferences, decode json and add the new chapter
|
||||||
val savedEntry = gson.fromJson<Entry>(existingEntry)
|
val savedEntry = Json.decodeFromString<Entry>(existingEntry)
|
||||||
|
|
||||||
// Append new chapters
|
// Append new chapters
|
||||||
val newChapters = savedEntry.chapters.addUniqueById(chapters)
|
val newChapters = savedEntry.chapters.addUniqueById(chapters)
|
||||||
@ -69,7 +65,7 @@ class DownloadPendingDeleter(context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save current state
|
// Save current state
|
||||||
val json = gson.toJson(newEntry)
|
val json = Json.encodeToString(newEntry)
|
||||||
preferences.edit {
|
preferences.edit {
|
||||||
putString(newEntry.manga.id.toString(), json)
|
putString(newEntry.manga.id.toString(), json)
|
||||||
}
|
}
|
||||||
@ -101,7 +97,7 @@ class DownloadPendingDeleter(context: Context) {
|
|||||||
private fun decodeAll(): List<Entry> {
|
private fun decodeAll(): List<Entry> {
|
||||||
return preferences.all.values.mapNotNull { rawEntry ->
|
return preferences.all.values.mapNotNull { rawEntry ->
|
||||||
try {
|
try {
|
||||||
(rawEntry as? String)?.let { gson.fromJson<Entry>(it) }
|
(rawEntry as? String)?.let { Json.decodeFromString<Entry>(it) }
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
@ -124,6 +120,7 @@ class DownloadPendingDeleter(context: Context) {
|
|||||||
/**
|
/**
|
||||||
* Class used to save an entry of chapters with their manga into preferences.
|
* Class used to save an entry of chapters with their manga into preferences.
|
||||||
*/
|
*/
|
||||||
|
@Serializable
|
||||||
private data class Entry(
|
private data class Entry(
|
||||||
val chapters: List<ChapterEntry>,
|
val chapters: List<ChapterEntry>,
|
||||||
val manga: MangaEntry
|
val manga: MangaEntry
|
||||||
@ -132,6 +129,7 @@ class DownloadPendingDeleter(context: Context) {
|
|||||||
/**
|
/**
|
||||||
* Class used to save an entry for a chapter into preferences.
|
* Class used to save an entry for a chapter into preferences.
|
||||||
*/
|
*/
|
||||||
|
@Serializable
|
||||||
private data class ChapterEntry(
|
private data class ChapterEntry(
|
||||||
val id: Long,
|
val id: Long,
|
||||||
val url: String,
|
val url: String,
|
||||||
@ -142,6 +140,7 @@ class DownloadPendingDeleter(context: Context) {
|
|||||||
/**
|
/**
|
||||||
* Class used to save an entry for a manga into preferences.
|
* Class used to save an entry for a manga into preferences.
|
||||||
*/
|
*/
|
||||||
|
@Serializable
|
||||||
private data class MangaEntry(
|
private data class MangaEntry(
|
||||||
val id: Long,
|
val id: Long,
|
||||||
val url: String,
|
val url: String,
|
||||||
|
@ -2,12 +2,15 @@ package eu.kanade.tachiyomi.data.download
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import com.google.gson.Gson
|
|
||||||
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
import eu.kanade.tachiyomi.data.database.DatabaseHelper
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||||
import eu.kanade.tachiyomi.data.download.model.Download
|
import eu.kanade.tachiyomi.data.download.model.Download
|
||||||
import eu.kanade.tachiyomi.source.SourceManager
|
import eu.kanade.tachiyomi.source.SourceManager
|
||||||
import eu.kanade.tachiyomi.source.online.HttpSource
|
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
|
import uy.kohesive.injekt.injectLazy
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,11 +28,6 @@ class DownloadStore(
|
|||||||
*/
|
*/
|
||||||
private val preferences = context.getSharedPreferences("active_downloads", Context.MODE_PRIVATE)
|
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()
|
private val db: DatabaseHelper by injectLazy()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,7 +109,7 @@ class DownloadStore(
|
|||||||
*/
|
*/
|
||||||
private fun serialize(download: Download): String {
|
private fun serialize(download: Download): String {
|
||||||
val obj = DownloadObject(download.manga.id!!, download.chapter.id!!, counter++)
|
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? {
|
private fun deserialize(string: String): DownloadObject? {
|
||||||
return try {
|
return try {
|
||||||
gson.fromJson(string, DownloadObject::class.java)
|
Json.decodeFromString<DownloadObject>(string)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
@ -134,5 +132,6 @@ class DownloadStore(
|
|||||||
* @param chapterId the id of the chapter.
|
* @param chapterId the id of the chapter.
|
||||||
* @param order the order of the download in the queue.
|
* @param order the order of the download in the queue.
|
||||||
*/
|
*/
|
||||||
|
@Serializable
|
||||||
data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int)
|
data class DownloadObject(val mangaId: Long, val chapterId: Long, val order: Int)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user