https://kotlinlang.org logo
#android
Title
# android
d

david.bilik

09/15/2017, 6:52 AM
extension property is pretty constraining, because you may want to pass callback, transformations etc. With function, you can make parameters with default values but with property you are done
👍 1
p

pablisco

09/15/2017, 10:42 AM
true, I was just thinking of a global way to do this
Also, something like this:
Copy code
data 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 😉