kibAr kibAr - 1 month ago 8
Android Question

Android kotlin infix issue

I getting an interesting problem. When I was debug my application isResColorId is false. Unfortunately

function is triggered then I see
on logcat.

fun drawableTint(context: Context, view: View, colorID: Int, isResColorId: Boolean = true) {
try {
val wrap = DrawableCompat.wrap(view.background)
DrawableCompat.setTint(wrap, isResColorId then let { Timber.d("qwe"); ContextCompat.getColor(context, colorID) } ?: colorID)
}catch (e: Resources.NotFoundException){
Timber.e(e, "c_id: $colorID coz: $isResColorId")

infix fun <T> Boolean.then(param: T): T? = if (this) param else null



Answer Source

To be sure about what is going on, you should print it inside of the lambda. But what is probably happening is:

isResColorId.then(this.let({...})) ?: colorId

This means the block inside let is always evaluated.