Modern Apps git

← Modern-Apps.git

library: update common history component

Commit
7ca4ab70f698cfbc27aa1e738961115ce1b93043
Author
Vayun Mathur <[email protected]>
When
2026-07-22 21:18 UTC (4 months ago)
Parents
cebc17a

Diff

findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.ktindex 47464dbe1f..bda23f87ec 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/ui/MainPage.kt@@ -36,6 +36,9 @@ import com.vayunmathur.library.ui.FilledTonalButton import com.vayunmathur.library.ui.FloatingActionButton import com.vayunmathur.library.ui.FloatingActionButtonMenu import com.vayunmathur.library.ui.FloatingActionButtonMenuItem+import com.vayunmathur.library.ui.HistoryScrubberCard+import com.vayunmathur.library.ui.HistoryStep+import com.vayunmathur.library.ui.rememberHistoryScrubberState import com.vayunmathur.library.ui.IconLink import com.vayunmathur.library.ui.IconLocationOn import com.vayunmathur.library.ui.IconPerson@@ -545,7 +548,7 @@ fun SectionHeader(title: String) {     ) } -@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class)+@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class, ExperimentalTime::class) @Composable fun BoxScope.HistoryScrubber(     backStack: NavBackStack<Route>,@@ -553,89 +556,41 @@ fun BoxScope.HistoryScrubber(     userid: Long,     setHistoricalPosition: (org.maplibre.spatialk.geojson.Position) -> Unit ) {-    val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())-    val currentDate = now.date-    val currentTime = now.time-    var pickedLocalDate by remember { mutableStateOf(currentDate) }-    val sliderState = rememberSliderState(-        currentTime.toSecondOfDay().toFloat(), valueRange = 0.0f..(24f * 60f * 60f - 0.1f)+    val state = rememberHistoryScrubberState(+        initialInstant = Clock.System.now(),+        initialNowMode = true,+        disallowFuture = true     ) -    sliderState.onValueChange = {-        val maximum = if (currentDate == pickedLocalDate) currentTime.toSecondOfDay().toFloat() else null-        if (maximum != null && it > maximum) sliderState.value = maximum-        else sliderState.value = it-    }-    val pickedLocalTime by remember {-        derivedStateOf {-            LocalTime.fromSecondOfDay(sliderState.value.toInt())-        }-    }+    val steps = listOf(+        HistoryStep(stringResource(R.string.history_step_minus_5m), -5 * 60L),+        HistoryStep(stringResource(R.string.history_step_minus_1m), -60L),+        HistoryStep(stringResource(R.string.history_step_minus_10s), -10L),+        HistoryStep(stringResource(R.string.history_step_plus_10s), 10L),+        HistoryStep(stringResource(R.string.history_step_plus_1m), 60L),+        HistoryStep(stringResource(R.string.history_step_plus_5m), 5 * 60L)+    ) -    Card(-        Modifier.align(Alignment.BottomCenter)-            .fillMaxWidth()-            .navigationBarsPadding()-            .padding(horizontal = 12.dp, vertical = 8.dp)-    ) {-        Column(-            Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 8.dp),-            verticalArrangement = Arrangement.spacedBy(6.dp)-        ) {-            Row(verticalAlignment = Alignment.CenterVertically) {-                Slider(sliderState, Modifier.weight(1f))-                Spacer(Modifier.width(8.dp))-                Text(-                    pickedLocalTime.format(DateFormats.TIME_SECOND_AM_PM),-                    style = MaterialTheme.typography.labelLarge-                )-            }-            AssistChip(-                { backStack.add(Route.UserPageHistoryDatePicker(pickedLocalDate)) },-                { Text(pickedLocalDate.format(DateFormats.DATE_INPUT)) }-            )-            Row(-                Modifier.fillMaxWidth(),-                horizontalArrangement = Arrangement.spacedBy(4.dp)-            ) {-                StepButton(stringResource(R.string.history_step_minus_5m)) { sliderState.value -= 5 * 60 }-                StepButton(stringResource(R.string.history_step_minus_1m)) { sliderState.value -= 60 }-                StepButton(stringResource(R.string.history_step_minus_10s)) { sliderState.value -= 10 }-                StepButton(stringResource(R.string.history_step_plus_10s)) { sliderState.value += 10 }-                StepButton(stringResource(R.string.history_step_plus_1m)) { sliderState.value += 60 }-                StepButton(stringResource(R.string.history_step_plus_5m)) { sliderState.value += 5 * 60 }-            }-        }-    }+    HistoryScrubberCard(+        state = state,+        steps = steps,+        onDateChipClick = { backStack.add(Route.UserPageHistoryDatePicker(state.date)) }+    )      ResultEffect<LocalDate>("HistoryDatePicker") {-        pickedLocalDate = it+        state.setDate(it)     } -    val simulatedTimestamp = pickedLocalDate.atTime(pickedLocalTime)-        .toInstant(TimeZone.currentSystemDefault())-     val locs by ffViewModel.locationHistory.collectAsState() -    LaunchedEffect(simulatedTimestamp, locs) {+    LaunchedEffect(state.instant, locs) {         if (locs.isNotEmpty()) {-            val closest = locs.minBy { (it.timestamp - simulatedTimestamp).absoluteValue }+            val closest = locs.minBy { (it.timestamp - state.instant).absoluteValue }             setHistoricalPosition(closest.coord.toPosition())         }     } } -@Composable-private fun RowScope.StepButton(label: String, onClick: () -> Unit) {-    FilledTonalButton(-        onClick,-        Modifier.weight(1f).heightIn(min = 36.dp),-        contentPadding = PaddingValues(horizontal = 4.dp, vertical = 0.dp)-    ) {-        Text(label, fontSize = 12.sp, maxLines = 1)-    }-}- @Composable fun AwaitingRequestCard(backStack: NavBackStack<Route>, id: Long) {     Card {
library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.ktnew file mode 100644index 0000000000..dc7e6f5470--- /dev/null+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.kt@@ -0,0 +1,221 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.BoxScope+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.PaddingValues+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.RowScope+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.heightIn+import androidx.compose.foundation.layout.navigationBarsPadding+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.width+import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.Stable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.sp+import kotlin.math.roundToInt+import kotlinx.coroutines.delay+import kotlinx.datetime.LocalDate+import kotlinx.datetime.LocalTime+import kotlinx.datetime.TimeZone+import kotlinx.datetime.atTime+import kotlinx.datetime.format+import kotlinx.datetime.format.MonthNames+import kotlinx.datetime.toInstant+import kotlinx.datetime.toLocalDateTime+import kotlin.time.Clock+import kotlin.time.Duration.Companion.seconds+import kotlin.time.ExperimentalTime+import kotlin.time.Instant++object HistoryDateFormats {+    val MONTH_DAY = LocalDate.Format {+        monthName(MonthNames.ENGLISH_ABBREVIATED)+        chars(" ")+        day()+    }++    val TIME_SECOND_AM_PM = LocalTime.Format {+        amPmHour()+        chars(":")+        minute()+        chars(":")+        second()+        chars(" ")+        amPmMarker("AM", "PM")+    }++    val DATE_INPUT = MONTH_DAY+}++/**+ * A single relative jump button for [HistoryScrubberCard]. [label] is shown on+ * the button; [deltaSeconds] is applied to the selected instant when tapped+ * (negative = into the past). Callers pick their own granularity — e.g. minutes+ * for FindFamily, hours/days for Astronomy.+ */+data class HistoryStep(val label: String, val deltaSeconds: Long)++/**+ * Holds the state for a [HistoryScrubberCard]. Owns the currently-selected+ * [instant] plus a [nowMode] flag. While in now mode the card ticks the instant+ * to the present every second; any user interaction (slider, step button, date+ * pick) leaves now mode and freezes time until [goNow] is called again.+ */+@OptIn(ExperimentalTime::class)+@Stable+class HistoryScrubberState internal constructor(+    val timeZone: TimeZone,+    initialInstant: Instant,+    initialNowMode: Boolean,+    private val disallowFuture: Boolean,+) {+    var nowMode by mutableStateOf(initialNowMode)+        private set+    var instant by mutableStateOf(initialInstant)+        private set++    val dateTime get() = instant.toLocalDateTime(timeZone)+    val date: LocalDate get() = dateTime.date+    val time: LocalTime get() = dateTime.time+    val secondOfDay: Int get() = time.toSecondOfDay()++    // Only user-driven changes respect the "no future" bound; live ticks are, by+    // definition, always the present. Recomputed each call so it never goes stale.+    private fun clampUser(candidate: Instant): Instant {+        if (disallowFuture) {+            val now = Clock.System.now()+            if (candidate > now) return now+        }+        return candidate+    }++    /** Slider drag: keep the date, change the time-of-day. Leaves now mode. */+    fun setSecondOfDay(sec: Int) {+        nowMode = false+        val clamped = sec.coerceIn(0, 86_399)+        instant = clampUser(date.atTime(LocalTime.fromSecondOfDay(clamped)).toInstant(timeZone))+    }++    /** Step button: shift the whole instant, possibly across a date boundary. Leaves now mode. */+    fun nudge(deltaSeconds: Long) {+        nowMode = false+        instant = clampUser(instant.plus(deltaSeconds.seconds))+    }++    /** Date-picker result: keep the time-of-day, change the date. Leaves now mode. */+    fun setDate(newDate: LocalDate) {+        nowMode = false+        instant = clampUser(newDate.atTime(time).toInstant(timeZone))+    }++    /** Now button: jump to the present and resume live tracking. */+    fun goNow() {+        instant = Clock.System.now()+        nowMode = true+    }++    internal fun tickNow() {+        if (nowMode) instant = Clock.System.now()+    }+}++@OptIn(ExperimentalTime::class)+@Composable+fun rememberHistoryScrubberState(+    initialInstant: Instant = Clock.System.now(),+    initialNowMode: Boolean = true,+    timeZone: TimeZone = TimeZone.currentSystemDefault(),+    disallowFuture: Boolean = false,+): HistoryScrubberState = remember {+    HistoryScrubberState(timeZone, initialInstant, initialNowMode, disallowFuture)+}++@Composable+fun RowScope.HistoryStepButton(label: String, onClick: () -> Unit) {+    FilledTonalButton(+        onClick,+        Modifier.weight(1f).heightIn(min = 36.dp),+        contentPadding = PaddingValues(horizontal = 4.dp, vertical = 0.dp)+    ) {+        Text(label, fontSize = 12.sp, maxLines = 1)+    }+}++@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class, ExperimentalTime::class)+@Composable+fun BoxScope.HistoryScrubberCard(+    state: HistoryScrubberState,+    steps: List<HistoryStep>,+    onDateChipClick: () -> Unit,+    modifier: Modifier = Modifier+) {+    // Advance to the present once per second while in now mode. Leaving now mode+    // (via any user interaction) flips the flag and cancels this loop.+    LaunchedEffect(state.nowMode) {+        while (state.nowMode) {+            state.tickNow()+            delay(1000)+        }+    }++    Card(+        modifier.align(Alignment.BottomCenter)+            .fillMaxWidth()+            .navigationBarsPadding()+            .padding(horizontal = 12.dp, vertical = 8.dp)+    ) {+        Column(+            Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 8.dp),+            verticalArrangement = Arrangement.spacedBy(6.dp)+        ) {+            Row(verticalAlignment = Alignment.CenterVertically) {+                Slider(+                    value = state.secondOfDay.toFloat(),+                    onValueChange = { state.setSecondOfDay(it.roundToInt()) },+                    valueRange = 0f..86_399f,+                    modifier = Modifier.weight(1f)+                )+                Spacer(Modifier.width(8.dp))+                Text(+                    state.time.format(HistoryDateFormats.TIME_SECOND_AM_PM),+                    style = MaterialTheme.typography.labelLarge+                )+            }+            Row(+                Modifier.fillMaxWidth(),+                verticalAlignment = Alignment.CenterVertically,+                horizontalArrangement = Arrangement.spacedBy(8.dp)+            ) {+                AssistChip(+                    { onDateChipClick() },+                    { Text(state.date.format(HistoryDateFormats.DATE_INPUT)) }+                )+                Spacer(Modifier.weight(1f))+                FilterChip(+                    selected = state.nowMode,+                    onClick = { state.goNow() },+                    label = { Text("Now") }+                )+            }+            Row(+                Modifier.fillMaxWidth(),+                horizontalArrangement = Arrangement.spacedBy(4.dp)+            ) {+                steps.forEach { step ->+                    HistoryStepButton(step.label) { state.nudge(step.deltaSeconds) }+                }+            }+        }+    }+}
library/ui/src/main/java/com/vayunmathur/library/ui/Icons.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/Icons.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/Icons.ktindex a220ab65ae..8c50bd529b 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/Icons.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/Icons.kt@@ -10,6 +10,7 @@ import androidx.compose.material.icons.filled.ForkRight import androidx.compose.material.icons.filled.Lightbulb import androidx.compose.material.icons.filled.Merge import androidx.compose.material.icons.filled.NightsStay+import androidx.compose.material.icons.filled.NoPhotography import androidx.compose.material.icons.filled.RampLeft import androidx.compose.material.icons.filled.RampRight import androidx.compose.material.icons.filled.RoundaboutLeft@@ -351,6 +352,10 @@ fun IconEraser(modifier: Modifier = Modifier, tint: Color = LocalContentColor.cu fun IconCamera(modifier: Modifier = Modifier, tint: Color = LocalContentColor.current) =     AppIcon(Icons.Filled.CameraAlt, "Camera", modifier, tint) +@Composable+fun IconCameraOff(modifier: Modifier = Modifier, tint: Color = LocalContentColor.current) =+    AppIcon(Icons.Filled.NoPhotography, "Camera off", modifier, tint)+ @Composable fun IconBackup(modifier: Modifier = Modifier, tint: Color = LocalContentColor.current) =     AppIcon(Icons.Filled.Backup, "Backup", modifier, tint)