How do callbacks work in Kotlin. I've got what I ...
# android
j
How do callbacks work in Kotlin. I've got what I think should be a working async HTTP request API built and I pass a callback all the way down into the Fuel request callback. I start by calling
Copy code
finUser.loginUser(email = email, password = hashedPassword,
                  onCompletion =  {rspObj -> loginUserRsp(response = rspObj)},
                  onError = {rspObj -> loginUserError(response = rspObj) })
Then
Copy code
public fun loginUser(email: String, password: String,
                     onCompletion: (MutableMap<String,Any>) -> Unit,
                     onError: (MutableMap<String,Any>) -> Unit){
    this.finApi.updateUserToken(email = email, password = password,
                                onCompletion = onCompletion, onError = onError)
}
Then
Copy code
public fun updateUserToken(email: String, password: String,
                           onCompletion: (MutableMap<String,Any>) -> Unit,
                           onError: (MutableMap<String,Any>) -> Unit){
    val jsonBody = """{"email": "$email", "username": "$email", "password": "$password"}"""
    this.postRequest(url = this.userLogin, header = this.header, jsonBody = jsonBody,
        onCompletion = onCompletion, onError = onError)
}
to
Copy code
private fun postRequest(url: String, header: Header, jsonBody: String,
                         onCompletion: (MutableMap<String,Any>) -> Unit,
                         onError: (MutableMap<String,Any>) -> Unit){
    var request = <http://Fuel.post|Fuel.post>(url).body(jsonBody).header(header.content, header.contentValue)
    if(header.authorizationValue != ""){
        request.header(header.authorization, header.authorizationValue)
    }
    request.responseString { request, response, result ->
            responseHandle(request = request, response = response, result = result,
                onCompletion = onCompletion, onError = onError) }
}
And then I get success
Copy code
private fun responseHandle(request: Request,
                           response: Response,
                           result: Result<String, FuelError>,
                           onCompletion: (MutableMap<String,Any>) -> Unit,
                           onError: (MutableMap<String,Any>) -> Unit){
    when (result) {
        is Result.Failure -> {
            onError(Klaxon().parse<MutableMap<String, Any>>(response.responseMessage)!!)
        }
        is Result.Success -> {
            onCompletion(Klaxon().parse<MutableMap<String, Any>>(response.responseMessage)!!)
        }
    }
}
But my breakpoint never hits here
Copy code
fun loginUserRsp(response: MutableMap<String, Any>){
    print(response)
}
It looks right... but I'm pretty new to kotlin EDIT: Nvm found the issue
Klaxon().parse<MutableMap<String, Any>>(response.responseMessage)!!
wasn't doing what i thought it was
🧵 1