0

Android Studio 3.6

My custom callback interface:

interface RecoveryPasswordConfirmCodeCallback {
    fun onSuccess()
    fun onError(ex: Throwable?)
}

Use:

val result = TransportService.recoverPasswordConfirmCode(
                    confirmCode,
                    ex,
                    object : RecoveryPasswordConfirmCodeCallback {
                        override fun onSuccess() {

                        }

                        override fun onError(ex: Throwable?) {

                            if (ex is InvalidOtpException) {
                                toastMessage.value = SingleEvent(
                                    getApplication<Application>().applicationContext.getString(
                                        R.string.incorrect_confirm_code
                                    )
                                )
                            } else {
                                toastMessage.value = SingleEvent(
                                    getApplication<Application>().applicationContext.getString(
                                        R.string.default_error_message
                                    ))
                            }
                        }
                    })


 fun recoverPasswordConfirmCode(
            confirmCode: String,
            ex: NeedTfaException,
            callBack: RecoveryPasswordConfirmCodeCallback
        ) {
           //some code here
        }

Nice. It's work fine. But... is it possible to replace my custom callback interface by Kotlin's coroutine. I don't want to create custom interface only for execute method recoverPasswordConfirmCode

2 Answers 2

1

You can convert recoverPasswordConfirmCode() to a suspend function and return the result in the form of a sealed class to indicate if it's an error or the valid response. Something like this:

// Generic response class
sealed class Response<out T>{
    data class Error(val ex: Throwable) : Response<Nothing>()
    data class Data<T>(val data: T) : Response<T>()
}

// in your TransportService class
suspend fun recoverPasswordConfirmCode(confirmCode, ex): Response<RecoverPasswordResponse>{
    // Do your stuff here 
    // return Response.Data<RecoverPasswordResponse>(/* your data object here */)

}

Then call it like this and check the response type:

val result = TransportService.recoverPasswordConfirmCode(confirmCode, ex)
when(result){
    is Response.Error -> // Do something
    is Response.Data -> // Do something
}

Note that you will have to call the suspend function inside a coroutine context.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to create a custom interface. Consume your API like this:

suspend fun recoverPasswordConfirmCode(confirmCode: String): YourReturnType = suspendCancellableCoroutine { cont ->
    try {
        val result = //Do your blocking API calls here
        if(result.code == confirmCode) //Check confirm code is correct
            cont.resume(YourResult) //Return your result here
        else
            cont.resumeWithException(YourException) //Throw an exception otherwise
    } catch (e: Exception) {
        cont.resumeWithException(e)
    }
}

Call recoverPasswordConfirmCode method inside a Coroutine Scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.