https://kotlinlang.org logo
Title
p

pavi2410

07/20/2019, 11:04 AM
Thanks Kotlin for making this utility function soooooo simple 🤗🙃😋
fun ImageView.loadImage(
        url: String?,
        @DrawableRes placeholder: Int? = null,
        circular: Boolean = false,
        roundedCorners: Int = 0
) {
    Glide.with(context)
            .load(url)
            .apply {
                placeholder?.let { apply(RequestOptions().placeholder(it)) }
                if (circular) {
                    apply(RequestOptions.circleCropTransform())
                } else if (roundedCorners != 0) {
                    apply(RequestOptions.bitmapTransform(RoundedCorners(context.dip(roundedCorners))))
               }
            }
            .into(this)
}
I would love to get any suggestions/improvements to this function.
d

Dominaezzz

07/20/2019, 11:19 AM
fun ImageView.loadImage(
        url: String?,
        @DrawableRes placeholder: Int? = null,
        circular: Boolean = false,
        roundedCorners: Int = 0
) {
    with(Glide.with(context)) {
        load(url)
        placeholder?.let { apply(RequestOptions().placeholder(it)) }
        if (circular) {
            apply(RequestOptions.circleCropTransform())
        } else if (roundedCorners != 0) {
            apply(RequestOptions.bitmapTransform(RoundedCorners(context.dip(roundedCorners))))
        }
        into(this@loadImage)
    }
}
More of an alternative, than an improvement I guess.
p

pavi2410

07/20/2019, 1:38 PM
this@loadImage
though 😣
d

Dominaezzz

07/20/2019, 2:03 PM
fun ImageView.loadImage(
        url: String?,
        @DrawableRes placeholder: Int? = null,
        circular: Boolean = false,
        roundedCorners: Int = 0
) {
    with(Glide.with(context)) {
        load(url)
        placeholder?.let { apply(RequestOptions().placeholder(it)) }
        if (circular) {
            apply(RequestOptions.circleCropTransform())
        } else if (roundedCorners != 0) {
            apply(RequestOptions.bitmapTransform(RoundedCorners(context.dip(roundedCorners))))
        }
    }.into(this)
}
🙃 1
😂 3
👍 1
🙂
k

KindRoacher

07/21/2019, 8:19 AM
4 params for one function? Is it not it too much? Generally, I never cross 3 params, ideally go for max 2 params for a clean code
j

Jacob Applin

07/21/2019, 2:11 PM
More params mean more utility though /s
p

pavi2410

07/21/2019, 6:57 PM
The use of named args with default values makes it cleaner.
d

Dominaezzz

07/21/2019, 7:00 PM
Might as well make a DSL too.
k

Kirill Grouchnikov

07/21/2019, 11:32 PM
Don't go overboard with DSLs. It takes ages to compile code that is using non trivial DSL parts
So something like 3-4 levels nested makes the compiler extra busy
p

prasham.h.trivedi

07/22/2019, 6:08 AM
Also, avoid using
Glide.with(context)
directly. If you can, generate
RequestManager
using
Glide.with(this)
in your
Activities
or
Fragments
. This step will help you a lot to reclaim memory when your fragment or activity is out of lifecycle.