TOTPRing.kt

1.8 KB · raw

1package com.peachpasswords.ui.components23import androidx.compose.foundation.Canvas4import androidx.compose.foundation.layout.size5import androidx.compose.runtime.Composable6import androidx.compose.runtime.getValue7import androidx.compose.runtime.produceState8import androidx.compose.ui.Modifier9import androidx.compose.ui.geometry.Offset10import androidx.compose.ui.geometry.Size11import androidx.compose.ui.graphics.StrokeCap12import androidx.compose.ui.graphics.drawscope.Stroke13import androidx.compose.ui.unit.Dp14import androidx.compose.ui.unit.dp15import com.peachpasswords.ui.theme.LocalPeachAccent16import com.peachpasswords.ui.theme.PeachColors17import com.peachpasswords.vault.TOTPGenerator18import kotlinx.coroutines.delay1920/**21 * A small ring that sweeps once per TOTP period and turns breach-red in the22 * final 5 seconds. Port of the brief's TOTPRing.23 */24@Composable25fun TOTPRing(modifier: Modifier = Modifier, size: Dp = 28.dp) {26    val accent = LocalPeachAccent.current27    val progress by produceState(TOTPGenerator.periodProgress()) {28        while (true) { value = TOTPGenerator.periodProgress(); delay(100) }29    }30    val isHot = TOTPGenerator.remainingSeconds() <= 53132    Canvas(modifier = modifier.size(size)) {33        val stroke = 2.dp.toPx()34        val radius = (this.size.minDimension - stroke) / 2f35        val center = Offset(this.size.width / 2f, this.size.height / 2f)36        drawCircle(color = PeachColors.T3, radius = radius, style = Stroke(stroke))37        drawArc(38            color = if (isHot) PeachColors.Breach else accent,39            startAngle = -90f,40            sweepAngle = 360f * progress,41            useCenter = false,42            style = Stroke(stroke, cap = StrokeCap.Round),43            topLeft = Offset(center.x - radius, center.y - radius),44            size = Size(radius * 2, radius * 2),45        )46    }47}