Add operator functions for handling set preferences

This commit is contained in:
arkon 2020-07-25 18:07:10 -04:00
parent e95a5be21d
commit 2dc47352f8
5 changed files with 29 additions and 26 deletions

View File

@ -27,6 +27,14 @@ fun <T> Preference<T>.asImmediateFlow(block: (value: T) -> Unit): Flow<T> {
.onEach { block(it) } .onEach { block(it) }
} }
operator fun <T> Preference<Set<T>>.plusAssign(item: T) {
set(get() + item)
}
operator fun <T> Preference<Set<T>>.minusAssign(item: T) {
set(get() - item)
}
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
class PreferencesHelper(val context: Context) { class PreferencesHelper(val context: Context) {

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import com.jakewharton.rxrelay.BehaviorRelay import com.jakewharton.rxrelay.BehaviorRelay
import eu.kanade.tachiyomi.data.preference.PreferencesHelper 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.api.ExtensionGithubApi
import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.extension.model.InstallStep import eu.kanade.tachiyomi.extension.model.InstallStep
@ -257,8 +258,7 @@ class ExtensionManager(
if (signature !in untrustedSignatures) return if (signature !in untrustedSignatures) return
ExtensionLoader.trustedSignatures += signature ExtensionLoader.trustedSignatures += signature
val preference = preferences.trustedSignatures() preferences.trustedSignatures() += signature
preference.set(preference.get() + signature)
val nowTrustedExtensions = untrustedExtensions.filter { it.signatureHash == signature } val nowTrustedExtensions = untrustedExtensions.filter { it.signatureHash == signature }
untrustedExtensions -= nowTrustedExtensions untrustedExtensions -= nowTrustedExtensions

View File

@ -26,6 +26,8 @@ import androidx.recyclerview.widget.LinearLayoutManager
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.EmptyPreferenceDataStore import eu.kanade.tachiyomi.data.preference.EmptyPreferenceDataStore
import eu.kanade.tachiyomi.data.preference.PreferencesHelper 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.databinding.ExtensionDetailControllerBinding
import eu.kanade.tachiyomi.extension.model.Extension import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.CatalogueSource
@ -197,15 +199,11 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
} }
private fun toggleSource(source: Source, enable: Boolean) { private fun toggleSource(source: Source, enable: Boolean) {
val current = preferences.disabledSources().get() if (enable) {
preferences.disabledSources() -= source.id.toString()
preferences.disabledSources().set( } else {
if (enable) { preferences.disabledSources() += source.id.toString()
current - source.id.toString() }
} else {
current + source.id.toString()
}
)
} }
private fun openInSettings() { private fun openInSettings() {

View File

@ -17,6 +17,8 @@ import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.IFlexible import eu.davidea.flexibleadapter.items.IFlexible
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper 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.databinding.SourceMainControllerBinding
import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.LocalSource
@ -152,8 +154,7 @@ class SourceController :
} }
private fun disableSource(source: Source) { private fun disableSource(source: Source) {
val current = preferences.disabledSources().get() preferences.disabledSources() += source.id.toString()
preferences.disabledSources().set(current + source.id.toString())
presenter.updateSources() presenter.updateSources()
} }

View File

@ -5,6 +5,8 @@ import androidx.preference.CheckBoxPreference
import androidx.preference.PreferenceGroup import androidx.preference.PreferenceGroup
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R 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.SourceManager
import eu.kanade.tachiyomi.source.getPreferenceKey import eu.kanade.tachiyomi.source.getPreferenceKey
import eu.kanade.tachiyomi.source.icon import eu.kanade.tachiyomi.source.icon
@ -54,12 +56,11 @@ class SourceFilterController : SettingsController() {
onChange { newValue -> onChange { newValue ->
val checked = newValue as Boolean val checked = newValue as Boolean
val current = preferences.enabledLanguages().get()
if (!checked) { if (!checked) {
preferences.enabledLanguages().set(current - lang) preferences.enabledLanguages() -= lang
removeAll() removeAll()
} else { } else {
preferences.enabledLanguages().set(current + lang) preferences.enabledLanguages() += lang
addLanguageSources(this, sources) addLanguageSources(this, sources)
} }
true true
@ -97,16 +98,11 @@ class SourceFilterController : SettingsController() {
onChange { newValue -> onChange { newValue ->
val checked = newValue as Boolean val checked = newValue as Boolean
val current = preferences.disabledSources().get() if (checked) {
preferences.disabledSources() -= id
preferences.disabledSources().set( } else {
if (checked) { preferences.disabledSources() += id
current - id }
} else {
current + id
}
)
true true
} }
} }