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

let
function is triggered then I see
qwe
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)
view.setBackgroundDrawable(wrap)
}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


Logcat:

qwe

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.