Coil: How to migrate picasso Downloader to coil?

Created on 31 Jan 2020  路  3Comments  路  Source: coil-kt/coil

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!!
    }
}

question

Most helpful comment

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()
        }
    }
}

All 3 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Starscream9559 picture Starscream9559  路  7Comments

ZacSweers picture ZacSweers  路  4Comments

ianhanniballake picture ianhanniballake  路  3Comments

cioccarellia picture cioccarellia  路  3Comments

gastsail picture gastsail  路  4Comments