From eea908dfa34e239f6c96d4a3abc12500ce734459 Mon Sep 17 00:00:00 2001 From: Oscar Mira Date: Fri, 10 Nov 2023 02:23:23 +0100 Subject: [PATCH] Fix toggle buttons in settings --- .../securesms/components/settings/DSLSettingsAdapter.kt | 6 +++++- .../components/settings/app/help/HelpSettingsFragment.kt | 5 +++-- .../settings/app/privacy/PrivacySettingsFragment.kt | 9 ++++----- .../thoughtcrime/securesms/components/settings/dsl.kt | 6 ++++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/DSLSettingsAdapter.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/DSLSettingsAdapter.kt index 48122bdfab..3e138583bf 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/DSLSettingsAdapter.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/DSLSettingsAdapter.kt @@ -214,7 +214,11 @@ class SwitchPreferenceViewHolder(itemView: View) : PreferenceViewHolder + switchWidget.setOnCheckedChangeListener { _, isChecked -> + if (!model.onToggle(isChecked)) { + // MOLLY: Set the toggle button back to the previous state + switchWidget.toggle() + } model.onClick() } diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/help/HelpSettingsFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/help/HelpSettingsFragment.kt index 3d30aee5fb..fa0c3d8f0a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/help/HelpSettingsFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/help/HelpSettingsFragment.kt @@ -73,8 +73,7 @@ class HelpSettingsFragment : DSLSettingsFragment(R.string.preferences__help) { switchPref( title = DSLSettingsText.from(R.string.preferences__enable_debug_log), isChecked = state.logEnabled, - onClick = { - if (state.logEnabled) { + onToggle = { isChecked -> if (!isChecked) { MaterialAlertDialogBuilder(requireContext()) .setMessage(R.string.HelpSettingsFragment_disable_and_delete_debug_log) .setPositiveButton(android.R.string.ok) { dialog, _ -> @@ -83,8 +82,10 @@ class HelpSettingsFragment : DSLSettingsFragment(R.string.preferences__help) { } .setNegativeButton(android.R.string.cancel, null) .show() + false } else { viewModel.setLogEnabled(true) + true } } ) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/privacy/PrivacySettingsFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/privacy/PrivacySettingsFragment.kt index df3264c199..ed03d76473 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/privacy/PrivacySettingsFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/privacy/PrivacySettingsFragment.kt @@ -90,17 +90,16 @@ class PrivacySettingsFragment : DSLSettingsFragment(R.string.preferences__privac title = DSLSettingsText.from(R.string.preferences__passphrase_lock), summary = DSLSettingsText.from(R.string.preferences__protect_molly_database_with_a_passphrase), isChecked = state.passphraseLock, - onClick = { - val enabled = !state.passphraseLock - val mode = if (enabled) ChangePassphraseDialogFragment.MODE_ENABLE else ChangePassphraseDialogFragment.MODE_DISABLE - + onToggle = { isChecked -> + val mode = if (isChecked) ChangePassphraseDialogFragment.MODE_ENABLE else ChangePassphraseDialogFragment.MODE_DISABLE val dialog = ChangePassphraseDialogFragment.newInstance(mode) dialog.setMasterSecretChangedListener { masterSecret -> - viewModel.setPassphraseLockEnabled(enabled) + viewModel.setPassphraseLockEnabled(isChecked) (activity as PassphraseActivity).setMasterSecret(masterSecret) ConversationUtil.refreshRecipientShortcuts() } dialog.show(parentFragmentManager, "ChangePassphraseDialogFragment") + false } ) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/dsl.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/dsl.kt index f8e85a2f86..558eba0e35 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/dsl.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/dsl.kt @@ -81,9 +81,10 @@ class DSLConfiguration { icon: DSLSettingsIcon? = null, isEnabled: Boolean = true, isChecked: Boolean, - onClick: () -> Unit + onToggle: (Boolean) -> Boolean = { true }, + onClick: () -> Unit = { }, ) { - val preference = SwitchPreference(title, summary, icon, isEnabled, isChecked, onClick) + val preference = SwitchPreference(title, summary, icon, isEnabled, isChecked, onToggle, onClick) children.add(preference) } @@ -300,6 +301,7 @@ class SwitchPreference( override val icon: DSLSettingsIcon? = null, override val isEnabled: Boolean, val isChecked: Boolean, + val onToggle: (Boolean) -> Boolean, val onClick: () -> Unit ) : PreferenceModel() {