H
H
h1_0ne2019-09-15 14:28:35
Android
h1_0ne, 2019-09-15 14:28:35

How to wait for a function to execute in Kotlin?

I have a function that is supposed to return the result of whether the biometric check passed or not

fun fingerPrintResult():Boolean
    {
        val executor = Executors.newSingleThreadExecutor()
        val activity: FragmentActivity = this // reference to activity

        var resultReturn:Boolean = false

        val biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() {

            override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                super.onAuthenticationError(errorCode, errString)
                resultReturn = false
            }

            override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
                resultReturn = true
            }

            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                resultReturn = false
            }
        })

        val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle("Подтвердите личность")
            .setNegativeButtonText("Negative Button")
            .build()

         biometricPrompt.authenticate(promptInfo)

        return resultReturn
    }

I call it in one of the buttons OnItemClickListener
ad.setNegativeButton("Удалить", object : DialogInterface.OnClickListener {
                    override fun onClick(dialog: DialogInterface, arg1: Int) {

                        var isTrue: Boolean = false

                        isTrue = fingerPrintResult()

                        if(!isTrue)
                        {
                            Toast.makeText(applicationContext,"Error!", Toast.LENGTH_SHORT).show()
                            return
                        }

                        var value = item["id"].toString()
                        var db = DBHandler(parent.context)
                        db.deleteCountry(Integer.parseInt(value))
                    }
                })

But isTrue does not wait for the test to complete.
How can I make it wait for the result of the function?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2019-09-16
@zagayevskiy

Doesn't it bother you at all that you are waiting for the same dialogue and doing something in the callback? The same meaning. In the methods you get the result.

A
Alexander Yudakov, 2019-09-16
@AlexanderYudakov

Something like this:

suspend fun fingerPrintResult(): Boolean {
    val executor = Executors.newSingleThreadExecutor()
    val activity: FragmentActivity = this // reference to activity

    return suspendCoroutine { coroutine ->

        val biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() {

                override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                    super.onAuthenticationError(errorCode, errString)
                    coroutine.resume(false)
                }

                override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                    super.onAuthenticationSucceeded(result)
                    coroutine.resume(true)
                }

                override fun onAuthenticationFailed() {
                    super.onAuthenticationFailed()
                    coroutine.resume(false)
                }
            })

        val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle("Подтвердите личность")
            .setNegativeButtonText("Negative Button")
            .build()

        biometricPrompt.authenticate(promptInfo)
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question