therealbluepandabear
10/29/2021, 11:41 AMTobias Suchalla
10/29/2021, 11:56 AMwith
is idiomatically 'wrong', you would rather use apply
here because you are directly editing the object:
binding.apply {
mImageView.setImageBitmap(item.bitmap)
mdate.text = item.dateCreated
mtext.apply {
if (data[position].title.length > 6) {
ellipsize = TextUtils.TruncateAt.MARQUEE
isSelected = true
isSingleLine = true
text = (item.title + " ".repeat(10)).repeat(200)
} else {
text = item.title
}
}
}
Other than that, apply
is perfectly valid and idiomatic here. If you actually want the two levels of apply
here is up to you.
with
, however, is rather for situations where you want to bring an object into scope for other calculations (i.e., more or less read only):
val myColor = with(MaterialTheme.colors) {
if (myCondition) primary else secondary
}
therealbluepandabear
10/29/2021, 12:02 PMJoffrey
11/19/2021, 12:42 PMwith
or apply
is long and mixed in other business logic like this, I would actually really go for an extension function insteadtherealbluepandabear
02/05/2022, 12:28 AM