mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
Remove use of dialog controllers in ext details
Also make the keyboard show when opening edit text reset preference
This commit is contained in:
parent
0cf8bbf29c
commit
7e8196e9d1
5 changed files with 123 additions and 36 deletions
|
@ -12,11 +12,8 @@ import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.preference.DialogPreference
|
import androidx.preference.DialogPreference
|
||||||
import androidx.preference.EditTextPreference
|
import androidx.preference.EditTextPreference
|
||||||
import androidx.preference.EditTextPreferenceDialogController
|
|
||||||
import androidx.preference.ListPreference
|
import androidx.preference.ListPreference
|
||||||
import androidx.preference.ListPreferenceDialogController
|
|
||||||
import androidx.preference.MultiSelectListPreference
|
import androidx.preference.MultiSelectListPreference
|
||||||
import androidx.preference.MultiSelectListPreferenceDialogController
|
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
import androidx.preference.PreferenceGroupAdapter
|
import androidx.preference.PreferenceGroupAdapter
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
|
@ -39,6 +36,7 @@ import eu.kanade.tachiyomi.source.preferenceKey
|
||||||
import eu.kanade.tachiyomi.source.sourcePreferences
|
import eu.kanade.tachiyomi.source.sourcePreferences
|
||||||
import eu.kanade.tachiyomi.ui.base.controller.BaseCoroutineController
|
import eu.kanade.tachiyomi.ui.base.controller.BaseCoroutineController
|
||||||
import eu.kanade.tachiyomi.ui.setting.DSL
|
import eu.kanade.tachiyomi.ui.setting.DSL
|
||||||
|
import eu.kanade.tachiyomi.ui.setting.defaultValue
|
||||||
import eu.kanade.tachiyomi.ui.setting.onChange
|
import eu.kanade.tachiyomi.ui.setting.onChange
|
||||||
import eu.kanade.tachiyomi.ui.setting.switchPreference
|
import eu.kanade.tachiyomi.ui.setting.switchPreference
|
||||||
import eu.kanade.tachiyomi.util.system.LocaleHelper
|
import eu.kanade.tachiyomi.util.system.LocaleHelper
|
||||||
|
@ -48,6 +46,9 @@ import eu.kanade.tachiyomi.util.view.scrollViewWith
|
||||||
import eu.kanade.tachiyomi.util.view.snack
|
import eu.kanade.tachiyomi.util.view.snack
|
||||||
import eu.kanade.tachiyomi.widget.LinearLayoutManagerAccurateOffset
|
import eu.kanade.tachiyomi.widget.LinearLayoutManagerAccurateOffset
|
||||||
import eu.kanade.tachiyomi.widget.TachiyomiTextInputEditText.Companion.setIncognito
|
import eu.kanade.tachiyomi.widget.TachiyomiTextInputEditText.Companion.setIncognito
|
||||||
|
import eu.kanade.tachiyomi.widget.preference.EditTextResetPreference
|
||||||
|
import eu.kanade.tachiyomi.widget.preference.ListMatPreference
|
||||||
|
import eu.kanade.tachiyomi.widget.preference.MultiListMatPreference
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
|
@ -326,23 +327,53 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
|
||||||
screen.getPreference(it) === preference
|
screen.getPreference(it) === preference
|
||||||
}
|
}
|
||||||
|
|
||||||
val f = when (preference) {
|
val context = preferences.context
|
||||||
is EditTextPreference ->
|
val matPref = when (preference) {
|
||||||
EditTextPreferenceDialogController
|
is EditTextPreference -> EditTextResetPreference(activity, context).apply {
|
||||||
.newInstance(preference.getKey())
|
dialogSummary = preference.dialogMessage
|
||||||
is ListPreference ->
|
onPreferenceChangeListener = preference.onPreferenceChangeListener
|
||||||
ListPreferenceDialogController
|
}
|
||||||
.newInstance(preference.getKey())
|
|
||||||
is MultiSelectListPreference ->
|
is ListPreference -> ListMatPreference(activity, context).apply {
|
||||||
MultiSelectListPreferenceDialogController
|
isPersistent = false
|
||||||
.newInstance(preference.getKey())
|
defaultValue = preference.value
|
||||||
|
entries = preference.entries.map { it.toString() }
|
||||||
|
entryValues = preference.entryValues.map { it.toString() }
|
||||||
|
onChange {
|
||||||
|
if (preference.callChangeListener(it)) {
|
||||||
|
preference.value = it as? String
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
is MultiSelectListPreference -> MultiListMatPreference(activity, context).apply {
|
||||||
|
isPersistent = false
|
||||||
|
defaultValue = preference.values
|
||||||
|
entries = preference.entries.map { it.toString() }
|
||||||
|
entryValues = preference.entryValues.map { it.toString() }
|
||||||
|
onChange { newValue ->
|
||||||
|
if (newValue is Set<*> && preference.callChangeListener(newValue)) {
|
||||||
|
preference.values = newValue.map { it.toString() }.toSet()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
else -> throw IllegalArgumentException(
|
else -> throw IllegalArgumentException(
|
||||||
"Tried to display dialog for unknown " +
|
"Tried to display dialog for unknown " +
|
||||||
"preference type. Did you forget to override onDisplayPreferenceDialog()?",
|
"preference type. Did you forget to override onDisplayPreferenceDialog()?",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
f.targetController = this
|
matPref.apply {
|
||||||
f.showDialog(router)
|
key = preference.key
|
||||||
|
preferenceDataStore = preference.preferenceDataStore
|
||||||
|
title = (preference as? DialogPreference)?.dialogTitle ?: preference.title
|
||||||
|
}.performClick()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Source.isEnabled(): Boolean {
|
private fun Source.isEnabled(): Boolean {
|
||||||
|
|
|
@ -2,12 +2,17 @@ package eu.kanade.tachiyomi.widget.preference
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.DialogInterface
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
|
import android.view.WindowManager
|
||||||
|
import android.view.inputmethod.InputMethodManager
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
|
import androidx.preference.Preference.SummaryProvider
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
|
|
||||||
|
@ -18,12 +23,19 @@ class EditTextResetPreference @JvmOverloads constructor(
|
||||||
) :
|
) :
|
||||||
MatPreference(activity, context, attrs) {
|
MatPreference(activity, context, attrs) {
|
||||||
private var defValue: String = ""
|
private var defValue: String = ""
|
||||||
|
var dialogSummary: CharSequence? = null
|
||||||
|
|
||||||
override fun onSetInitialValue(defaultValue: Any?) {
|
override fun onSetInitialValue(defaultValue: Any?) {
|
||||||
super.onSetInitialValue(defaultValue)
|
super.onSetInitialValue(defaultValue)
|
||||||
defValue = defaultValue as? String ?: defValue
|
defValue = defaultValue as? String ?: defValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun didShow(dialog: DialogInterface) {
|
||||||
|
val textView = (dialog as? AlertDialog)?.findViewById<EditText>(android.R.id.edit) ?: return
|
||||||
|
textView.requestFocus()
|
||||||
|
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||||
|
}
|
||||||
|
|
||||||
override fun setDefaultValue(defaultValue: Any?) {
|
override fun setDefaultValue(defaultValue: Any?) {
|
||||||
super.setDefaultValue(defaultValue)
|
super.setDefaultValue(defaultValue)
|
||||||
defValue = defaultValue as? String ?: defValue
|
defValue = defaultValue as? String ?: defValue
|
||||||
|
@ -41,22 +53,43 @@ class EditTextResetPreference @JvmOverloads constructor(
|
||||||
val view = LayoutInflater.from(context).inflate(resourceId, null)
|
val view = LayoutInflater.from(context).inflate(resourceId, null)
|
||||||
val textView = view.findViewById<EditText>(android.R.id.edit)
|
val textView = view.findViewById<EditText>(android.R.id.edit)
|
||||||
val message = view.findViewById<TextView>(android.R.id.message)
|
val message = view.findViewById<TextView>(android.R.id.message)
|
||||||
message?.isVisible = false
|
message?.isVisible = dialogSummary != null
|
||||||
textView?.append(sharedPreferences?.getString(key, defValue))
|
message?.text = dialogSummary
|
||||||
|
textView?.append(
|
||||||
|
preferenceDataStore?.getString(key, defValue)
|
||||||
|
?: sharedPreferences?.getString(key, defValue),
|
||||||
|
)
|
||||||
|
val inputMethodManager: InputMethodManager =
|
||||||
|
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
|
inputMethodManager.showSoftInput(textView, WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
|
||||||
|
// Place cursor at the end
|
||||||
|
textView.setSelection(textView.text.length)
|
||||||
this.setView(view)
|
this.setView(view)
|
||||||
this.setNeutralButton(R.string.reset) { _, _ ->
|
this.setNeutralButton(R.string.reset) { _, _ ->
|
||||||
if (callChangeListener(defValue)) {
|
if (callChangeListener(defValue)) {
|
||||||
sharedPreferences?.edit { remove(key) }
|
if (preferenceDataStore != null) {
|
||||||
|
preferenceDataStore?.putString(key, null)
|
||||||
|
} else {
|
||||||
|
sharedPreferences?.edit { remove(key) }
|
||||||
|
}
|
||||||
notifyChanged()
|
notifyChanged()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.setPositiveButton(android.R.string.ok) { _, _ ->
|
this.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||||
if (callChangeListener(textView.text.toString())) {
|
if (callChangeListener(textView.text.toString())) {
|
||||||
sharedPreferences?.edit {
|
if (preferenceDataStore != null) {
|
||||||
if (textView.text.isNullOrBlank()) {
|
if (textView.text.isNullOrBlank()) {
|
||||||
remove(key)
|
preferenceDataStore?.putString(key, null)
|
||||||
} else {
|
} else {
|
||||||
putString(key, textView.text.toString())
|
preferenceDataStore?.putString(key, textView.text.toString())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sharedPreferences?.edit {
|
||||||
|
if (textView.text.isNullOrBlank()) {
|
||||||
|
remove(key)
|
||||||
|
} else {
|
||||||
|
putString(key, textView.text.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
notifyChanged()
|
notifyChanged()
|
||||||
|
|
|
@ -29,10 +29,16 @@ open class ListMatPreference @JvmOverloads constructor(
|
||||||
defValue = defaultValue as? String ?: defValue
|
defValue = defaultValue as? String ?: defValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun setDefaultValue(defaultValue: Any?) {
|
||||||
|
super.setDefaultValue(defaultValue)
|
||||||
|
defValue = defaultValue as? String ?: defValue
|
||||||
|
}
|
||||||
|
|
||||||
private val indexOfPref: Int
|
private val indexOfPref: Int
|
||||||
get() = tempValue ?: entryValues.indexOf(
|
get() = tempValue ?: entryValues.indexOf(
|
||||||
if (isPersistent) {
|
if (isPersistent || preferenceDataStore != null) {
|
||||||
sharedPreferences?.getString(key, defValue)
|
preferenceDataStore?.getString(key, defValue)
|
||||||
|
?: sharedPreferences?.getString(key, defValue)
|
||||||
} else {
|
} else {
|
||||||
tempEntry
|
tempEntry
|
||||||
} ?: defValue,
|
} ?: defValue,
|
||||||
|
@ -58,15 +64,21 @@ open class ListMatPreference @JvmOverloads constructor(
|
||||||
val default = indexOfPref
|
val default = indexOfPref
|
||||||
setSingleChoiceItems(entries.toTypedArray(), default) { dialog, pos ->
|
setSingleChoiceItems(entries.toTypedArray(), default) { dialog, pos ->
|
||||||
val value = entryValues[pos]
|
val value = entryValues[pos]
|
||||||
if (isPersistent) {
|
if (callChangeListener(value)) {
|
||||||
sharedPreferences?.edit { putString(key, value) }
|
if (isPersistent) {
|
||||||
} else {
|
if (preferenceDataStore != null) {
|
||||||
tempValue = pos
|
preferenceDataStore?.putString(key, value)
|
||||||
tempEntry = entries.getOrNull(pos)
|
notifyChanged()
|
||||||
notifyChanged()
|
} else {
|
||||||
|
sharedPreferences?.edit { putString(key, value) }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tempValue = pos
|
||||||
|
tempEntry = entries.getOrNull(pos)
|
||||||
|
notifyChanged()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this@ListMatPreference.summary = this@ListMatPreference.summary
|
this@ListMatPreference.summary = this@ListMatPreference.summary
|
||||||
callChangeListener(value)
|
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.widget.preference
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.DialogInterface
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
@ -36,15 +37,17 @@ open class MatPreference @JvmOverloads constructor(
|
||||||
val dialog = dialog().apply {
|
val dialog = dialog().apply {
|
||||||
setOnDismissListener { this@MatPreference.isShowing = false }
|
setOnDismissListener { this@MatPreference.isShowing = false }
|
||||||
}.create()
|
}.create()
|
||||||
// dialog.setOnShowListener {
|
|
||||||
onShow(dialog)
|
onShow(dialog)
|
||||||
// }
|
dialog.setOnShowListener {
|
||||||
|
didShow(it)
|
||||||
|
}
|
||||||
dialog.show()
|
dialog.show()
|
||||||
}
|
}
|
||||||
isShowing = true
|
isShowing = true
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun onShow(dialog: AlertDialog) { }
|
protected open fun onShow(dialog: AlertDialog) { }
|
||||||
|
protected open fun didShow(dialog: DialogInterface) { }
|
||||||
|
|
||||||
protected open var customSummaryProvider: SummaryProvider<MatPreference>? = null
|
protected open var customSummaryProvider: SummaryProvider<MatPreference>? = null
|
||||||
set(value) {
|
set(value) {
|
||||||
|
|
|
@ -64,7 +64,8 @@ class MultiListMatPreference @JvmOverloads constructor(
|
||||||
|
|
||||||
@SuppressLint("CheckResult")
|
@SuppressLint("CheckResult")
|
||||||
override fun MaterialAlertDialogBuilder.setListItems() {
|
override fun MaterialAlertDialogBuilder.setListItems() {
|
||||||
val set = sharedPreferences?.getStringSet(key, defValue) ?: defValue
|
val set = preferenceDataStore?.getStringSet(key, defValue)
|
||||||
|
?: sharedPreferences?.getStringSet(key, defValue) ?: defValue
|
||||||
val items = if (allSelectionRes != null) {
|
val items = if (allSelectionRes != null) {
|
||||||
if (showAllLast) {
|
if (showAllLast) {
|
||||||
entries + listOf(context.getString(allSelectionRes!!))
|
entries + listOf(context.getString(allSelectionRes!!))
|
||||||
|
@ -88,9 +89,16 @@ class MultiListMatPreference @JvmOverloads constructor(
|
||||||
var value = pos.mapNotNull {
|
var value = pos.mapNotNull {
|
||||||
entryValues.getOrNull(it - if (allSelectionRes != null && !showAllLast) 1 else 0)
|
entryValues.getOrNull(it - if (allSelectionRes != null && !showAllLast) 1 else 0)
|
||||||
}.toSet()
|
}.toSet()
|
||||||
if (allSelectionRes != null && !allIsAlwaysSelected && selected[allPos]) value = emptySet()
|
if (allSelectionRes != null && !allIsAlwaysSelected && selected[allPos]) {
|
||||||
sharedPreferences?.edit { putStringSet(key, value) }
|
value = emptySet()
|
||||||
callChangeListener(value)
|
}
|
||||||
|
if (callChangeListener(value) && isPersistent) {
|
||||||
|
if (preferenceDataStore != null) {
|
||||||
|
preferenceDataStore?.putStringSet(key, value)
|
||||||
|
} else if (preferenceDataStore == null) {
|
||||||
|
sharedPreferences?.edit { putStringSet(key, value) }
|
||||||
|
}
|
||||||
|
}
|
||||||
notifyChanged()
|
notifyChanged()
|
||||||
}
|
}
|
||||||
setMultiChoiceItems(items.toTypedArray(), selected) { dialog, pos, checked ->
|
setMultiChoiceItems(items.toTypedArray(), selected) { dialog, pos, checked ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue