I've an extension function to ImageView
class. I've implemented some logic on how to load an image according to arguments passed. But here I'm stuck.
These fit()
, centerCrop()
etc. return Picasso's RequestCreator
, which I can't even construct (it has package-private constructor) in order to modify it later (based on arguments).
I just don't know how to do this. The only way I managed to do this is as you can see below (Be warned: your eyes'll start bleeding). I can't find a "normal", "good" way to do it.
So I'm asking you: how should I approach doing this?
fun ImageView.load(resId: Int, centerCrop: Boolean = true, fit: Boolean = true) {
// Improve this, there must be a better way
if (centerCrop && fit) {
Picasso.get()
.load(resId)
.fit()
.centerCrop()
.into(this)
} else if (centerCrop && !fit) {
Picasso.get()
.load(resId)
.centerCrop()
.into(this)
} else if (!centerCrop && fit) {
Picasso.get()
.load(resId)
.fit()
.into(this)
} else if (!centerCrop && !fit) {
Picasso.get()
.load(resId)
.into(this)
}
}
You could use Kotlins also function:
fun ImageView.load(resId: Int, centerCrop: Boolean = true, fit: Boolean = true) {
Picasso.get()
.load(resId)
.also { if (centerCrop) it.centerCrop() }
.also { if (fit) it.fit() }
.into(this)
}