PixelHamster
04/25/2024, 2:42 AMLaunchedEffect(Unit)
which probably causes the issue. Could someone point me to some quick resources to work that'd help me fix my design ?PixelHamster
04/25/2024, 2:44 AMprivate val downloadChannel = mutableStateOf(ConcurrentLinkedQueue<DiskItem>())
private val activeDownloads = mutableStateOf(CopyOnWriteArrayList<DiskItem>())
private val finishedDownloads = mutableStateOf(CopyOnWriteArrayList<DiskItem>())
@Composable
fun superviseDownloads(
onStateChange: (MainState) -> Unit
) {
val downloadChannel by remember { downloadChannel }
val activeDownloads by remember { activeDownloads }
val finishedDownloads by remember { finishedDownloads }
LaunchedEffect(Unit) {
CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch {
while (true) {
val toRemove = activeDownloads.filter {
val downloadProgress by it.item.downloadProgress
downloadProgress < 0.0 || downloadProgress >= 1.0
}
activeDownloads.removeAll(toRemove)
finishedDownloads.addAll(toRemove)
if (activeDownloads.size < 2) {
val diskItem = downloadChannel.poll() ?: continue
activeDownloads.add(diskItem)
diskItem.download()
}
if (finishedDownloads.isNotEmpty() && activeDownloads.isEmpty() && downloadChannel.isEmpty()) {
onStateChange(MainState.FINISHED)
}
delay(5)
}
}
}
}
Albert Chang
04/25/2024, 2:56 AMPixelHamster
04/25/2024, 3:47 AMPixelHamster
04/25/2024, 4:19 AM