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) }
}
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)
class PreferencesHelper(val context: Context) {

View File

@ -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

View File

@ -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() {

View File

@ -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()
}

View File

@ -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
}
}