From d9109c9a4cb54711822543b25e64e124c826d204 Mon Sep 17 00:00:00 2001 From: kantahrek <152828461+kantahrek@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:42:43 -0800 Subject: [PATCH] Changed activity 'Since' labels to live time tracking --- .../presentation/components/ActivityChip.kt | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/wear/src/main/java/com/example/util/simpletimetracker/presentation/components/ActivityChip.kt b/wear/src/main/java/com/example/util/simpletimetracker/presentation/components/ActivityChip.kt index 451ab112..e9acb375 100644 --- a/wear/src/main/java/com/example/util/simpletimetracker/presentation/components/ActivityChip.kt +++ b/wear/src/main/java/com/example/util/simpletimetracker/presentation/components/ActivityChip.kt @@ -11,6 +11,11 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.semantics.contentDescription @@ -18,6 +23,7 @@ import androidx.compose.ui.semantics.semantics import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import androidx.wear.compose.material.SplitToggleChip import androidx.wear.compose.material.Switch import androidx.wear.compose.material.SwitchDefaults @@ -26,11 +32,17 @@ import androidx.wear.compose.material.ToggleChipDefaults import androidx.wear.tooling.preview.devices.WearDevices import com.example.util.simpletimetracker.wearrpc.Activity import com.example.util.simpletimetracker.wearrpc.Tag +import kotlinx.coroutines.time.delay +import java.time.Duration import java.time.Instant import java.time.LocalDateTime import java.time.ZoneId import java.time.format.DateTimeFormatter +private const val ISO_HOURS_MINUTES_PARTS_REGEX = "(\\d[HM])(?!$)" +private const val DECIMAL_SEPARATOR_AND_FRACTIONAL_PART_REGEX = "\\.\\d+" +private const val ISO_MISSING_MINUTES_REGEX = "(\\d+H) (\\d+S)" + @Composable fun ActivityChip( activity: Activity, @@ -68,7 +80,19 @@ fun ActivityChip( }, secondaryLabel = { if (startedAt != null) { - Text("Since ${recentTimestampToString(startedAt)}") + var startedDiff by remember { mutableStateOf(Duration.between(Instant.ofEpochMilli(startedAt), Instant.now())) } + + Text( + text = durationToLabel(startedDiff), + color = Color.White, + fontSize = 10.sp, + modifier = Modifier.padding(start = 22.dp), + ) + + LaunchedEffect(startedDiff) { + delay(Duration.ofSeconds(1L)) + startedDiff = Duration.between(Instant.ofEpochMilli(startedAt), Instant.now()) + } } else { null } @@ -108,6 +132,15 @@ fun ActivityChip( ) } +fun durationToLabel(duration: Duration): String { + return duration.toString() + .substring(2) // remove "PT" at the beginning of the string representation + .replace(ISO_HOURS_MINUTES_PARTS_REGEX.toRegex(), "$1 ") + .replace(DECIMAL_SEPARATOR_AND_FRACTIONAL_PART_REGEX.toRegex(), "") + .replace(ISO_MISSING_MINUTES_REGEX.toRegex(), "$1 0M $2") + .lowercase() +} + fun recentTimestampToString(epochMillis: Long): String { // Someday, it would be nice for this to show nicer time strings // e.g. "a few minutes ago", "yesterday", etc. -- 2.51.2