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 androidx.preference.DialogPreference
import androidx.preference.EditTextPreference
import androidx.preference.EditTextPreferenceDialogController
import androidx.preference.ListPreference
import androidx.preference.ListPreferenceDialogController
import androidx.preference.MultiSelectListPreference
import androidx.preference.MultiSelectListPreferenceDialogController
import androidx.preference.Preference
import androidx.preference.PreferenceGroupAdapter
import androidx.preference.PreferenceManager
@ -39,6 +36,7 @@ import eu.kanade.tachiyomi.source.preferenceKey
import eu.kanade.tachiyomi.source.sourcePreferences
import eu.kanade.tachiyomi.ui.base.controller.BaseCoroutineController
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.switchPreference
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.widget.LinearLayoutManagerAccurateOffset
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.onEach
import okhttp3.HttpUrl.Companion.toHttpUrl
@ -326,23 +327,53 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
screen.getPreference(it) === preference
}
val f = when (preference) {
is EditTextPreference ->
EditTextPreferenceDialogController
.newInstance(preference.getKey())
is ListPreference ->
ListPreferenceDialogController
.newInstance(preference.getKey())
is MultiSelectListPreference ->
MultiSelectListPreferenceDialogController
.newInstance(preference.getKey())
val context = preferences.context
val matPref = when (preference) {
is EditTextPreference -> EditTextResetPreference(activity, context).apply {
dialogSummary = preference.dialogMessage
onPreferenceChangeListener = preference.onPreferenceChangeListener
}
is ListPreference -> ListMatPreference(activity, context).apply {
isPersistent = false
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(
"Tried to display dialog for unknown " +
"preference type. Did you forget to override onDisplayPreferenceDialog()?",
)
}
f.targetController = this
f.showDialog(router)
matPref.apply {
key = preference.key
preferenceDataStore = preference.preferenceDataStore
title = (preference as? DialogPreference)?.dialogTitle ?: preference.title
}.performClick()
}
private fun Source.isEnabled(): Boolean {

View file

@ -2,12 +2,17 @@ package eu.kanade.tachiyomi.widget.preference
import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.edit
import androidx.core.view.isVisible
import androidx.preference.Preference.SummaryProvider
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import eu.kanade.tachiyomi.R
@ -18,12 +23,19 @@ class EditTextResetPreference @JvmOverloads constructor(
) :
MatPreference(activity, context, attrs) {
private var defValue: String = ""
var dialogSummary: CharSequence? = null
override fun onSetInitialValue(defaultValue: Any?) {
super.onSetInitialValue(defaultValue)
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?) {
super.setDefaultValue(defaultValue)
defValue = defaultValue as? String ?: defValue
@ -41,22 +53,43 @@ class EditTextResetPreference @JvmOverloads constructor(
val view = LayoutInflater.from(context).inflate(resourceId, null)
val textView = view.findViewById<EditText>(android.R.id.edit)
val message = view.findViewById<TextView>(android.R.id.message)
message?.isVisible = false
textView?.append(sharedPreferences?.getString(key, defValue))
message?.isVisible = dialogSummary != null
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.setNeutralButton(R.string.reset) { _, _ ->
if (callChangeListener(defValue)) {
sharedPreferences?.edit { remove(key) }
if (preferenceDataStore != null) {
preferenceDataStore?.putString(key, null)
} else {
sharedPreferences?.edit { remove(key) }
}
notifyChanged()
}
}
this.setPositiveButton(android.R.string.ok) { _, _ ->
if (callChangeListener(textView.text.toString())) {
sharedPreferences?.edit {
if (preferenceDataStore != null) {
if (textView.text.isNullOrBlank()) {
remove(key)
preferenceDataStore?.putString(key, null)
} 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()

View file

@ -29,10 +29,16 @@ open class ListMatPreference @JvmOverloads constructor(
defValue = defaultValue as? String ?: defValue
}
override fun setDefaultValue(defaultValue: Any?) {
super.setDefaultValue(defaultValue)
defValue = defaultValue as? String ?: defValue
}
private val indexOfPref: Int
get() = tempValue ?: entryValues.indexOf(
if (isPersistent) {
sharedPreferences?.getString(key, defValue)
if (isPersistent || preferenceDataStore != null) {
preferenceDataStore?.getString(key, defValue)
?: sharedPreferences?.getString(key, defValue)
} else {
tempEntry
} ?: defValue,
@ -58,15 +64,21 @@ open class ListMatPreference @JvmOverloads constructor(
val default = indexOfPref
setSingleChoiceItems(entries.toTypedArray(), default) { dialog, pos ->
val value = entryValues[pos]
if (isPersistent) {
sharedPreferences?.edit { putString(key, value) }
} else {
tempValue = pos
tempEntry = entries.getOrNull(pos)
notifyChanged()
if (callChangeListener(value)) {
if (isPersistent) {
if (preferenceDataStore != null) {
preferenceDataStore?.putString(key, value)
notifyChanged()
} else {
sharedPreferences?.edit { putString(key, value) }
}
} else {
tempValue = pos
tempEntry = entries.getOrNull(pos)
notifyChanged()
}
}
this@ListMatPreference.summary = this@ListMatPreference.summary
callChangeListener(value)
dialog.dismiss()
}
}

View file

@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.widget.preference
import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.util.AttributeSet
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
@ -36,15 +37,17 @@ open class MatPreference @JvmOverloads constructor(
val dialog = dialog().apply {
setOnDismissListener { this@MatPreference.isShowing = false }
}.create()
// dialog.setOnShowListener {
onShow(dialog)
// }
dialog.setOnShowListener {
didShow(it)
}
dialog.show()
}
isShowing = true
}
protected open fun onShow(dialog: AlertDialog) { }
protected open fun didShow(dialog: DialogInterface) { }
protected open var customSummaryProvider: SummaryProvider<MatPreference>? = null
set(value) {

View file

@ -64,7 +64,8 @@ class MultiListMatPreference @JvmOverloads constructor(
@SuppressLint("CheckResult")
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) {
if (showAllLast) {
entries + listOf(context.getString(allSelectionRes!!))
@ -88,9 +89,16 @@ class MultiListMatPreference @JvmOverloads constructor(
var value = pos.mapNotNull {
entryValues.getOrNull(it - if (allSelectionRes != null && !showAllLast) 1 else 0)
}.toSet()
if (allSelectionRes != null && !allIsAlwaysSelected && selected[allPos]) value = emptySet()
sharedPreferences?.edit { putStringSet(key, value) }
callChangeListener(value)
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) }
}
}
notifyChanged()
}
setMultiChoiceItems(items.toTypedArray(), selected) { dialog, pos, checked ->