I've created a bit of a mess in me compose desktop...
# compose
p
I've created a bit of a mess in me compose desktop app. I am unsure how to connect my suspending download logic (App can run x downloaders in parallel) with the UI. Currently UI does not recompose with the updated list of active downloading items (the items have progress and download speed ect inside them, which does cause recomposition when updated somehow). My current suspending downloader stuff run inside a
LaunchedEffect(Unit)
which probably causes the issue. Could someone point me to some quick resources to work that'd help me fix my design ?
Here's a snippet for context idk if it's helpful at all
Copy code
private 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)
                }
            }
        }
    }
p
Thanks, I was not aware of that 😕
I solved pretty much all the issues I was having now thanks ❤️
👍 1