From 2dc47352f87acd7017bb1ad385380231f0fff043 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 25 Jul 2020 18:07:10 -0400 Subject: [PATCH] Add operator functions for handling set preferences --- .../data/preference/PreferencesHelper.kt | 8 +++++++ .../tachiyomi/extension/ExtensionManager.kt | 4 ++-- .../details/ExtensionDetailsController.kt | 16 ++++++-------- .../ui/browse/source/SourceController.kt | 5 +++-- .../browse/source/SourceFilterController.kt | 22 ++++++++----------- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index e49752eadf..3c43f3081a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -27,6 +27,14 @@ fun Preference.asImmediateFlow(block: (value: T) -> Unit): Flow { .onEach { block(it) } } +operator fun Preference>.plusAssign(item: T) { + set(get() + item) +} + +operator fun Preference>.minusAssign(item: T) { + set(get() - item) +} + @OptIn(ExperimentalCoroutinesApi::class) class PreferencesHelper(val context: Context) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt index 96ef8b3176..785cc70c16 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.drawable.Drawable import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.plusAssign import eu.kanade.tachiyomi.extension.api.ExtensionGithubApi import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.InstallStep @@ -257,8 +258,7 @@ class ExtensionManager( if (signature !in untrustedSignatures) return ExtensionLoader.trustedSignatures += signature - val preference = preferences.trustedSignatures() - preference.set(preference.get() + signature) + preferences.trustedSignatures() += signature val nowTrustedExtensions = untrustedExtensions.filter { it.signatureHash == signature } untrustedExtensions -= nowTrustedExtensions diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt index a132bf30d9..a715ca4c84 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt @@ -26,6 +26,8 @@ import androidx.recyclerview.widget.LinearLayoutManager import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.EmptyPreferenceDataStore import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.minusAssign +import eu.kanade.tachiyomi.data.preference.plusAssign import eu.kanade.tachiyomi.databinding.ExtensionDetailControllerBinding import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.source.CatalogueSource @@ -197,15 +199,11 @@ class ExtensionDetailsController(bundle: Bundle? = null) : } private fun toggleSource(source: Source, enable: Boolean) { - val current = preferences.disabledSources().get() - - preferences.disabledSources().set( - if (enable) { - current - source.id.toString() - } else { - current + source.id.toString() - } - ) + if (enable) { + preferences.disabledSources() -= source.id.toString() + } else { + preferences.disabledSources() += source.id.toString() + } } private fun openInSettings() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt index c0593c7df7..52636430c0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceController.kt @@ -17,6 +17,8 @@ import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.items.IFlexible import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.minusAssign +import eu.kanade.tachiyomi.data.preference.plusAssign import eu.kanade.tachiyomi.databinding.SourceMainControllerBinding import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.LocalSource @@ -152,8 +154,7 @@ class SourceController : } private fun disableSource(source: Source) { - val current = preferences.disabledSources().get() - preferences.disabledSources().set(current + source.id.toString()) + preferences.disabledSources() += source.id.toString() presenter.updateSources() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterController.kt index 24db7e8b1d..1edf4969e6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceFilterController.kt @@ -5,6 +5,8 @@ import androidx.preference.CheckBoxPreference import androidx.preference.PreferenceGroup import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.minusAssign +import eu.kanade.tachiyomi.data.preference.plusAssign import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.getPreferenceKey import eu.kanade.tachiyomi.source.icon @@ -54,12 +56,11 @@ class SourceFilterController : SettingsController() { onChange { newValue -> val checked = newValue as Boolean - val current = preferences.enabledLanguages().get() if (!checked) { - preferences.enabledLanguages().set(current - lang) + preferences.enabledLanguages() -= lang removeAll() } else { - preferences.enabledLanguages().set(current + lang) + preferences.enabledLanguages() += lang addLanguageSources(this, sources) } true @@ -97,16 +98,11 @@ class SourceFilterController : SettingsController() { onChange { newValue -> val checked = newValue as Boolean - val current = preferences.disabledSources().get() - - preferences.disabledSources().set( - if (checked) { - current - id - } else { - current + id - } - ) - + if (checked) { + preferences.disabledSources() -= id + } else { + preferences.disabledSources() += id + } true } }