refactor: eliminate one level of nesting from BiometricAuthenticator

This commit is contained in:
Harsh Shandilya 2023-07-09 17:11:55 +05:30
parent 6f3f7e4b4c
commit dfe4b14b4c
No known key found for this signature in database

View file

@ -61,43 +61,45 @@ object BiometricAuthenticator {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString) super.onAuthenticationError(errorCode, errString)
logcat(TAG) { "onAuthenticationError(errorCode=$errorCode, msg=$errString)" } logcat(TAG) { "onAuthenticationError(errorCode=$errorCode, msg=$errString)" }
callback(
when (errorCode) { when (errorCode) {
BiometricPrompt.ERROR_CANCELED, BiometricPrompt.ERROR_CANCELED,
BiometricPrompt.ERROR_USER_CANCELED, BiometricPrompt.ERROR_USER_CANCELED,
BiometricPrompt.ERROR_NEGATIVE_BUTTON -> { BiometricPrompt.ERROR_NEGATIVE_BUTTON -> {
Result.Cancelled callback(Result.Cancelled)
} }
BiometricPrompt.ERROR_HW_NOT_PRESENT, BiometricPrompt.ERROR_HW_NOT_PRESENT,
BiometricPrompt.ERROR_HW_UNAVAILABLE, BiometricPrompt.ERROR_HW_UNAVAILABLE,
BiometricPrompt.ERROR_NO_BIOMETRICS, BiometricPrompt.ERROR_NO_BIOMETRICS,
BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL -> { BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL -> {
Result.HardwareUnavailableOrDisabled callback(Result.HardwareUnavailableOrDisabled)
} }
BiometricPrompt.ERROR_LOCKOUT, BiometricPrompt.ERROR_LOCKOUT,
BiometricPrompt.ERROR_LOCKOUT_PERMANENT, BiometricPrompt.ERROR_LOCKOUT_PERMANENT,
BiometricPrompt.ERROR_NO_SPACE, BiometricPrompt.ERROR_NO_SPACE,
BiometricPrompt.ERROR_TIMEOUT, BiometricPrompt.ERROR_TIMEOUT,
BiometricPrompt.ERROR_VENDOR -> { BiometricPrompt.ERROR_VENDOR -> {
callback(
Result.Failure( Result.Failure(
errorCode, errorCode,
activity.getString(R.string.biometric_auth_error_reason, errString) activity.getString(R.string.biometric_auth_error_reason, errString)
) )
)
} }
BiometricPrompt.ERROR_UNABLE_TO_PROCESS -> { BiometricPrompt.ERROR_UNABLE_TO_PROCESS -> {
Result.Retry callback(Result.Retry)
} }
// We cover all guaranteed values above, but [errorCode] is still an Int // We cover all guaranteed values above, but [errorCode] is still an Int
// at the end of the day so a catch-all else will always be required. // at the end of the day so a catch-all else will always be required.
else -> { else -> {
callback(
Result.Failure( Result.Failure(
errorCode, errorCode,
activity.getString(R.string.biometric_auth_error_reason, errString) activity.getString(R.string.biometric_auth_error_reason, errString)
) )
}
}
) )
} }
}
}
override fun onAuthenticationFailed() { override fun onAuthenticationFailed() {
super.onAuthenticationFailed() super.onAuthenticationFailed()