From eb0e0a1952aa3ba0de56f99f15768cf83d8f67b8 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 30 Jul 2020 23:04:50 -0400 Subject: [PATCH] Use Kotlin extensions for preference editing --- .../data/download/DownloadPendingDeleter.kt | 15 ++++++++++----- .../tachiyomi/data/download/DownloadStore.kt | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 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 ff036006c..cf30dfaa5 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 @@ -1,6 +1,7 @@ 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 @@ -22,7 +23,7 @@ class DownloadPendingDeleter(context: Context) { /** * 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. @@ -49,7 +50,7 @@ class DownloadPendingDeleter(context: Context) { // Last entry matches the manga, reuse it to avoid decoding json from preferences lastEntry.copy(chapters = newChapters) } else { - val existingEntry = prefs.getString(manga.id!!.toString(), null) + 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) @@ -69,7 +70,9 @@ class DownloadPendingDeleter(context: Context) { // Save current state val json = gson.toJson(newEntry) - prefs.edit().putString(newEntry.manga.id.toString(), json).apply() + preferences.edit { + putString(newEntry.manga.id.toString(), json) + } lastAddedEntry = newEntry } @@ -82,7 +85,9 @@ class DownloadPendingDeleter(context: Context) { @Synchronized fun getPendingChapters(): Map> { val entries = decodeAll() - prefs.edit().clear().apply() + preferences.edit { + clear() + } lastAddedEntry = null return entries.associate { entry -> @@ -94,7 +99,7 @@ class DownloadPendingDeleter(context: Context) { * Decodes all the chapters from preferences. */ private fun decodeAll(): List { - return prefs.all.values.mapNotNull { rawEntry -> + return preferences.all.values.mapNotNull { rawEntry -> try { (rawEntry as? String)?.let { gson.fromJson(it) } } catch (e: Exception) { 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 de9b48630..42a55bb0d 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 @@ -1,6 +1,7 @@ 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 @@ -42,9 +43,9 @@ class DownloadStore( * @param downloads the list of downloads to add. */ fun addAll(downloads: List) { - val editor = preferences.edit() - downloads.forEach { editor.putString(getKey(it), serialize(it)) } - editor.apply() + preferences.edit { + downloads.forEach { putString(getKey(it), serialize(it)) } + } } /** @@ -53,14 +54,18 @@ class DownloadStore( * @param download the download to remove. */ fun remove(download: Download) { - preferences.edit().remove(getKey(download)).apply() + preferences.edit { + remove(getKey(download)) + } } /** * Removes all the downloads from the store. */ fun clear() { - preferences.edit().clear().apply() + preferences.edit { + clear() + } } /**