orangy
05/01/2023, 7:34 PMmap
function on SnapshotStateList
instance that would be itself state list
(but not mutable), so that it only calls map’s lambda when original list changes? Kinda like val files = mutableStateListOf("/path/to/file1", "/path/to/file2"); val loadedFiles = files.map { loadFile(it) }
and so that it calls loadFile
only on inserted/modified elements of files
.dbaelz
05/01/2023, 8:27 PMderivedStateOf
and toMutableStateList
could work. It's still a mutable state list, but should change when the files state list changes.
val files = remember { mutableStateListOf("/path/to/file1", "/path/to/file2") }
val loadedFile = remember {
derivedStateOf {
files.map { path ->
File(path)
}
}.value.toMutableStateList()
}
orangy
05/01/2023, 8:37 PMmap
there is just an ordinary collection function.Zach Klippenstein (he/him) [MOD]
05/01/2023, 9:56 PMorangy
05/01/2023, 9:57 PMZach Klippenstein (he/him) [MOD]
05/02/2023, 5:09 PMval loadedFile = remember(files) { MappingList(files) { doTransform(it) } }
class MappingList<I, O>(
private val source: SnapshotStateList<I>,
private val transform: (I) -> O
): List<O> {
override val size get() = source.size
override fun get(i: Int) = transform(source[i])
// etc
}
orangy
05/02/2023, 5:30 PMSnapshotStateList<T>
as a source, I would like to synchronize it to another SnapshotStateList<R>
with a transformation function T->R
. So that if an item is added to the source, the transformation function is called just once on the new value and it is placed in the target. If the item is removed, it is removed from both lists (by index, since they are synced). If the item is updated, the transformation function is called and value is replaced at target by index again.Zach Klippenstein (he/him) [MOD]
05/02/2023, 5:31 PMmikehearn
05/03/2023, 9:10 AM