brent
10/04/2017, 3:58 PMefemoney
10/04/2017, 4:42 PMCancellationSignal
anyways.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
}
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