From a6c1a992a02b31e955318be39904bcbf0ecb209a Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Wed, 15 Nov 2023 16:56:05 -0700 Subject: [PATCH] create a LanguageDownloadProgressIndicator for the downloadable languages UI --- ...nguageDownloadProgressIndicator+Preview.kt | 23 +++++++ .../LanguageDownloadProgressIndicator.kt | 65 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator+Preview.kt create mode 100644 app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator.kt diff --git a/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator+Preview.kt b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator+Preview.kt new file mode 100644 index 0000000000..77cc331859 --- /dev/null +++ b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator+Preview.kt @@ -0,0 +1,23 @@ +package org.cru.godtools.ui.languages.downloadable + +import androidx.compose.runtime.Composable +import androidx.compose.ui.tooling.preview.Preview +import org.cru.godtools.base.ui.theme.GodToolsTheme + +@Preview +@Composable +private fun LanguageDownloadProgressIndicatorNotPinned() { + GodToolsTheme { LanguageDownloadProgressIndicator(isPinned = false, downloaded = 0, total = 5) } +} + +@Preview +@Composable +private fun LanguageDownloadProgressIndicatorInProgress() { + GodToolsTheme { LanguageDownloadProgressIndicator(isPinned = true, downloaded = 2, total = 5) } +} + +@Preview +@Composable +private fun LanguageDownloadProgressIndicatorDownloaded() { + GodToolsTheme { LanguageDownloadProgressIndicator(isPinned = true, downloaded = 5, total = 5) } +} diff --git a/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator.kt b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator.kt new file mode 100644 index 0000000000..c155dca746 --- /dev/null +++ b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator.kt @@ -0,0 +1,65 @@ +package org.cru.godtools.ui.languages.downloadable + +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.CheckCircle +import androidx.compose.material.icons.outlined.DownloadForOffline +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp + +@Composable +internal fun LanguageDownloadProgressIndicator( + isPinned: Boolean, + downloaded: Int, + total: Int, + modifier: Modifier = Modifier, + iconSize: Dp = 24.dp, +) { + val total = total.coerceAtLeast(0) + val downloaded = downloaded.coerceIn(0, total) + val contentModifier = modifier.size(iconSize) + + when { + !isPinned -> Icon( + Icons.Outlined.DownloadForOffline, + null, + modifier = contentModifier, + tint = MaterialTheme.colorScheme.outline, + ) + downloaded == total -> Icon( + Icons.Outlined.CheckCircle, + null, + modifier = contentModifier, + tint = MaterialTheme.colorScheme.primary, + ) + else -> { + val progress by animateFloatAsState( + label = "Download Progress", + targetValue = when (total) { + 0 -> 1f + else -> downloaded.toFloat() / total + }, + ) + + val iconPadding = iconSize / 12 + CircularProgressIndicator( + progress = progress, + color = MaterialTheme.colorScheme.primary, + strokeWidth = (iconSize / 2) - iconPadding, + modifier = contentModifier + .padding(iconPadding) + .border(iconSize / 12, MaterialTheme.colorScheme.primary, CircleShape) + ) + } + } +}