From e13a54f212f7fc628eb48561f1b225e28468c6e7 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 7 Apr 2021 19:23:21 +0530 Subject: Refactor biometric preference handling (#1374) Fixes #1371 Signed-off-by: Harsh Shandilya --- .../msfjarvis/aps/ui/settings/GeneralSettings.kt | 62 ++++++++++------------ 1 file changed, 28 insertions(+), 34 deletions(-) (limited to 'app/src/main/java/dev') diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/settings/GeneralSettings.kt b/app/src/main/java/dev/msfjarvis/aps/ui/settings/GeneralSettings.kt index 39501d52..da93c3fe 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/settings/GeneralSettings.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/settings/GeneralSettings.kt @@ -58,49 +58,43 @@ class GeneralSettings(private val activity: FragmentActivity) : SettingsProvider defaultValue = false } + val canAuthenticate = BiometricAuthenticator.canAuthenticate(activity) checkBox(PreferenceKeys.BIOMETRIC_AUTH) { titleRes = R.string.pref_biometric_auth_title defaultValue = false - } - .apply { - val canAuthenticate = BiometricAuthenticator.canAuthenticate(activity) - if (!canAuthenticate) { - enabled = false - checked = false - summaryRes = R.string.pref_biometric_auth_summary_error - } else { - summaryRes = R.string.pref_biometric_auth_summary - onClick { - enabled = false - val isChecked = checked - activity.sharedPrefs.edit { - BiometricAuthenticator.authenticate(activity) { result -> - when (result) { - is BiometricAuthenticator.Result.Success -> { - // Apply the changes - putBoolean(PreferenceKeys.BIOMETRIC_AUTH, checked) - enabled = true - } - else -> { - // If any error occurs, revert back to the previous - // state. This - // catch-all clause includes the cancellation case. - putBoolean(PreferenceKeys.BIOMETRIC_AUTH, !checked) - checked = !isChecked - enabled = true - } - } + enabled = canAuthenticate + summaryRes = + if (canAuthenticate) R.string.pref_biometric_auth_summary else R.string.pref_biometric_auth_summary_error + onClick { + enabled = false + val isChecked = checked + activity.sharedPrefs.edit { + BiometricAuthenticator.authenticate(activity) { result -> + when (result) { + is BiometricAuthenticator.Result.Success -> { + // Apply the changes + putBoolean(PreferenceKeys.BIOMETRIC_AUTH, checked) + enabled = true } - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { - activity.getSystemService()?.apply { - removeDynamicShortcuts(dynamicShortcuts.map { it.id }.toMutableList()) + else -> { + // If any error occurs, revert back to the previous + // state. This + // catch-all clause includes the cancellation case. + putBoolean(PreferenceKeys.BIOMETRIC_AUTH, !checked) + checked = !isChecked + enabled = true } } - false } } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { + activity.getSystemService()?.apply { + removeDynamicShortcuts(dynamicShortcuts.map { it.id }.toMutableList()) + } + } + false } + } } } } -- cgit v1.2.3