Skip to content

Commit

Permalink
fix: add text download percent
Browse files Browse the repository at this point in the history
  • Loading branch information
DevScyu committed Nov 6, 2022
1 parent 2fe07a8 commit f764188
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/main/kotlin/com/mineinabyss/launchy/logic/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ object Downloader {
writeTo: Path,
onProgressUpdate: (progress: Progress) -> Unit = {},
) {
val startTime = System.currentTimeMillis()
val response = httpClient.get<HttpStatement>(url) {
onDownload { bytesSentTotal, contentLength ->
onProgressUpdate(Progress(bytesSentTotal, contentLength))
onProgressUpdate(Progress(bytesSentTotal, contentLength, timeElapsed = System.currentTimeMillis() - startTime))
}
}.receive<ByteArray>()
writeTo.parent.createDirectories()
Expand All @@ -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()
}
12 changes: 11 additions & 1 deletion src/main/kotlin/com/mineinabyss/launchy/logic/LaunchyState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ class LaunchyState(
val downloadingConfigs = mutableStateMapOf<Mod, Progress>()
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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit f764188

Please sign in to comment.