david.bilik
09/15/2017, 6:52 AMpablisco
09/15/2017, 10:42 AMdata class LoadedImage(val url: String, val fallback: Int = 0) {
companion object {
operator fun invoke(url: String, config: LoadedImage.() -> Unit) =
LoadedImage(url).apply(config)
val LOADED_IMAGE_ID = View.generateViewId()
}
}
var ImageView.image: LoadedImage
get() = getTag(LoadedImage.LOADED_IMAGE_ID) as LoadedImage
set(value) {
setTag(LoadedImage.LOADED_IMAGE_ID, value)
Glide.with(this)
.load(value.url)
.apply(RequestOptions().fallback(value.fallback))
.into(this)
}
This let’s you move the logic out of the core code and if decide to move to another loading library don’t have to change the whole app. And a bit more convenient than using injection 😉