Skip to content

Commit

Permalink
Fix toggle buttons in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
valldrac committed Nov 10, 2023
1 parent e350d4a commit eea908d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ class SwitchPreferenceViewHolder(itemView: View) : PreferenceViewHolder<SwitchPr
switchWidget.isChecked = model.isChecked
switchWidget.isEnabled = model.isEnabled

switchWidget.setOnCheckedChangeListener { _, _ ->
switchWidget.setOnCheckedChangeListener { _, isChecked ->
if (!model.onToggle(isChecked)) {
// MOLLY: Set the toggle button back to the previous state
switchWidget.toggle()
}
model.onClick()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, _ ->
Expand All @@ -83,8 +82,10 @@ class HelpSettingsFragment : DSLSettingsFragment(R.string.preferences__help) {
}
.setNegativeButton(android.R.string.cancel, null)
.show()
false
} else {
viewModel.setLogEnabled(true)
true
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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<SwitchPreference>() {

Expand Down

0 comments on commit eea908d

Please sign in to comment.