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)