从回调到悬浮功能

发布于 2025-01-23 08:35:15 字数 1779 浏览 3 评论 0 原文

我有此功能:

fun createBiometricPrompt(
        activity: AppCompatActivity,
        processSuccess: (BiometricPrompt.AuthenticationResult) -> Unit,
        callback: BiometricCallback
    ): BiometricPrompt {
        var nbFailure = 0
        val executor = ContextCompat.getMainExecutor(activity)

        val callback = object : BiometricPrompt.AuthenticationCallback() {

            override fun onAuthenticationError(errCode: Int, errString: CharSequence) {
                super.onAuthenticationError(errCode, errString)
                Log.d(TAG, "errCode is $errCode and errString is: $errString")
            }

            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                Log.d(TAG, "User biometric rejected.")
                nbFailure++;
                if(nbFailure == MAX_BIOMETRICS_FAILURE){
                    nbFailure = 0
                    callback.onFailure()
                }
            }

使用回调回调:Biometriccallback ,它仅用于使用它来通知通过发送 callback.onfailure()

回调发生的故障发生。在通过此操作的调用片段中:

fragment.kt

biometricPrompt =
                BiometricPromptUtils.createBiometricPrompt(requireActivity() as AppCompatActivity, ::encryptAndStoreServerToken, object: BiometricCallback {
                    override fun onFailure() {
                        biometricPrompt.cancelAuthentication()
                        viewModel.loginError.set("Error Biometrics")
                    }
                })

onfailure 被使用来取消过程并显示错误。

回调界面的定义为:

interface BiometricCallback {
    fun onFailure()
}

我被要求使用悬挂函数,而不是回调,但我不知道如何正确执行此操作。我应该使用Livedata吗?如果有的话,请帮助

谢谢

I have this function :

fun createBiometricPrompt(
        activity: AppCompatActivity,
        processSuccess: (BiometricPrompt.AuthenticationResult) -> Unit,
        callback: BiometricCallback
    ): BiometricPrompt {
        var nbFailure = 0
        val executor = ContextCompat.getMainExecutor(activity)

        val callback = object : BiometricPrompt.AuthenticationCallback() {

            override fun onAuthenticationError(errCode: Int, errString: CharSequence) {
                super.onAuthenticationError(errCode, errString)
                Log.d(TAG, "errCode is $errCode and errString is: $errString")
            }

            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                Log.d(TAG, "User biometric rejected.")
                nbFailure++;
                if(nbFailure == MAX_BIOMETRICS_FAILURE){
                    nbFailure = 0
                    callback.onFailure()
                }
            }

which is using a callback callback: BiometricCallback, it's only for now using it for notifying that a failure happened by sending callback.onFailure()

Callback is called in the calling Fragment by doing this:

Fragment.kt

biometricPrompt =
                BiometricPromptUtils.createBiometricPrompt(requireActivity() as AppCompatActivity, ::encryptAndStoreServerToken, object: BiometricCallback {
                    override fun onFailure() {
                        biometricPrompt.cancelAuthentication()
                        viewModel.loginError.set("Error Biometrics")
                    }
                })

The onFailure is used then to cancel the process and display an error.

The Callback interface is defined as :

interface BiometricCallback {
    fun onFailure()
}

I have been asked to use a suspend function instead of a callback but I have no clue how to do it properly. Should I use LiveData ? If any idea, please help

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2025-01-30 08:35:15

您可以使用 suppendCancellableCoroutine supstendCoroutine 转换任何基于直接样式的基于回调的API,以使其更加kotlin友好友好,而且它提供了更多的封装,您将刚刚返回结果。仅在函数中处理

 suspend fun authenticate():BiomatricPrompt.AuthenticationResult? {
 return suspendCancelableCoroutine { continuation -> 
     biometricPrompt = BiometricPrompt(this, executor,
        object : BiometricPrompt.AuthenticationCallback() {
    override fun onAuthenticationError(errorCode: Int,
            errString: CharSequence) {
        super.onAuthenticationError(errorCode, errString)
        continuation.resume(null,null)
    }

    override fun onAuthenticationSucceeded(
            result: BiometricPrompt.AuthenticationResult) {
        super.onAuthenticationSucceeded(result)
         continuation.resume(result,null)
    }

    override fun onAuthenticationFailed() {
        super.onAuthenticationFailed()
         continuation.resume(null,null)
    }
   })
    promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .build()
    biometricPrompt.authenticate(promptInfo)
 }
}

val authenticationResult  = authenticate()
if(authenticationResult == null){
 //authentication failed
}else{
//authenticated successfully 
}

可以使用自定义对象作为结果类型来处理更多用例

you can use suspendCancellableCoroutine or suspendCoroutine to convert any callback based Api in direct style to make it more kotlin friendly plus it provides more encapsulation you will be just returned with the result all complexity is handled inside the function only

 suspend fun authenticate():BiomatricPrompt.AuthenticationResult? {
 return suspendCancelableCoroutine { continuation -> 
     biometricPrompt = BiometricPrompt(this, executor,
        object : BiometricPrompt.AuthenticationCallback() {
    override fun onAuthenticationError(errorCode: Int,
            errString: CharSequence) {
        super.onAuthenticationError(errorCode, errString)
        continuation.resume(null,null)
    }

    override fun onAuthenticationSucceeded(
            result: BiometricPrompt.AuthenticationResult) {
        super.onAuthenticationSucceeded(result)
         continuation.resume(result,null)
    }

    override fun onAuthenticationFailed() {
        super.onAuthenticationFailed()
         continuation.resume(null,null)
    }
   })
    promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .build()
    biometricPrompt.authenticate(promptInfo)
 }
}

usage

val authenticationResult  = authenticate()
if(authenticationResult == null){
 //authentication failed
}else{
//authenticated successfully 
}

you can use custom object as result type to handle more use cases

more information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文