Skip to content

Commit

Permalink
create a LanguageDownloadProgressIndicator for the downloadable langu…
Browse files Browse the repository at this point in the history
…ages UI
  • Loading branch information
frett committed Nov 16, 2023
1 parent 58f9905 commit a6c1a99
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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) }
}
Original file line number Diff line number Diff line change
@@ -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)
)
}
}
}

0 comments on commit a6c1a99

Please sign in to comment.