Might have more luck with Rx for cancelation <@U5E...
# android
b
Might have more luck with Rx for cancelation @efemoney
e
I wrote something quick around
CancellationSignal
anyways.
Copy code
fun <T> Call<T>.enqueue(
        success: (call: Call<T>, response: Response<T>) -> Unit,
        failure: (call: Call<T>, t: Throwable) -> Unit
): Call<T> {
    enqueue(object : Callback<T> {
        override fun onResponse(call: Call<T>, response: Response<T>) = success(call, response)
        override fun onFailure(call: Call<T>, t: Throwable) = failure(call, t)
    })
    return this
}

fun <T> Call<T>.cancelOn(signal: CancellationSignal): Call<T> {
    signal.setOnCancelListener { this.cancel() }
    return this
}

infix fun CancellationSignal.or(other: CancellationSignal): CancellationSignal {

    val c = CancellationSignal()

    this.setOnCancelListener { c.cancel() }
    other.setOnCancelListener { c.cancel() }

    return c
}
…which allows me to write
Copy code
api.doSomething()
    .cancelOn(dialog.cancellationSignal or onDestroy)
    .enqueue(
            { call, res -> },
            { call, t -> }
    )
dialog
is a custom
ProgressDialog
that exposes a
CancellationSignal
that is called when the dialog is cancelled.
onDestroy
is a
CancellationSignal
called when the activity is destroyed