clock: stopwatch page (no background task/notification yet)
- Commit
137016f63cd5fed255bb26848cd45ad8eef7507f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-03-05 01:29 UTC (9 months ago)
- Parents
38187c5
Diff
clock/src/main/java/com/vayunmathur/clock/ui/ClockPage.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/ui/ClockPage.kt b/clock/src/main/java/com/vayunmathur/clock/ui/ClockPage.ktindex 296b97c55e..b7b8b327e9 100644--- a/clock/src/main/java/com/vayunmathur/clock/ui/ClockPage.kt+++ b/clock/src/main/java/com/vayunmathur/clock/ui/ClockPage.kt@@ -33,6 +33,7 @@ import com.vayunmathur.clock.Route import com.vayunmathur.library.ui.IconAdd import com.vayunmathur.library.util.BottomNavBar import com.vayunmathur.library.util.DataStoreUtils+import com.vayunmathur.library.util.nowState import kotlinx.coroutines.delay import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalTime@@ -50,12 +51,7 @@ import kotlin.time.Clock @OptIn(ExperimentalMaterial3Api::class) @Composable fun ClockPage(backStack: NavBackStack<Route>, ds: DataStoreUtils) {- val now by produceState(Clock.System.now()) {- while (true) {- value = Clock.System.now()- delay(100)- }- }+ val now by nowState() val time = now.toLocalDateTime(TimeZone.currentSystemDefault()) val timeZones by ds.stringSetFlow("time_zones").collectAsState(setOf()) Scaffold(topBar = {
clock/src/main/java/com/vayunmathur/clock/ui/StopwatchPage.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/ui/StopwatchPage.kt b/clock/src/main/java/com/vayunmathur/clock/ui/StopwatchPage.ktindex 606c1f859d..266f0adee1 100644--- a/clock/src/main/java/com/vayunmathur/clock/ui/StopwatchPage.kt+++ b/clock/src/main/java/com/vayunmathur/clock/ui/StopwatchPage.kt@@ -1,28 +1,235 @@ package com.vayunmathur.clock.ui +import androidx.compose.foundation.Canvas+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.itemsIndexed+import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.ExperimentalMaterial3Api+import androidx.compose.material3.ExtendedFloatingActionButton+import androidx.compose.material3.FloatingActionButton+import androidx.compose.material3.FloatingActionButtonMenu+import androidx.compose.material3.HorizontalDivider+import androidx.compose.material3.Icon+import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold+import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable+import androidx.compose.runtime.derivedStateOf+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableDoubleStateOf+import androidx.compose.runtime.mutableLongStateOf+import androidx.compose.runtime.mutableStateListOf+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.retain.retain+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.StrokeCap+import androidx.compose.ui.graphics.drawscope.Stroke+import androidx.compose.ui.res.painterResource+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.sp import androidx.navigation3.runtime.NavBackStack import com.vayunmathur.clock.MAIN_PAGES+import com.vayunmathur.clock.R import com.vayunmathur.clock.Route+import com.vayunmathur.library.ui.IconPause+import com.vayunmathur.library.ui.IconPlay import com.vayunmathur.library.util.BottomNavBar+import com.vayunmathur.library.util.nowState+import kotlin.time.Clock+import kotlin.time.Duration+import kotlin.time.Duration.Companion.seconds @OptIn(ExperimentalMaterial3Api::class) @Composable fun StopwatchPage(backStack: NavBackStack<Route>) {+ var isRunning by retain { mutableStateOf(false) }+ var totalTime by retain { mutableStateOf(0.seconds) }+ var startTime by retain { mutableStateOf(Clock.System.now()) }+ val now by nowState()+ val countingTime = if(isRunning) (now - startTime) + totalTime else totalTime+ val lapTimes = retain { mutableStateListOf<Duration>() }+ val lapSplits by remember {+ derivedStateOf {+ lapTimes.mapIndexed { index, totalTimeAtLap ->+ if (index == 0) {+ totalTimeAtLap // The first lap's length is just its end time+ } else {+ totalTimeAtLap - lapTimes[index - 1] // Subtract previous end time+ }+ }+ }+ }+ Scaffold(topBar = { TopAppBar({Text("Stopwatch")}) }, bottomBar = { BottomNavBar(backStack, MAIN_PAGES, Route.Stopwatch)+ }, floatingActionButton = {+ Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {+ if(isRunning) {+ FloatingActionButton({+ lapTimes += countingTime+ // add lap+ }) {+ Icon(painterResource(R.drawable.outline_timer_24), null)+ }+ }+ if(countingTime > 0.seconds) {+ FloatingActionButton(onClick = {+ isRunning = false+ totalTime = 0.seconds+ }) {+ Icon(painterResource(R.drawable.outline_restart_alt_24), null)+ }+ }+ FloatingActionButton({+ if(isRunning) {+ isRunning = false+ totalTime += Clock.System.now() - startTime+ } else {+ isRunning = true+ startTime = Clock.System.now()+ }+ }) {+ if(isRunning) {+ IconPause()+ } else {+ IconPlay()+ }+ }+ } }) { paddingValues ->- Column(Modifier.padding(paddingValues)) {+ Column(+ modifier = Modifier+ .fillMaxSize()+ .padding(paddingValues)+ .padding(horizontal = 16.dp),+ horizontalAlignment = Alignment.CenterHorizontally+ ) {+ // --- 1. CIRCULAR TIMER ---+ Box(+ modifier = Modifier+ .padding(top = 40.dp, bottom = 40.dp)+ .size(320.dp),+ contentAlignment = Alignment.Center+ ) {+ // Background Track+ Canvas(modifier = Modifier.fillMaxSize()) {+ drawCircle(color = Color.DarkGray.copy(alpha = 0.3f), style = Stroke(width = 8f))+ }++ // Sweeping Progress Arc (60-second loop)+ val sweepAngle = ((countingTime.inWholeMilliseconds % 60000) / 60000f) * 360f+ Canvas(modifier = Modifier.fillMaxSize()) {+ drawArc(+ color = Color.LightGray,+ startAngle = -90f,+ sweepAngle = sweepAngle,+ useCenter = false,+ style = Stroke(width = 12f, cap = StrokeCap.Round)+ )+ }++ // Time Display+ Column(horizontalAlignment = Alignment.CenterHorizontally) {+ val minutes = countingTime.inWholeMinutes+ val seconds = countingTime.inWholeSeconds % 60+ val centiseconds = (countingTime.inWholeMilliseconds % 1000) / 10++ Text(+ text = "$minutes:${seconds.toString().padStart(2, '0')}",+ style = MaterialTheme.typography.displayLarge.copy(+ fontSize = 84.sp,+ fontWeight = FontWeight.Normal+ )+ )+ Text(+ text = centiseconds.toString().padStart(2, '0'),+ style = MaterialTheme.typography.headlineMedium.copy(+ color = Color.Gray,+ fontWeight = FontWeight.Light+ )+ )+ }+ } + // --- 2. LAPS BOX ---+ Surface(+ modifier = Modifier.fillMaxWidth(),+ shape = RoundedCornerShape(12.dp),+ color = Color(0xFF1C1C1E) // Darker background for the box+ ) {+ Column {+ // Table Header+ Row(+ modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),+ horizontalArrangement = Arrangement.SpaceBetween+ ) {+ Text("Laps", Modifier.weight(1f), textAlign = TextAlign.Center, style = MaterialTheme.typography.labelLarge)+ Text("Split", Modifier.weight(1f), textAlign = TextAlign.Center, style = MaterialTheme.typography.labelLarge)+ Text("Total", Modifier.weight(1f), textAlign = TextAlign.Center, style = MaterialTheme.typography.labelLarge)+ }+ HorizontalDivider(Modifier.padding(horizontal = 8.dp), color = Color.Gray.copy(alpha = 0.5f))++ // Lap List+ LazyColumn(modifier = Modifier.heightIn(max = 240.dp)) {+ itemsIndexed(lapTimes.reversed()) { index, currentTotal ->+ val lapNumber = lapTimes.size - index+ val prevTotal = if (lapNumber > 1) lapTimes[lapNumber - 2] else 0.seconds+ val split = currentTotal - prevTotal+ val maxLength = lapSplits.max()+ val minLength = lapSplits.min()++ LapRow(lapNumber, when(split) {+ minLength -> Color.Green+ maxLength -> Color.Red+ else -> Color.White+ }, split, currentTotal)+ }+ }+ }+ } } }+}++@Composable+fun LapRow(number: Int, color: Color, split: Duration, total: Duration) {+ Row(+ modifier = Modifier+ .fillMaxWidth()+ .padding(horizontal = 16.dp, vertical = 10.dp),+ horizontalArrangement = Arrangement.SpaceBetween+ ) {+ Text("# $number", color = color, modifier = Modifier.weight(1f), textAlign = TextAlign.Center)+ Text(formatDuration(split), color = color, modifier = Modifier.weight(1f), textAlign = TextAlign.Center)+ Text(formatDuration(total), color = color, modifier = Modifier.weight(1f), textAlign = TextAlign.Center)+ }+}++fun formatDuration(d: Duration): String {+ val m = d.inWholeMinutes+ val s = d.inWholeSeconds % 60+ val ms = (d.inWholeMilliseconds % 1000) / 10+ if(m == 0L) {+ return "${s}.${ms.toString().padStart(2, '0')}"+ }+ return "$m:${s.toString().padStart(2, '0')}.${ms.toString().padStart(2, '0')}" }\ No newline at end of file
clock/src/main/res/drawable/outline_restart_alt_24.xml
diff --git a/clock/src/main/res/drawable/outline_restart_alt_24.xml b/clock/src/main/res/drawable/outline_restart_alt_24.xmlnew file mode 100644index 0000000000..bbd8f9acc5--- /dev/null+++ b/clock/src/main/res/drawable/outline_restart_alt_24.xml@@ -0,0 +1,5 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">+ + <path android:fillColor="@android:color/white" android:pathData="M440,838Q319,823 239.5,732.5Q160,642 160,520Q160,454 186,393.5Q212,333 260,288L317,345Q279,379 259.5,424Q240,469 240,520Q240,608 296,675.5Q352,743 440,758L440,838ZM520,838L520,758Q607,742 663.5,675Q720,608 720,520Q720,420 650,350Q580,280 480,280L477,280L521,324L465,380L325,240L465,100L521,156L477,200L480,200Q614,200 707,293Q800,386 800,520Q800,641 720.5,731.5Q641,822 520,838Z"/>+ +</vector>
library/src/main/java/com/vayunmathur/library/util/Util.kt
diff --git a/library/src/main/java/com/vayunmathur/library/util/Util.kt b/library/src/main/java/com/vayunmathur/library/util/Util.ktindex 97f1269a9e..c6e91a8a9f 100644--- a/library/src/main/java/com/vayunmathur/library/util/Util.kt+++ b/library/src/main/java/com/vayunmathur/library/util/Util.kt@@ -3,10 +3,15 @@ package com.vayunmathur.library.util import android.content.Context import android.content.ContextWrapper import androidx.activity.ComponentActivity+import androidx.compose.runtime.Composable+import androidx.compose.runtime.produceState+import kotlinx.coroutines.delay import okio.Source import okio.buffer import kotlin.math.pow import kotlin.math.round+import kotlin.time.Clock+import kotlin.time.Instant fun <T> tryOrDefault(default: T, block: () -> T): T { return try {@@ -44,4 +49,12 @@ inline fun <reified T: ComponentActivity> Context.findActivity(): T? { } data class Tuple3<A, B, C>(val first: A, val second: B, val third: C)-data class Tuple4<A, B, C, D>(val first: A, val second: B, val third: C, val fourth: D)\ No newline at end of file+data class Tuple4<A, B, C, D>(val first: A, val second: B, val third: C, val fourth: D)++@Composable+fun nowState() = produceState(Clock.System.now()) {+ while (true) {+ value = Clock.System.now()+ delay(100)+ }+}\ No newline at end of file