32 lines
928 B
Kotlin
32 lines
928 B
Kotlin
package com.keylesspalace.tusky.network
|
|
|
|
import android.content.Context
|
|
import android.widget.Toast
|
|
import androidx.core.content.ContextCompat
|
|
import okhttp3.Interceptor
|
|
import okhttp3.Response
|
|
|
|
class HttpToastInterceptor(
|
|
private val context: Context,
|
|
) : Interceptor {
|
|
|
|
override fun intercept(chain: Interceptor.Chain): Response {
|
|
val request = chain.request()
|
|
|
|
return runCatching { chain.proceed(request) }
|
|
.onSuccess {
|
|
if (!it.isSuccessful) {
|
|
showToast("${request.method} ${request.url}\n${it.code} ${it.message}")
|
|
}
|
|
}
|
|
.onFailure { showToast("${request.method} ${request.url}\n${it}") }
|
|
.getOrThrow()
|
|
}
|
|
|
|
private fun showToast(message: String) {
|
|
ContextCompat.getMainExecutor(context).execute {
|
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
|
|
}
|
|
}
|
|
}
|