Fix crash when entering to "Protect access" screen.

This commit is contained in:
Benoit Marty 2023-05-09 13:46:49 +02:00
parent 79b451546d
commit 8b7601029b

View file

@ -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`
}
}