Extended Usage

Coil allows you to extend its functionality so that you can adapt it to your own needs.

firecoil can also be extended with custom components such as Mappers and/or Decoders.

You simply need to add these components to the ImageLoader through its componentRegistry:

val imageLoader = ImageLoader.Builder(context)
    .componentRegistry {
        // This is the core of firecoil. Don't forget to add it
        add(StorageReferenceKeyer())
        add(StorageReferenceFetcher.Factory())

        // Add other Mappers and/or Decoders:
        add(ProductMapper())
        add(GifDecoder.Factory())
    }
    .build()

Creating Custom Mappers

As mentioned on the Coil Docs, Mappers allow you to add support for custom data types.

Suppose you're fetching this model from your server:

data class Product(
    val id: Long,
    val title: String,
    val product_type: String,
    // Suppose this is the path to the image in Cloud Storage
    val imageRef: String
)

You could write a custom mapper:

class ProductMapper : Mapper<Product, StorageReference> {
    override fun map(data: Product): StorageReference {
        return Firebase.storage.reference.child(data.imageRef)
    }
}

And display it on an ImageView using:

val product: Product = ...

val imageLoader = ImageLoader.Builder(context)
    .componentRegistry {
        // This is the core of firecoil. Don't forget to add it
        add(StorageReferenceKeyer())
        add(StorageReferenceFetcher.Factory())

        // Add the custom Mapper:
        add(ProductMapper())
    }
    .build()

imageView.loadAny(product, imageLoader)

Using Decoders

Unlike other image loading libraries, Coil doesn't support some formats by default, like GIF and SVG, so you need to use your own decoders in order to do that.

For example, to add GIF support, add the Gif Decoder dependency to your gradle file:

implementation "io.coil-kt:coil-gif:0.9.5"

and add it to your ImageLoader:

val imageLoader = ImageLoader.Builder(context)
    .componentRegistry {
        // This is the core of firecoil. Don't forget to add it
        add(StorageReferenceKeyer())
        add(StorageReferenceFetcher.Factory())

        // Add the Gif Decoder:
        if (SDK_INT >= P) {
            add(ImageDecoder.Factory())
        } else {
            add(GifDecoder.Factory())
        }
    }
    .build()