Improve sources settings
parent
9bd47e0410
commit
8a365250d9
@ -1,23 +1,28 @@
|
||||
package org.koitharu.kotatsu.core.prefs
|
||||
|
||||
import android.content.Context
|
||||
import org.koitharu.kotatsu.parsers.MangaSourceConfig
|
||||
import androidx.core.content.edit
|
||||
import org.koitharu.kotatsu.parsers.config.ConfigKey
|
||||
import org.koitharu.kotatsu.parsers.config.MangaSourceConfig
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
import org.koitharu.kotatsu.parsers.model.SortOrder
|
||||
import org.koitharu.kotatsu.utils.ext.getEnumValue
|
||||
import org.koitharu.kotatsu.utils.ext.putEnumValue
|
||||
|
||||
private const val KEY_SORT_ORDER = "sort_order"
|
||||
|
||||
class SourceSettings(context: Context, source: MangaSource) : MangaSourceConfig {
|
||||
|
||||
private val prefs = context.getSharedPreferences(source.name, Context.MODE_PRIVATE)
|
||||
|
||||
override fun getDomain(defaultValue: String) = prefs.getString(KEY_DOMAIN, defaultValue)
|
||||
?.takeUnless(String::isBlank)
|
||||
?: defaultValue
|
||||
|
||||
override fun isSslEnabled(defaultValue: Boolean) = prefs.getBoolean(KEY_USE_SSL, defaultValue)
|
||||
|
||||
companion object {
|
||||
var defaultSortOrder: SortOrder?
|
||||
get() = prefs.getEnumValue(KEY_SORT_ORDER, SortOrder::class.java)
|
||||
set(value) = prefs.edit { putEnumValue(KEY_SORT_ORDER, value) }
|
||||
|
||||
const val KEY_DOMAIN = "domain"
|
||||
const val KEY_USE_SSL = "ssl"
|
||||
const val KEY_AUTH = "auth"
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T> get(key: ConfigKey<T>): T {
|
||||
return when (key) {
|
||||
is ConfigKey.Domain -> prefs.getString(key.key, key.defaultValue) ?: key.defaultValue
|
||||
} as T
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package org.koitharu.kotatsu.settings
|
||||
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import androidx.preference.*
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
||||
import org.koitharu.kotatsu.parsers.config.ConfigKey
|
||||
import org.koitharu.kotatsu.settings.utils.EditTextBindListener
|
||||
import org.koitharu.kotatsu.settings.utils.EditTextDefaultSummaryProvider
|
||||
import org.koitharu.kotatsu.utils.ext.setDefaultValueCompat
|
||||
|
||||
private const val KEY_DOMAIN = "domain"
|
||||
|
||||
fun PreferenceFragmentCompat.addPreferencesFromRepository(repository: RemoteMangaRepository) {
|
||||
val configKeys = repository.getConfigKeys()
|
||||
val screen = preferenceScreen
|
||||
for (key in configKeys) {
|
||||
val preference: Preference = when (key) {
|
||||
is ConfigKey.Domain -> {
|
||||
val presetValues = key.presetValues
|
||||
if (presetValues.isNullOrEmpty()) {
|
||||
EditTextPreference(requireContext()).apply {
|
||||
summaryProvider = EditTextDefaultSummaryProvider(key.defaultValue)
|
||||
setOnBindEditTextListener(
|
||||
EditTextBindListener(
|
||||
inputType = EditorInfo.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_URI,
|
||||
hint = key.defaultValue,
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
DropDownPreference(requireContext()).apply {
|
||||
entries = presetValues
|
||||
entryValues = entries
|
||||
summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()
|
||||
setDefaultValueCompat(key.defaultValue)
|
||||
}
|
||||
}.apply {
|
||||
setTitle(R.string.domain)
|
||||
setDialogTitle(R.string.domain)
|
||||
}
|
||||
}
|
||||
}
|
||||
preference.isIconSpaceReserved = false
|
||||
preference.key = key.key
|
||||
screen.addPreference(preference)
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,25 @@
|
||||
package org.koitharu.kotatsu.utils.ext
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import androidx.preference.ListPreference
|
||||
|
||||
fun ListPreference.setDefaultValueCompat(defaultValue: String) {
|
||||
if (value == null) {
|
||||
value = defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
fun <E: Enum<E>> SharedPreferences.getEnumValue(key: String, enumClass: Class<E>): E? {
|
||||
val stringValue = getString(key, null) ?: return null
|
||||
return enumClass.enumConstants?.find {
|
||||
it.name == stringValue
|
||||
}
|
||||
}
|
||||
|
||||
fun <E: Enum<E>> SharedPreferences.getEnumValue(key: String, defaultValue: E): E {
|
||||
return getEnumValue(key, defaultValue.javaClass) ?: defaultValue
|
||||
}
|
||||
|
||||
fun <E: Enum<E>> SharedPreferences.Editor.putEnumValue(key: String, value: E?) {
|
||||
putString(key, value?.name)
|
||||
}
|
||||
Loading…
Reference in New Issue