I need to do the next:
- Check is exist profile
- If exist then start async http request (
getAdvertising()
) by Retrofit lib - if not exist then first loading profile from remote (by Retrofit) AND if success get result then start async http request (
getAdvertising()
)
I do this by callback like this:
fun getAdvertising(callback: Callback<List<Advertising>>) {
val call = myRestClient.advertising
executeAsync(call, callback)
}
private fun <T> executeAsync(call: Call<T>, callback: Callback<T>) {
val currentApplicationProfileResponse = foService.applicationProfileResponse
if (currentApplicationProfileResponse == null) {
getApplicationProfile(object : DefaultRestClientCallback<ApplicationProfileResponse>() {
override fun onTransportResponse(transportResponse: TransportResponse) {
super.onTransportResponse(transportResponse)
if (transportResponse.isSuccessful) {
//asynchronously
call.enqueue(callback)
} else { // not success
if (callback is DefaultRestClientCallback<*>) {
callback.onTransportResponse(transportResponse)
} else {
callback.onResponse(call, transportResponse.originalResponse as Response<T>?)
}
}
}
})
} else { // appProfile is not null
//asynchronously
call.enqueue(callback)
}
}
Nice it's work fine.
But is to much code. Is it possible to repalace Callback by Kotlin's coroutines?