refine extension preferences API

This commit is contained in:
Aria Moradi
2021-07-31 07:24:45 +04:30
parent 75f635a28b
commit dadb686514
5 changed files with 69 additions and 45 deletions
@@ -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;
/*
* 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 EditTextPreference extends Preference {
// 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 CharSequence summary;
private CharSequence dialogTitle;
private CharSequence dialogMessage;
@@ -15,22 +20,6 @@ public class EditTextPreference extends Preference {
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() {
return dialogTitle;
}
@@ -20,6 +20,7 @@ public class Preference {
private String key;
private CharSequence title;
private CharSequence summary;
private Object defaultValue;
@JsonIgnore
@@ -41,14 +42,26 @@ public class Preference {
this.key = key;
}
public void setDefaultValue(Object defaultValue) {
this.defaultValue = defaultValue;
}
public CharSequence getTitle() {
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) {
this.onChangeListener = onPreferenceChangeListener;
}
@@ -61,6 +74,10 @@ public class Preference {
return defaultValue;
}
public String getDefaultValueType() {
return defaultValue.getClass().getSimpleName();
}
public interface OnPreferenceChangeListener {
boolean onPreferenceChange(Preference preference, Object newValue);
}
@@ -113,13 +113,13 @@ object MangaAPI {
}
// 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()
ctx.json(getSourcePreferences(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 preferenceChange = ctx.bodyAsClass(SourcePreferenceChange::class.java)
ctx.json(setSourcePreference(sourceId, preferenceChange))
@@ -31,12 +31,12 @@ object Source {
return transaction {
SourceTable.selectAll().map {
SourceDataClass(
it[SourceTable.id].value.toString(),
it[SourceTable.name],
it[SourceTable.lang],
getExtensionIconUrl(ExtensionTable.select { ExtensionTable.id eq it[SourceTable.extension] }.first()[ExtensionTable.apkName]),
getHttpSource(it[SourceTable.id].value).supportsLatest,
getHttpSource(it[SourceTable.id].value) is ConfigurableSource
it[SourceTable.id].value.toString(),
it[SourceTable.name],
it[SourceTable.lang],
getExtensionIconUrl(ExtensionTable.select { ExtensionTable.id eq it[SourceTable.extension] }.first()[ExtensionTable.apkName]),
getHttpSource(it[SourceTable.id].value).supportsLatest,
getHttpSource(it[SourceTable.id].value) is ConfigurableSource
)
}
}
@@ -47,12 +47,12 @@ object Source {
val source = SourceTable.select { SourceTable.id eq sourceId }.firstOrNull()
SourceDataClass(
sourceId.toString(),
source?.get(SourceTable.name),
source?.get(SourceTable.lang),
source?.let { ExtensionTable.select { ExtensionTable.id eq source[SourceTable.extension] }.first()[ExtensionTable.iconUrl] },
source?.let { getHttpSource(sourceId).supportsLatest },
source?.let { getHttpSource(sourceId) is ConfigurableSource },
sourceId.toString(),
source?.get(SourceTable.name),
source?.get(SourceTable.lang),
source?.let { ExtensionTable.select { ExtensionTable.id eq source[SourceTable.extension] }.first()[ExtensionTable.iconUrl] },
source?.let { getHttpSource(sourceId).supportsLatest },
source?.let { getHttpSource(sourceId) is ConfigurableSource },
)
}
}
@@ -60,8 +60,8 @@ object Source {
private val context by DI.global.instance<CustomContext>()
data class PreferenceObject(
val type: String,
val props: Any
val type: String,
val props: Any
)
var preferenceScreenMap: MutableMap<Long, PreferenceScreen> = mutableMapOf()
@@ -80,22 +80,22 @@ object Source {
preferenceScreenMap[sourceId] = screen
return screen.preferences.map {
PreferenceObject(it::class.java.name, it)
PreferenceObject(it::class.java.simpleName, it)
}
}
return emptyList()
}
data class SourcePreferenceChange(
val position: Int,
val type: String,
val value: String
val position: Int,
val value: String
)
fun setSourcePreference(sourceId: Long, change: SourcePreferenceChange ) {
fun setSourcePreference(sourceId: Long, change: SourcePreferenceChange) {
val screen = preferenceScreenMap[sourceId]!!
val pref = screen.preferences[change.position]
val newValue = when(change.type) {
val newValue = when (pref.defaultValueType) {
"String" -> change.value
"Int" -> change.value.toInt()
"Long" -> change.value.toLong()
@@ -105,7 +105,7 @@ object Source {
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
invalidateSourceCache(sourceId)