implement data store for extension prefs

This commit is contained in:
Aria Moradi
2021-08-06 03:37:09 +04:30
parent 53cc73701c
commit afabaccf1d
4 changed files with 54 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ package androidx.preference;
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import android.content.Context;
import android.content.SharedPreferences;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
@@ -25,6 +26,10 @@ public class Preference {
private CharSequence summary;
private Object defaultValue;
/** Tachidesk specific API */
@JsonIgnore
private SharedPreferences sharedPreferences;
@JsonIgnore
public OnPreferenceChangeListener onChangeListener;
@@ -89,6 +94,16 @@ public class Preference {
return defaultValue.getClass().getSimpleName();
}
/** Tachidesk specific API */
public SharedPreferences getSharedPreferences() {
return sharedPreferences;
}
/** Tachidesk specific API */
public void setSharedPreferences(SharedPreferences sharedPreferences) {
this.sharedPreferences = sharedPreferences;
}
public interface OnPreferenceChangeListener {
boolean onPreferenceChange(Preference preference, Object newValue);
}
@@ -96,4 +111,30 @@ public class Preference {
public interface OnPreferenceClickListener {
boolean onPreferenceClick(Preference preference);
}
/** Tachidesk specific API */
public Object getCurrentValue() {
switch (getDefaultValueType()) {
case "String":
return sharedPreferences.getString(key, (String)defaultValue);
case "Boolean":
return sharedPreferences.getBoolean(key, (Boolean)defaultValue);
default:
throw new RuntimeException("Unsupported type");
}
}
/** Tachidesk specific API */
public void saveNewValue(Object value) {
switch (getDefaultValueType()) {
case "String":
sharedPreferences.edit().putString(key, (String)value).apply();
break;
case "Boolean":
sharedPreferences.edit().putBoolean(key, (Boolean)value).apply();
break;
default:
throw new RuntimeException("Unsupported type");
}
}
}

View File

@@ -21,6 +21,9 @@ public class PreferenceScreen extends Preference {
}
public boolean addPreference(Preference preference) {
// propagate own shared preferences
preference.setSharedPreferences(getSharedPreferences());
preferences.add(preference);
return true;

View File

@@ -44,4 +44,4 @@ interface Source {
// fun Source.icon(): Drawable? = Injekt.get<ExtensionManager>().getAppIconForSource(this)
// fun Source.getPreferenceKey(): String = "source_$id"
fun Source.getPreferenceKey(): String = "source_$id"

View File

@@ -7,8 +7,11 @@ package suwayomi.tachidesk.manga.impl
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import android.app.Application
import android.content.Context
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.source.ConfigurableSource
import eu.kanade.tachiyomi.source.getPreferenceKey
import mu.KotlinLogging
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
@@ -22,6 +25,8 @@ import suwayomi.tachidesk.manga.impl.util.GetHttpSource.invalidateSourceCache
import suwayomi.tachidesk.manga.model.dataclass.SourceDataClass
import suwayomi.tachidesk.manga.model.table.ExtensionTable
import suwayomi.tachidesk.manga.model.table.SourceTable
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import xyz.nulldev.androidcompat.androidimpl.CustomContext
object Source {
@@ -80,7 +85,10 @@ object Source {
val source = getHttpSource(sourceId)
if (source is ConfigurableSource) {
val sourceShardPreferences = Injekt.get<Application>().getSharedPreferences(source.getPreferenceKey(), Context.MODE_PRIVATE)
val screen = PreferenceScreen(context)
screen.sharedPreferences = sourceShardPreferences
source.setupPreferenceScreen(screen)
@@ -112,6 +120,7 @@ object Source {
else -> throw RuntimeException("Unsupported type conversion")
}
pref.saveNewValue(newValue)
pref.callChangeListener(newValue)
// must reload the source cache because a preference was changed