mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-03 10:54:38 -05:00
add support for MultiSelectListPreference (#258)
* add support for MultiSelectListPreference * Update AndroidCompat/src/main/java/xyz/nulldev/androidcompat/io/sharedprefs/JavaSharedPreferences.kt Co-authored-by: Mitchell Syer <Mitchellptbo@gmail.com> * don't convert to list * fix by @Syer10 Co-authored-by: Mitchell Syer <Mitchellptbo@gmail.com>
This commit is contained in:
@@ -13,33 +13,49 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class MultiSelectListPreference extends DialogPreference {
|
public class MultiSelectListPreference extends DialogPreference {
|
||||||
|
// reference: https://android.googlesource.com/platform/frameworks/support/+/996971f962fcd554339a7cb2859cef9ca89dbcb7/preference/preference/src/main/java/androidx/preference/MultiSelectListPreference.java
|
||||||
// Note: remove @JsonIgnore and implement methods if any extension ever uses these methods or the variables behind them
|
// Note: remove @JsonIgnore and implement methods if any extension ever uses these methods or the variables behind them
|
||||||
|
|
||||||
public MultiSelectListPreference(Context context) { super(context); }
|
private CharSequence[] entries;
|
||||||
|
private CharSequence[] entryValues;
|
||||||
|
|
||||||
|
public MultiSelectListPreference(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEntries(CharSequence[] entries) {
|
||||||
|
this.entries = entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CharSequence[] getEntries() {
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEntryValues(CharSequence[] entryValues) {
|
||||||
|
this.entryValues = entryValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CharSequence[] getEntryValues() {
|
||||||
|
return entryValues;
|
||||||
|
}
|
||||||
|
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
public void setEntries(CharSequence[] entries) { throw new RuntimeException("Stub!"); }
|
public void setValues(Set<String> values) {
|
||||||
|
throw new RuntimeException("Stub!");
|
||||||
|
}
|
||||||
|
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
public CharSequence[] getEntries() { throw new RuntimeException("Stub!"); }
|
public Set<String> getValues() {
|
||||||
|
throw new RuntimeException("Stub!");
|
||||||
|
}
|
||||||
|
|
||||||
@JsonIgnore
|
public int findIndexOfValue(String value) {
|
||||||
public void setEntryValues(CharSequence[] entryValues) { throw new RuntimeException("Stub!"); }
|
throw new RuntimeException("Stub!");
|
||||||
|
}
|
||||||
@JsonIgnore
|
|
||||||
public CharSequence[] getEntryValues() { throw new RuntimeException("Stub!"); }
|
|
||||||
|
|
||||||
@JsonIgnore
|
|
||||||
public void setValues(Set<String> values) { throw new RuntimeException("Stub!"); }
|
|
||||||
|
|
||||||
@JsonIgnore
|
|
||||||
public Set<String> getValues() { throw new RuntimeException("Stub!"); }
|
|
||||||
|
|
||||||
public int findIndexOfValue(String value) { throw new RuntimeException("Stub!"); }
|
|
||||||
|
|
||||||
/** Tachidesk specific API */
|
/** Tachidesk specific API */
|
||||||
@Override
|
@Override
|
||||||
public String getDefaultValueType() {
|
public String getDefaultValueType() {
|
||||||
return "Set";
|
return "Set<String>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ package androidx.preference;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A minimal implementation of androidx.preference.Preference
|
* A minimal implementation of androidx.preference.Preference
|
||||||
@@ -113,18 +114,22 @@ public class Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Tachidesk specific API */
|
/** Tachidesk specific API */
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public Object getCurrentValue() {
|
public Object getCurrentValue() {
|
||||||
switch (getDefaultValueType()) {
|
switch (getDefaultValueType()) {
|
||||||
case "String":
|
case "String":
|
||||||
return sharedPreferences.getString(key, (String)defaultValue);
|
return sharedPreferences.getString(key, (String)defaultValue);
|
||||||
case "Boolean":
|
case "Boolean":
|
||||||
return sharedPreferences.getBoolean(key, (Boolean)defaultValue);
|
return sharedPreferences.getBoolean(key, (Boolean)defaultValue);
|
||||||
|
case "Set<String>":
|
||||||
|
return sharedPreferences.getStringSet(key, (Set<String>)defaultValue);
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("Unsupported type");
|
throw new RuntimeException("Unsupported type");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Tachidesk specific API */
|
/** Tachidesk specific API */
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void saveNewValue(Object value) {
|
public void saveNewValue(Object value) {
|
||||||
switch (getDefaultValueType()) {
|
switch (getDefaultValueType()) {
|
||||||
case "String":
|
case "String":
|
||||||
@@ -133,6 +138,9 @@ public class Preference {
|
|||||||
case "Boolean":
|
case "Boolean":
|
||||||
sharedPreferences.edit().putBoolean(key, (Boolean)value).apply();
|
sharedPreferences.edit().putBoolean(key, (Boolean)value).apply();
|
||||||
break;
|
break;
|
||||||
|
case "Set<String>":
|
||||||
|
sharedPreferences.edit().putStringSet(key, (Set<String>)value).apply();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException("Unsupported type");
|
throw new RuntimeException("Unsupported type");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ import com.russhwolf.settings.ExperimentalSettingsApi
|
|||||||
import com.russhwolf.settings.ExperimentalSettingsImplementation
|
import com.russhwolf.settings.ExperimentalSettingsImplementation
|
||||||
import com.russhwolf.settings.JvmPreferencesSettings
|
import com.russhwolf.settings.JvmPreferencesSettings
|
||||||
import com.russhwolf.settings.serialization.decodeValue
|
import com.russhwolf.settings.serialization.decodeValue
|
||||||
|
import com.russhwolf.settings.serialization.decodeValueOrNull
|
||||||
import com.russhwolf.settings.serialization.encodeValue
|
import com.russhwolf.settings.serialization.encodeValue
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.SerializationException
|
import kotlinx.serialization.SerializationException
|
||||||
import kotlinx.serialization.builtins.SetSerializer
|
import kotlinx.serialization.builtins.SetSerializer
|
||||||
import kotlinx.serialization.builtins.nullable
|
|
||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
import java.util.prefs.PreferenceChangeListener
|
import java.util.prefs.PreferenceChangeListener
|
||||||
import java.util.prefs.Preferences
|
import java.util.prefs.Preferences
|
||||||
@@ -40,13 +40,13 @@ class JavaSharedPreferences(key: String) : SharedPreferences {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getStringSet(key: String, defValues: MutableSet<String>?): MutableSet<String>? {
|
override fun getStringSet(key: String, defValues: Set<String>?): Set<String>? {
|
||||||
try {
|
try {
|
||||||
return if (defValues != null) {
|
return if (defValues != null) {
|
||||||
preferences.decodeValue(SetSerializer(String.serializer()).nullable, key, defValues)
|
preferences.decodeValue(SetSerializer(String.serializer()), key, defValues)
|
||||||
} else {
|
} else {
|
||||||
preferences.decodeValue(SetSerializer(String.serializer()).nullable, key, null)
|
preferences.decodeValueOrNull(SetSerializer(String.serializer()), key)
|
||||||
}?.toMutableSet()
|
}
|
||||||
} catch (e: SerializationException) {
|
} catch (e: SerializationException) {
|
||||||
throw ClassCastException("$key was not a StringSet")
|
throw ClassCastException("$key was not a StringSet")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import android.content.Context
|
|||||||
import androidx.preference.PreferenceScreen
|
import androidx.preference.PreferenceScreen
|
||||||
import eu.kanade.tachiyomi.source.ConfigurableSource
|
import eu.kanade.tachiyomi.source.ConfigurableSource
|
||||||
import eu.kanade.tachiyomi.source.getPreferenceKey
|
import eu.kanade.tachiyomi.source.getPreferenceKey
|
||||||
|
import io.javalin.plugin.json.JsonMapper
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import org.jetbrains.exposed.sql.select
|
import org.jetbrains.exposed.sql.select
|
||||||
import org.jetbrains.exposed.sql.selectAll
|
import org.jetbrains.exposed.sql.selectAll
|
||||||
@@ -81,11 +82,12 @@ object Source {
|
|||||||
private val context by DI.global.instance<CustomContext>()
|
private val context by DI.global.instance<CustomContext>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (2021-08) Clients should support these types for extensions to work properly
|
* (2021-11) Clients should support these types for extensions to work properly
|
||||||
* - EditTextPreference
|
* - EditTextPreference
|
||||||
* - SwitchPreferenceCompat
|
* - SwitchPreferenceCompat
|
||||||
* - ListPreference
|
* - ListPreference
|
||||||
* - CheckBoxPreference
|
* - CheckBoxPreference
|
||||||
|
* - MultiSelectListPreference
|
||||||
*/
|
*/
|
||||||
data class PreferenceObject(
|
data class PreferenceObject(
|
||||||
val type: String,
|
val type: String,
|
||||||
@@ -123,13 +125,18 @@ object Source {
|
|||||||
val value: String
|
val value: String
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private val jsonMapper by DI.global.instance<JsonMapper>()
|
||||||
|
|
||||||
|
@Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
|
||||||
fun setSourcePreference(sourceId: Long, change: SourcePreferenceChange) {
|
fun setSourcePreference(sourceId: Long, change: SourcePreferenceChange) {
|
||||||
val screen = preferenceScreenMap[sourceId]!!
|
val screen = preferenceScreenMap[sourceId]!!
|
||||||
val pref = screen.preferences[change.position]
|
val pref = screen.preferences[change.position]
|
||||||
|
|
||||||
|
println(jsonMapper::class.java.name)
|
||||||
val newValue = when (pref.defaultValueType) {
|
val newValue = when (pref.defaultValueType) {
|
||||||
"String" -> change.value
|
"String" -> change.value
|
||||||
"Boolean" -> change.value.toBoolean()
|
"Boolean" -> change.value.toBoolean()
|
||||||
|
"Set<String>" -> jsonMapper.fromJsonString(change.value, List::class.java as Class<List<String>>).toSet()
|
||||||
else -> throw RuntimeException("Unsupported type conversion")
|
else -> throw RuntimeException("Unsupported type conversion")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user