Merge pull request #4601 from Bnyro/master

fix: don't update instance preference while dialog visible to user
This commit is contained in:
Bnyro 2023-08-23 13:03:28 +02:00 committed by GitHub
commit 0385c21bd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -16,12 +16,20 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
abstract class BasePreferenceFragment : PreferenceFragmentCompat() {
abstract val titleResourceId: Int
/**
* Whether any preference dialog is currently visible to the user.
*/
var isDialogVisible = false
override fun onStart() {
super.onStart()
(activity as? SettingsActivity)?.changeTopBarText(getString(titleResourceId))
}
override fun onDisplayPreferenceDialog(preference: Preference) {
// can be set to true here since we only use the following preferences with dialogs
isDialogVisible = true
when (preference) {
/**
* Show a [MaterialAlertDialogBuilder] when the preference is a [ListPreference]
@ -41,6 +49,7 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat() {
dialog.dismiss()
}
.setNegativeButton(R.string.cancel, null)
.setOnDismissListener { isDialogVisible = false }
.show()
}
@ -50,7 +59,7 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat() {
}.toBooleanArray()
MaterialAlertDialogBuilder(requireContext())
.setTitle(preference.title)
.setMultiChoiceItems(preference.entries, selectedItems) { dialog, _, _ ->
.setMultiChoiceItems(preference.entries, selectedItems) { _, _, _ ->
val newValues = preference.entryValues
.filterIndexed { index, _ -> selectedItems[index] }
.map { it.toString() }
@ -60,6 +69,7 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat() {
}
}
.setNegativeButton(R.string.cancel, null)
.setOnDismissListener { isDialogVisible = false }
.show()
}
@ -76,10 +86,12 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat() {
}
}
.setNegativeButton(R.string.cancel, null)
.setOnDismissListener { isDialogVisible = false }
.show()
}
/**
* Otherwise show the normal dialog, dialogs for other preference types are not supported yet
* Otherwise show the normal dialog, dialogs for other preference types are not supported yet,
* nor used anywhere inside the app
*/
else -> super.onDisplayPreferenceDialog(preference)
}

View File

@ -138,6 +138,10 @@ class InstanceSettings : BasePreferenceFragment() {
.plus(customInstances)
.sortedBy { it.name }
// If any preference dialog is visible in this fragment, it's one of the instance selection
// dialogs. In order to prevent UX issues, we don't update the instances list then.
if (isDialogVisible) return@runCatching
for (instancePref in instancePrefs) {
// add custom instances to the list preference
instancePref.entries = instances.map { it.name }.toTypedArray()