Use Kotlin extensions for preference editing

This commit is contained in:
arkon 2020-07-30 23:04:50 -04:00
parent 01a837fde6
commit eb0e0a1952
2 changed files with 20 additions and 10 deletions

View file

@ -1,6 +1,7 @@
package eu.kanade.tachiyomi.data.download package eu.kanade.tachiyomi.data.download
import android.content.Context import android.content.Context
import androidx.core.content.edit
import com.github.salomonbrys.kotson.fromJson import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson import com.google.gson.Gson
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
@ -22,7 +23,7 @@ class DownloadPendingDeleter(context: Context) {
/** /**
* Preferences used to store the list of chapters to delete. * Preferences used to store the list of chapters to delete.
*/ */
private val prefs = context.getSharedPreferences("chapters_to_delete", Context.MODE_PRIVATE) private val preferences = context.getSharedPreferences("chapters_to_delete", Context.MODE_PRIVATE)
/** /**
* Last added chapter, used to avoid decoding from the preference too often. * Last added chapter, used to avoid decoding from the preference too often.
@ -49,7 +50,7 @@ class DownloadPendingDeleter(context: Context) {
// Last entry matches the manga, reuse it to avoid decoding json from preferences // Last entry matches the manga, reuse it to avoid decoding json from preferences
lastEntry.copy(chapters = newChapters) lastEntry.copy(chapters = newChapters)
} else { } else {
val existingEntry = prefs.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 = gson.fromJson<Entry>(existingEntry)
@ -69,7 +70,9 @@ class DownloadPendingDeleter(context: Context) {
// Save current state // Save current state
val json = gson.toJson(newEntry) val json = gson.toJson(newEntry)
prefs.edit().putString(newEntry.manga.id.toString(), json).apply() preferences.edit {
putString(newEntry.manga.id.toString(), json)
}
lastAddedEntry = newEntry lastAddedEntry = newEntry
} }
@ -82,7 +85,9 @@ class DownloadPendingDeleter(context: Context) {
@Synchronized @Synchronized
fun getPendingChapters(): Map<Manga, List<Chapter>> { fun getPendingChapters(): Map<Manga, List<Chapter>> {
val entries = decodeAll() val entries = decodeAll()
prefs.edit().clear().apply() preferences.edit {
clear()
}
lastAddedEntry = null lastAddedEntry = null
return entries.associate { entry -> return entries.associate { entry ->
@ -94,7 +99,7 @@ class DownloadPendingDeleter(context: Context) {
* Decodes all the chapters from preferences. * Decodes all the chapters from preferences.
*/ */
private fun decodeAll(): List<Entry> { private fun decodeAll(): List<Entry> {
return prefs.all.values.mapNotNull { rawEntry -> return preferences.all.values.mapNotNull { rawEntry ->
try { try {
(rawEntry as? String)?.let { gson.fromJson<Entry>(it) } (rawEntry as? String)?.let { gson.fromJson<Entry>(it) }
} catch (e: Exception) { } catch (e: Exception) {

View file

@ -1,6 +1,7 @@
package eu.kanade.tachiyomi.data.download package eu.kanade.tachiyomi.data.download
import android.content.Context import android.content.Context
import androidx.core.content.edit
import com.google.gson.Gson 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
@ -42,9 +43,9 @@ class DownloadStore(
* @param downloads the list of downloads to add. * @param downloads the list of downloads to add.
*/ */
fun addAll(downloads: List<Download>) { fun addAll(downloads: List<Download>) {
val editor = preferences.edit() preferences.edit {
downloads.forEach { editor.putString(getKey(it), serialize(it)) } downloads.forEach { putString(getKey(it), serialize(it)) }
editor.apply() }
} }
/** /**
@ -53,14 +54,18 @@ class DownloadStore(
* @param download the download to remove. * @param download the download to remove.
*/ */
fun remove(download: Download) { fun remove(download: Download) {
preferences.edit().remove(getKey(download)).apply() preferences.edit {
remove(getKey(download))
}
} }
/** /**
* Removes all the downloads from the store. * Removes all the downloads from the store.
*/ */
fun clear() { fun clear() {
preferences.edit().clear().apply() preferences.edit {
clear()
}
} }
/** /**