Skip to content

Instantly share code, notes, and snippets.

@dilrajsingh1997
Last active June 28, 2022 18:02
Show Gist options
  • Save dilrajsingh1997/e7c614d46da83210be112dce9763f8e0 to your computer and use it in GitHub Desktop.
Save dilrajsingh1997/e7c614d46da83210be112dce9763f8e0 to your computer and use it in GitHub Desktop.
val itemsToAnimate = remember {
mutableStateOf<List<ItemState>>(mutableListOf())
}
val itemsChannel = remember {
Channel<List<ItemState>>(Channel.UNLIMITED)
}
var job: Job? = remember {
null
}
LaunchedEffect(true) {
for (_items in itemsChannel) {
job?.cancel()
job = launch {
withContext(Dispatchers.IO) {
val items = _items.toMutableList()
while (true) {
withFrameMillis {
val iterator = items.iterator()
var i = 0
while (iterator.hasNext()) {
val element = iterator.next() //element is ItemState
val elapsedTime = System.nanoTime() - element.startTime
items.safeSet(i) {
it.copy(
y = element.yAnimation.getValueFromNanos(counter),
x = element.xAnimation.getValueFromNanos(counter),
)
i++
}
hearts.value = items.toMutableList()
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment