mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-07-03 10:54:38 -05:00
refine extension preferences API
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
package androidx.preference;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) Contributors to the Suwayomi project
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* 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.content.Context;
|
||||||
|
|
||||||
|
public class CheckBoxPreference extends Preference {
|
||||||
|
// reference: https://android.googlesource.com/platform/frameworks/support/+/996971f962fcd554339a7cb2859cef9ca89dbcb7/preference/preference/src/main/java/androidx/preference/CheckBoxPreference.java
|
||||||
|
|
||||||
|
public CheckBoxPreference(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,18 @@
|
|||||||
package androidx.preference;
|
package androidx.preference;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) Contributors to the Suwayomi project
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* 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.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
public class EditTextPreference extends Preference {
|
public class EditTextPreference extends Preference {
|
||||||
// reference: https://android.googlesource.com/platform/frameworks/support/+/996971f962fcd554339a7cb2859cef9ca89dbcb7/preference/preference/src/main/java/androidx/preference/EditTextPreference.java
|
// reference: https://android.googlesource.com/platform/frameworks/support/+/996971f962fcd554339a7cb2859cef9ca89dbcb7/preference/preference/src/main/java/androidx/preference/EditTextPreference.java
|
||||||
|
|
||||||
private String title;
|
|
||||||
private String text;
|
private String text;
|
||||||
private CharSequence summary;
|
|
||||||
private CharSequence dialogTitle;
|
private CharSequence dialogTitle;
|
||||||
private CharSequence dialogMessage;
|
private CharSequence dialogMessage;
|
||||||
|
|
||||||
@@ -15,22 +20,6 @@ public class EditTextPreference extends Preference {
|
|||||||
super(context);
|
super(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTitle(CharSequence title) {
|
|
||||||
this.title = (String) title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CharSequence getSummary() {
|
|
||||||
return summary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSummary(CharSequence summary) {
|
|
||||||
this.summary = summary;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CharSequence getDialogTitle() {
|
public CharSequence getDialogTitle() {
|
||||||
return dialogTitle;
|
return dialogTitle;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ public class Preference {
|
|||||||
|
|
||||||
private String key;
|
private String key;
|
||||||
private CharSequence title;
|
private CharSequence title;
|
||||||
|
private CharSequence summary;
|
||||||
private Object defaultValue;
|
private Object defaultValue;
|
||||||
|
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
@@ -41,14 +42,26 @@ public class Preference {
|
|||||||
this.key = key;
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDefaultValue(Object defaultValue) {
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CharSequence getTitle() {
|
public CharSequence getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setTitle(CharSequence title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CharSequence getSummary() {
|
||||||
|
return summary;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSummary(CharSequence summary) {
|
||||||
|
this.summary = summary;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefaultValue(Object defaultValue) {
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
public void setOnPreferenceChangeListener(OnPreferenceChangeListener onPreferenceChangeListener) {
|
public void setOnPreferenceChangeListener(OnPreferenceChangeListener onPreferenceChangeListener) {
|
||||||
this.onChangeListener = onPreferenceChangeListener;
|
this.onChangeListener = onPreferenceChangeListener;
|
||||||
}
|
}
|
||||||
@@ -61,6 +74,10 @@ public class Preference {
|
|||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getDefaultValueType() {
|
||||||
|
return defaultValue.getClass().getSimpleName();
|
||||||
|
}
|
||||||
|
|
||||||
public interface OnPreferenceChangeListener {
|
public interface OnPreferenceChangeListener {
|
||||||
boolean onPreferenceChange(Preference preference, Object newValue);
|
boolean onPreferenceChange(Preference preference, Object newValue);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,13 +113,13 @@ object MangaAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fetch preferences of source with id `sourceId`
|
// fetch preferences of source with id `sourceId`
|
||||||
app.get("/api/v1/source/:sourceId/preference") { ctx ->
|
app.get("/api/v1/source/:sourceId/preferences") { ctx ->
|
||||||
val sourceId = ctx.pathParam("sourceId").toLong()
|
val sourceId = ctx.pathParam("sourceId").toLong()
|
||||||
ctx.json(getSourcePreferences(sourceId))
|
ctx.json(getSourcePreferences(sourceId))
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch preferences of source with id `sourceId`
|
// fetch preferences of source with id `sourceId`
|
||||||
app.post("/api/v1/source/:sourceId/preference") { ctx ->
|
app.post("/api/v1/source/:sourceId/preferences") { ctx ->
|
||||||
val sourceId = ctx.pathParam("sourceId").toLong()
|
val sourceId = ctx.pathParam("sourceId").toLong()
|
||||||
val preferenceChange = ctx.bodyAsClass(SourcePreferenceChange::class.java)
|
val preferenceChange = ctx.bodyAsClass(SourcePreferenceChange::class.java)
|
||||||
ctx.json(setSourcePreference(sourceId, preferenceChange))
|
ctx.json(setSourcePreference(sourceId, preferenceChange))
|
||||||
|
|||||||
@@ -31,12 +31,12 @@ object Source {
|
|||||||
return transaction {
|
return transaction {
|
||||||
SourceTable.selectAll().map {
|
SourceTable.selectAll().map {
|
||||||
SourceDataClass(
|
SourceDataClass(
|
||||||
it[SourceTable.id].value.toString(),
|
it[SourceTable.id].value.toString(),
|
||||||
it[SourceTable.name],
|
it[SourceTable.name],
|
||||||
it[SourceTable.lang],
|
it[SourceTable.lang],
|
||||||
getExtensionIconUrl(ExtensionTable.select { ExtensionTable.id eq it[SourceTable.extension] }.first()[ExtensionTable.apkName]),
|
getExtensionIconUrl(ExtensionTable.select { ExtensionTable.id eq it[SourceTable.extension] }.first()[ExtensionTable.apkName]),
|
||||||
getHttpSource(it[SourceTable.id].value).supportsLatest,
|
getHttpSource(it[SourceTable.id].value).supportsLatest,
|
||||||
getHttpSource(it[SourceTable.id].value) is ConfigurableSource
|
getHttpSource(it[SourceTable.id].value) is ConfigurableSource
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,12 +47,12 @@ object Source {
|
|||||||
val source = SourceTable.select { SourceTable.id eq sourceId }.firstOrNull()
|
val source = SourceTable.select { SourceTable.id eq sourceId }.firstOrNull()
|
||||||
|
|
||||||
SourceDataClass(
|
SourceDataClass(
|
||||||
sourceId.toString(),
|
sourceId.toString(),
|
||||||
source?.get(SourceTable.name),
|
source?.get(SourceTable.name),
|
||||||
source?.get(SourceTable.lang),
|
source?.get(SourceTable.lang),
|
||||||
source?.let { ExtensionTable.select { ExtensionTable.id eq source[SourceTable.extension] }.first()[ExtensionTable.iconUrl] },
|
source?.let { ExtensionTable.select { ExtensionTable.id eq source[SourceTable.extension] }.first()[ExtensionTable.iconUrl] },
|
||||||
source?.let { getHttpSource(sourceId).supportsLatest },
|
source?.let { getHttpSource(sourceId).supportsLatest },
|
||||||
source?.let { getHttpSource(sourceId) is ConfigurableSource },
|
source?.let { getHttpSource(sourceId) is ConfigurableSource },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,8 +60,8 @@ object Source {
|
|||||||
private val context by DI.global.instance<CustomContext>()
|
private val context by DI.global.instance<CustomContext>()
|
||||||
|
|
||||||
data class PreferenceObject(
|
data class PreferenceObject(
|
||||||
val type: String,
|
val type: String,
|
||||||
val props: Any
|
val props: Any
|
||||||
)
|
)
|
||||||
|
|
||||||
var preferenceScreenMap: MutableMap<Long, PreferenceScreen> = mutableMapOf()
|
var preferenceScreenMap: MutableMap<Long, PreferenceScreen> = mutableMapOf()
|
||||||
@@ -80,22 +80,22 @@ object Source {
|
|||||||
preferenceScreenMap[sourceId] = screen
|
preferenceScreenMap[sourceId] = screen
|
||||||
|
|
||||||
return screen.preferences.map {
|
return screen.preferences.map {
|
||||||
PreferenceObject(it::class.java.name, it)
|
PreferenceObject(it::class.java.simpleName, it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return emptyList()
|
return emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
data class SourcePreferenceChange(
|
data class SourcePreferenceChange(
|
||||||
val position: Int,
|
val position: Int,
|
||||||
val type: String,
|
val value: String
|
||||||
val value: String
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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 newValue = when(change.type) {
|
val newValue = when (pref.defaultValueType) {
|
||||||
"String" -> change.value
|
"String" -> change.value
|
||||||
"Int" -> change.value.toInt()
|
"Int" -> change.value.toInt()
|
||||||
"Long" -> change.value.toLong()
|
"Long" -> change.value.toLong()
|
||||||
@@ -105,7 +105,7 @@ object Source {
|
|||||||
else -> throw RuntimeException("Unsupported type conversion")
|
else -> throw RuntimeException("Unsupported type conversion")
|
||||||
}
|
}
|
||||||
|
|
||||||
screen.preferences[change.position].callChangeListener(newValue)
|
pref.callChangeListener(newValue)
|
||||||
|
|
||||||
// must reload the source cache because a preference was changed
|
// must reload the source cache because a preference was changed
|
||||||
invalidateSourceCache(sourceId)
|
invalidateSourceCache(sourceId)
|
||||||
|
|||||||
Reference in New Issue
Block a user