Marcin Wisniowski
05/26/2023, 2:22 PMvar list: List<Item>
, what would be an idiomatic way of replacing one item of this list? It’s not a MutableList, so I need a way to generate a new copy of this list, with a single element replaced.Marcin Wisniowski
05/26/2023, 2:24 PMlist = list.take(indexOfItem) + newItem + list.drop(indexOfItem + 1)
but it feels like it could be simpler.Adam S
05/26/2023, 2:25 PMMarcin Wisniowski
05/26/2023, 2:25 PMMarcin Wisniowski
05/26/2023, 2:26 PMitems = items.map { if (it == oldItem) newItem else it }
Adam S
05/26/2023, 2:27 PMAdam S
05/26/2023, 2:27 PMrequire(oldItem in items)
checkAdam S
05/26/2023, 2:28 PMList<T>
via delegationMarcin Wisniowski
05/26/2023, 2:30 PMvar items by remember { mutableStateOf(listOf(originalContents...)) }
that I need mutate in response to click handlers in Compose. Normally the handlers would go up higher but in this case it’s a Compose @Preview.Joffrey
05/26/2023, 2:41 PMmutate
extension which simplifies this kind of operationJoffrey
05/26/2023, 2:42 PMCLOVIS
05/26/2023, 2:46 PMval items = remember { mutableStateListOf(originalContents) }
then use the regular mutable replace
. Compose will still recompose correctly.Marcin Wisniowski
05/26/2023, 3:39 PMreplace
method? I’m not seeing it. There is replaceAll
which looks like it would solve it, but sadly it requires API level 24 on Android.ephemient
05/26/2023, 3:41 PMephemient
05/26/2023, 3:41 PMval newList = buildList(list.size) {
addAll(list)
set(indexOfItem, newItem)
}
CLOVIS
05/26/2023, 4:02 PMset
, not replace
Marcin Wisniowski
05/26/2023, 4:19 PM