Last active
May 31, 2022 11:14
-
-
Save dilrajsingh1997/44c1119d0a2244fb2c23e1d0598d919b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
delay(10) | |
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++ | |
} | |
itemsToAnimate.value = items.toMutableList() | |
} | |
} | |
} | |
} | |
} | |
///// | |
data class ItemState( | |
val x: Float, | |
val y: Float, | |
val xAnimation: Animation<Float, AnimationVector1D>, | |
val yAnimation: Animation<Float, AnimationVector1D>, | |
val startTime: Long, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment