coil-kt / coil

Image loading for Android and Compose Multiplatform.

Home Page:https://coil-kt.github.io/coil/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Call existing Fetcher from a custom Fetcher

AChep opened this issue · comments

Is your feature request related to a problem? Please describe.

   HTTP request             HTTP request
X ------------> String ------------> Image

The current architecture with Mappers and Fetchers does not give me enough flexibility. It seems like the only option here is to create a custom Fetcher that duplicates the functionality of the internal HttpUriFetcher and also does the first data conversion request too.

It's already possible to call an existing fetcher using an ImageLoader's ComponentRegistry in Coil 2.x. E.g.:

class CustomFetcher(data: Uri, options: Options, imageLoader: ImageLoader) {

    override suspend fun fetch(): FetchResult? {
        // Pass 1 to skip our custom fetcher.
        val (fetcher) = imageLoader.components.newFetcher(data, options, imageLoader, startIndex = 1)
        val result = fetcher.fetch()
        return result
    }

    class Factory : Fetcher.Factory<Uri> {

        override fun create(data: Uri, options: Options, imageLoader: ImageLoader): Fetcher? {
            if (data.scheme != "http" && data.scheme != "https") return null
            return CustomFetcher(data, options, imageLoader)
        }
    }
}