Coil: Use placeholderRes/errorRes if url is null

Created on 22 Aug 2019  路  5Comments  路  Source: coil-kt/coil

It would be cool if passing null url would use placeholder drawable or error drawable if set. Right now it defeats the purpose of fluent api when you have to do something like:

if (imageUrl != null) { it.load(imageUrl) { placeholder(placeholderRes) } } else { it.setImageResource(placeholderRes) }

enhancement

Most helpful comment

@mlilienberg Great point. I think it makes sense to use the same API as Glide. i.e.:

imageView.load(itemModel.imageUrl) {
   placeholder(R.drawable.ic_placeholder)
   error(R.drawable.ic_error)
   fallback(R.drawable.ic_error)
}

Similar to placeholder and error, you should be able to set the default fallback on your ImageLoader.

All 5 comments

To help design this, if you run:

imageView.load(null) {
    placeholder(R.drawable.placeholder)
    error(R.drawable.error)
}

Would you expect it to show the placeholder drawable or the error drawable?

To me it would make more sense to use error when defined, and placehold as a fallback. Same goes for the case where you do have an url and there's an error.

I saw multiple points of view about this topic. Some people think that null is a valid case and should show nothing. Maybe it makes more sense to define your own null strategy.

imageView.load(itemModel.imageUrl) {
   placeholder(R.drawable.ic_placeholder)
   error(R.drawable.ic_error)
   isNull(R.drawable.ic_null)
}

or

imageView.load(itemModel.imageUrl) {
   placeholder(R.drawable.ic_placeholder)
   error(R.drawable.ic_error)
   isNull(null)
}

Related topic in glide a few years ago:
https://github.com/bumptech/glide/issues/268

@mlilienberg Great point. I think it makes sense to use the same API as Glide. i.e.:

imageView.load(itemModel.imageUrl) {
   placeholder(R.drawable.ic_placeholder)
   error(R.drawable.ic_error)
   fallback(R.drawable.ic_error)
}

Similar to placeholder and error, you should be able to set the default fallback on your ImageLoader.

This is now supported in master. To get the fix asap you can depend on 0.9.0-SNAPSHOT.

NOTE: There might be minor behaviour changes to the feature before 0.9.0 stable.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dafi picture dafi  路  5Comments

ghost picture ghost  路  4Comments

dafi picture dafi  路  3Comments

billyjoker picture billyjoker  路  5Comments

theScrabi picture theScrabi  路  5Comments