Remove use of dialog controllers in ext details

Also make the keyboard show when opening edit text reset preference
This commit is contained in:
Jays2Kings 2023-11-05 18:13:40 -08:00
parent 0cf8bbf29c
commit 7e8196e9d1
5 changed files with 123 additions and 36 deletions

View file

@ -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 {

View file

@ -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,17 +53,37 @@ 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)) {
if (preferenceDataStore != null) {
preferenceDataStore?.putString(key, null)
} else {
sharedPreferences?.edit { remove(key) } 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())) {
if (preferenceDataStore != null) {
if (textView.text.isNullOrBlank()) {
preferenceDataStore?.putString(key, null)
} else {
preferenceDataStore?.putString(key, textView.text.toString())
}
} else {
sharedPreferences?.edit { sharedPreferences?.edit {
if (textView.text.isNullOrBlank()) { if (textView.text.isNullOrBlank()) {
remove(key) remove(key)
@ -59,6 +91,7 @@ class EditTextResetPreference @JvmOverloads constructor(
putString(key, textView.text.toString()) putString(key, textView.text.toString())
} }
} }
}
notifyChanged() notifyChanged()
} }
} }

View file

@ -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 (callChangeListener(value)) {
if (isPersistent) { if (isPersistent) {
if (preferenceDataStore != null) {
preferenceDataStore?.putString(key, value)
notifyChanged()
} else {
sharedPreferences?.edit { putString(key, value) } sharedPreferences?.edit { putString(key, value) }
}
} else { } else {
tempValue = pos tempValue = pos
tempEntry = entries.getOrNull(pos) tempEntry = entries.getOrNull(pos)
notifyChanged() notifyChanged()
} }
}
this@ListMatPreference.summary = this@ListMatPreference.summary this@ListMatPreference.summary = this@ListMatPreference.summary
callChangeListener(value)
dialog.dismiss() dialog.dismiss()
} }
} }

View file

@ -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) {

View file

@ -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]) {
value = emptySet()
}
if (callChangeListener(value) && isPersistent) {
if (preferenceDataStore != null) {
preferenceDataStore?.putStringSet(key, value)
} else if (preferenceDataStore == null) {
sharedPreferences?.edit { putStringSet(key, value) } sharedPreferences?.edit { putStringSet(key, value) }
callChangeListener(value) }
}
notifyChanged() notifyChanged()
} }
setMultiChoiceItems(items.toTypedArray(), selected) { dialog, pos, checked -> setMultiChoiceItems(items.toTypedArray(), selected) { dialog, pos, checked ->