diff --git a/src/main/kotlin/com/mineinabyss/launchy/logic/Downloader.kt b/src/main/kotlin/com/mineinabyss/launchy/logic/Downloader.kt index d012f3c..e0c062b 100644 --- a/src/main/kotlin/com/mineinabyss/launchy/logic/Downloader.kt +++ b/src/main/kotlin/com/mineinabyss/launchy/logic/Downloader.kt @@ -18,9 +18,10 @@ object Downloader { writeTo: Path, onProgressUpdate: (progress: Progress) -> Unit = {}, ) { + val startTime = System.currentTimeMillis() val response = httpClient.get(url) { onDownload { bytesSentTotal, contentLength -> - onProgressUpdate(Progress(bytesSentTotal, contentLength)) + onProgressUpdate(Progress(bytesSentTotal, contentLength, timeElapsed = System.currentTimeMillis() - startTime)) } }.receive() writeTo.parent.createDirectories() @@ -30,7 +31,7 @@ object Downloader { } } -data class Progress(val bytesDownloaded: Long, val totalBytes: Long) { +data class Progress(val bytesDownloaded: Long, val totalBytes: Long, val timeElapsed : Long) { val percent: Float get() = bytesDownloaded.toFloat() / totalBytes.toFloat() } diff --git a/src/main/kotlin/com/mineinabyss/launchy/logic/LaunchyState.kt b/src/main/kotlin/com/mineinabyss/launchy/logic/LaunchyState.kt index e2e235a..ef800b5 100644 --- a/src/main/kotlin/com/mineinabyss/launchy/logic/LaunchyState.kt +++ b/src/main/kotlin/com/mineinabyss/launchy/logic/LaunchyState.kt @@ -91,6 +91,13 @@ class LaunchyState( val downloadingConfigs = mutableStateMapOf() val isDownloading by derivedStateOf { downloading.isNotEmpty() || downloadingConfigs.isNotEmpty() } + // Caclculate the speed of the download + val downloadSpeed by derivedStateOf { + val total = downloading.values.sumOf { it.bytesDownloaded } + val time = downloading.values.sumOf { it.timeElapsed } + if (time == 0L) 0 else total / time + } + fun isDownloading(mod: Mod) = downloading[mod] != null || downloadingConfigs[mod] != null var installingProfile by mutableStateOf(false) @@ -179,7 +186,8 @@ class LaunchyState( suspend fun download(mod: Mod) { runCatching { - downloading[mod] = Progress(0, 0) // set progress to 0 + println("Starting download of ${mod.name}") + downloading[mod] = Progress(0, 0, 0) // set progress to 0 Downloader.download(url = mod.url, writeTo = mod.file) progress@{ downloading[mod] = it } @@ -202,6 +210,8 @@ class LaunchyState( // "Failed to download ${mod.name}: ${it.localizedMessage}!", "OK" // ) }.onSuccess { + val downloadInfo = downloading[mod]!! + println("Finished downloading ${mod.name} in ${downloadInfo.timeElapsed}ms, ${downloadInfo.bytesDownloaded.toFloat() / 1024}kb") downloading -= mod downloadingConfigs -= mod } diff --git a/src/main/kotlin/com/mineinabyss/launchy/ui/screens/settings/InfoBar.kt b/src/main/kotlin/com/mineinabyss/launchy/ui/screens/settings/InfoBar.kt index 6ddf9cf..8c665c1 100644 --- a/src/main/kotlin/com/mineinabyss/launchy/ui/screens/settings/InfoBar.kt +++ b/src/main/kotlin/com/mineinabyss/launchy/ui/screens/settings/InfoBar.kt @@ -70,6 +70,19 @@ fun InfoBar(modifier: Modifier = Modifier) { desc = "Will remove", extra = state.queuedDeletions.size.toString() ) + Spacer(Modifier.width(10.dp).weight(1f)) + + if (state.isDownloading) { + // Show download progress + val totalBytesToDownload = + state.downloading.values.sumOf { it.totalBytes } + state.downloadingConfigs.values.sumOf { it.totalBytes } + val totalBytesDownloaded = + state.downloading.values.sumOf { it.bytesDownloaded } + state.downloadingConfigs.values.sumOf { it.bytesDownloaded } + Text( + text = "Downloading ${state.downloading.size + state.downloadingConfigs.size} files (${totalBytesDownloaded / 1000} / ${totalBytesToDownload / 1000} KB)", + style = MaterialTheme.typography.bodySmall, + ) + } // var path by remember { mutableStateOf("") } // Button(onClick = {