From 8b7601029b970a1e53ba6f60f3a58ed973f82486 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 9 May 2023 13:46:49 +0200 Subject: [PATCH] Fix crash when entering to "Protect access" screen. --- .../settings/VectorSettingsPinFragment.kt | 66 +++++++++---------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPinFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPinFragment.kt index 840ae23115..f86095e12d 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPinFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPinFragment.kt @@ -16,9 +16,7 @@ package im.vector.app.features.settings -import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope -import androidx.lifecycle.repeatOnLifecycle import androidx.preference.Preference import androidx.preference.SwitchPreference import dagger.hilt.android.AndroidEntryPoint @@ -80,13 +78,13 @@ class VectorSettingsPinFragment : override fun onResume() { super.onResume() - updateBiometricPrefState(isPinCodeChecked = usePinCodePref.isChecked) + viewLifecycleOwner.lifecycleScope.launch { + refreshPinCodeStatus() + } } override fun bindPref() { - refreshPinCodeStatus() - usePinCodePref.setOnPreferenceChangeListener { _, value -> val isChecked = (value as? Boolean).orFalse() updateBiometricPrefState(isPinCodeChecked = isChecked) @@ -130,38 +128,34 @@ class VectorSettingsPinFragment : .onFailure { Timber.e(it) } } - private fun refreshPinCodeStatus() { - viewLifecycleOwner.lifecycleScope.launch { - viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) { - val hasPinCode = pinCodeStore.hasEncodedPin() - usePinCodePref.isChecked = hasPinCode - usePinCodePref.onPreferenceClickListener = Preference.OnPreferenceClickListener { - if (hasPinCode) { - lifecycleScope.launch { - pinCodeStore.deletePinCode() - refreshPinCodeStatus() - } - } else { - navigator.openPinCode( - requireContext(), - pinActivityResultLauncher, - PinMode.CREATE - ) - } - true - } - - changePinCodePref.onPreferenceClickListener = Preference.OnPreferenceClickListener { - if (hasPinCode) { - navigator.openPinCode( - requireContext(), - pinActivityResultLauncher, - PinMode.MODIFY - ) - } - true + private suspend fun refreshPinCodeStatus() { + val hasPinCode = pinCodeStore.hasEncodedPin() + usePinCodePref.isChecked = hasPinCode + usePinCodePref.onPreferenceClickListener = Preference.OnPreferenceClickListener { + if (hasPinCode) { + lifecycleScope.launch { + pinCodeStore.deletePinCode() + refreshPinCodeStatus() } + } else { + navigator.openPinCode( + requireContext(), + pinActivityResultLauncher, + PinMode.CREATE + ) } + true + } + + changePinCodePref.onPreferenceClickListener = Preference.OnPreferenceClickListener { + if (hasPinCode) { + navigator.openPinCode( + requireContext(), + pinActivityResultLauncher, + PinMode.MODIFY + ) + } + true } } @@ -170,6 +164,6 @@ class VectorSettingsPinFragment : } private val pinActivityResultLauncher = registerStartForActivityResult { - refreshPinCodeStatus() + // Nothing to do, refreshPinCodeStatus() will be called by `onResume` } }