|
| 1 | +package com.aliucord.manager.ui.screens.install.components |
| 2 | + |
| 3 | +import androidx.compose.animation.Crossfade |
| 4 | +import androidx.compose.animation.core.* |
| 5 | +import androidx.compose.foundation.layout.size |
| 6 | +import androidx.compose.material3.* |
| 7 | +import androidx.compose.runtime.Composable |
| 8 | +import androidx.compose.runtime.getValue |
| 9 | +import androidx.compose.ui.Modifier |
| 10 | +import androidx.compose.ui.graphics.Color |
| 11 | +import androidx.compose.ui.res.painterResource |
| 12 | +import androidx.compose.ui.res.stringResource |
| 13 | +import androidx.compose.ui.semantics.contentDescription |
| 14 | +import androidx.compose.ui.semantics.semantics |
| 15 | +import androidx.compose.ui.unit.Dp |
| 16 | +import com.aliucord.manager.R |
| 17 | +import com.aliucord.manager.installer.steps.base.StepState |
| 18 | +import kotlin.math.floor |
| 19 | +import kotlin.math.roundToInt |
| 20 | + |
| 21 | +@Composable |
| 22 | +fun StepStateIcon( |
| 23 | + state: StepState, |
| 24 | + size: Dp, |
| 25 | + stepProgress: Float = -1f, |
| 26 | +) { |
| 27 | + val animatedProgress by animateFloatAsState( |
| 28 | + targetValue = stepProgress, |
| 29 | + animationSpec = spring(stiffness = Spring.StiffnessVeryLow), |
| 30 | + label = "Progress", |
| 31 | + ) |
| 32 | + |
| 33 | + Crossfade(targetState = state, label = "State CrossFade") { animatedState -> |
| 34 | + when (animatedState) { |
| 35 | + StepState.Pending -> Icon( |
| 36 | + painter = painterResource(R.drawable.ic_circle), |
| 37 | + contentDescription = stringResource(R.string.status_queued), |
| 38 | + tint = MaterialTheme.colorScheme.onSurface.copy(0.4f), |
| 39 | + modifier = Modifier.size(size) |
| 40 | + ) |
| 41 | + |
| 42 | + StepState.Running -> { |
| 43 | + val strokeWidth = Dp(floor(size.value / 10) + 1) |
| 44 | + |
| 45 | + if (stepProgress > .05f) { |
| 46 | + CircularProgressIndicator( |
| 47 | + progress = { animatedProgress }, |
| 48 | + strokeWidth = strokeWidth, |
| 49 | + modifier = Modifier |
| 50 | + .size(size) |
| 51 | + .semantics { contentDescription = "${(stepProgress * 100).roundToInt()}%" }, |
| 52 | + ) |
| 53 | + } else { |
| 54 | + val description = stringResource(R.string.status_ongoing) |
| 55 | + |
| 56 | + // Infinite spinner |
| 57 | + CircularProgressIndicator( |
| 58 | + strokeWidth = strokeWidth, |
| 59 | + modifier = Modifier |
| 60 | + .size(size) |
| 61 | + .semantics { contentDescription = description }, |
| 62 | + ) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + StepState.Success -> Icon( |
| 67 | + painter = painterResource(R.drawable.ic_check_circle), |
| 68 | + contentDescription = stringResource(R.string.status_success), |
| 69 | + tint = Color(0xFF59B463), |
| 70 | + modifier = Modifier.size(size) |
| 71 | + ) |
| 72 | + |
| 73 | + StepState.Error -> Icon( |
| 74 | + painter = painterResource(R.drawable.ic_canceled), |
| 75 | + contentDescription = stringResource(R.string.status_failed), |
| 76 | + tint = MaterialTheme.colorScheme.error, |
| 77 | + modifier = Modifier.size(size) |
| 78 | + ) |
| 79 | + |
| 80 | + StepState.Skipped -> Icon( |
| 81 | + painter = painterResource(R.drawable.ic_check_circle), |
| 82 | + contentDescription = stringResource(R.string.status_skipped), |
| 83 | + tint = Color(0xFFAEAEAE), |
| 84 | + modifier = Modifier.size(size) |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments