I'm a bit confused about fetcher, mappers and loadRequest...
I have a picasso downloader reading using oAuth Scribe library a protected image and I want to rewrite it with coil but I need at least an hint to start the conversion
The image is loaded inside the ImageView using the following code
PicassoOAuth
.get(context)
.load("https://my_profile_image.png")
.placeholder(R.drawable.stub)
.noFade()
.into(myImageView)
The picasso downloader code is shown below
import com.squareup.picasso.Downloader
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
class OAuthDownloader(val oauthService: MyAuthService) : Downloader {
override fun shutdown() {
}
override fun load(request: Request): Response {
val url = request.url.toString()
val buffer = ByteArrayOutputStream()
// oauthResponse is com.github.scribejava.core.model.Response
val oauthResponse = oauthService.consumer.getSignedGetResponse(url, null)
oauthResponse.stream.use { stream ->
stream.copyTo(buffer)
return Response.Builder()
.code(oauthResponse.code)
.protocol(Protocol.HTTP_1_1)
.request(Request.Builder().url(url).build())
.message(oauthResponse.message)
.body(buffer.toByteArray().toResponseBody())
.build()
}
}
}
object PicassoOAuth {
@SuppressLint("StaticFieldLeak")
private var instance: Picasso? = null
fun get(context: Context): Picasso {
if (instance == null) {
instance = Picasso.Builder(context).downloader(OAuthDownloader(MyAuthService.getInstance(context))).build()
}
return instance!!
}
}
https://coil-kt.github.io/coil/getting_started/#imageloaders
Specifically, something like this:
Coil.setDefaultImageLoader {
ImageLoader(context) {
crossfade(true)
okHttpClient {
OkHttpClient.Builder()
.cache(CoilUtils.createDefaultCache(context))
.addInterceptor { request ->
// add your custom logic here, read more about interceptors here:
// https://square.github.io/okhttp/interceptors/
}
.build()
}
}
}
Thank you very much, everything works fine now, the missing key was the addInterceptor
My ImageLoader can't replace the default one so now I will study the best way to "add" it side by side with the default one as a singleton but this is another story :)
Best of luck!
Most helpful comment
https://coil-kt.github.io/coil/getting_started/#imageloaders
Specifically, something like this: