Countdown
Wall-clock-driven countdown: the target wall clock is recorded once and remaining time = target − now (clamped to ≥ 0), never decremented per tick — staying accurate after background switches or timer throttling. Controlled display and uncontrolled self-running modes, format tokens DD/HH/mm/ss/S, and a single onFinish on zero.
Contract:
lh-ui-core/specs/countdown.json· Golden cases:cases/countdown.json(10 cases, translated to JVM unit tests + Compose UI Test)
Basic usage
The uncontrolled mode takes the total duration via time and self-runs after mount according to autoStart; the default format is HH:mm:ss. Digits render tabular (the countdown.digitMinWidth token + tnum) and separators are de-emphasized.
LhCountdown(time = 60_000L) // self-runs 60 seconds after mount
LhCountdown(time = 60_000L, format = "mm:ss")Format tokens
The format string is case-sensitive: only DD (days) / HH (hours) / mm (minutes) / ss (seconds) / S (milliseconds, zero-padded to three digits) are tokens; every other character renders literally. Without DD, HH carries the total hours (may exceed 24); with DD, HH only carries the 0-23 positional value. Negative / invalid values always render as zero.
LhCountdown(value = 3665000L) // 01:01:05
LhCountdown(value = 181810000L) // 50:30:10 (HH carries total hours)
LhCountdown(value = 86401500L, format = "DD天HH:mm:ss.S") // 01天00:00:01.500
LhCountdown(value = 89500L, format = "mm:ss.S", millisecond = true)Controlled and uncontrolled
Passing value makes the component controlled: it does not self-drive, displays the external value, and advancing plus the finish callback are the caller's responsibility. Passing time makes it uncontrolled: it self-runs after mount when autoStart and calls onChange(remainingMs) on every step. Both modes write back through onChange.
// Controlled: the caller owns the remaining value
var remaining by remember { mutableLongStateOf(10_000L) }
LhCountdown(value = remaining, onChange = { remaining = it })
// Uncontrolled: self-running, onFinish fires once at zero
LhCountdown(time = 5_000L, onFinish = { /* finished */ })Handle methods
LhCountdownHandle exposes instance methods as a ref: start() (resume from the paused point, no-op when disabled), pause() (freeze the remaining value), and reset(timeMs?) (return to the total duration and restart per autoStart). The handle has no effect in controlled mode — controlled display is driven by the caller.
val handle = remember { LhCountdownHandle() }
LhCountdown(time = 60_000L, handle = handle)
// From interactions: handle.start() / handle.pause() / handle.reset(30_000L)States
disabled = true greys the text (using color.text.disabled) and pauses the countdown: the remaining value freezes and resumes from the paused point when re-enabled. millisecond = true refreshes every frame (16ms step; recommended when the millisecond token is shown, otherwise 250ms).
LhCountdown(value = 65000L, disabled = true)
LhCountdown(time = 8950L, format = "mm:ss.S", millisecond = true)API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
modifier | Modifier | Modifier | Modifier |
value | Remaining ms (controlled: the component does not self-drive) | Long? | null |
time | Uncontrolled total duration ms (self-runs per autoStart) | Long | 0 |
format | Format string; tokens DD/HH/mm/ss/S | String | "HH:mm:ss" |
autoStart | Whether the uncontrolled mode starts after mount | Boolean | true |
millisecond | Frame-level refresh (16ms step, otherwise 250ms) | Boolean | false |
disabled | Disabled: greys and pauses the countdown | Boolean | false |
ariaLabel | Accessibility name (falls back to the formatted text) | String? | null |
handle | Method handle (start/pause/reset); no-op in controlled mode | LhCountdownHandle? | null |
onChange | Remaining-ms change (uncontrolled steps; controlled write-backs) | ((Long) -> Unit)? | null |
onFinish | Fires once when reaching zero | (() -> Unit)? | null |
content | Trailing content slot (rendered after the digits) | (@Composable () -> Unit)? | null |
LhCountdownHandle methods
| Method | Description | Parameter |
|---|---|---|
start() | Resume from the paused point (no-op when disabled) | none |
pause() | Pause and freeze the remaining ms | none |
reset(timeMs) | Return to the total duration (optional new value) and restart per autoStart | timeMs: Long? |
Token mapping
Tokens consumed by the countdown (aligned with tokenRefs of specs/countdown.json):
| Token | Usage |
|---|---|
countdown.digitMinWidth | Minimum digit width (tabular-figure stabilization) |
typography.fontSize.base | Font size of digits and separators |
color.text.primary | Digit text color (normal state) |
color.text.secondary | De-emphasized separator color |
color.text.disabled | Disabled text color (digits and separators) |
Golden case mapping
The 10 assertions of cases/countdown.json and their local tests:
| case id | Assertion | Local test |
|---|---|---|
renders-formatted-remaining | Controlled value renders 01:01:05 with the default format | LhCountdownTest.controlled_value_renders_formatted_text (Compose UI Test) |
negative-clamps-to-zero | Negative value clamps to all zeros | LhCountdownTest.negative_value_renders_zeros + LhCountdownDefaultsTest.negative_clamps_to_zero |
hours-over-24-without-dd | Without DD, HH carries total hours 50:30:10 | LhCountdownTest.hours_over_24_without_dd_renders_total_hours |
dd-format-splits-days | DD splits days and zero-pads milliseconds to 01天00:00:01.500 | LhCountdownTest.dd_format_splits_days_and_pads_millis |
uncontrolled-autostart-ticks | Uncontrolled autoStart self-runs and emits onChange | LhCountdownDefaultsTest.uncontrolled_autostart_ticks |
finish-emits-once-at-zero | Reaching zero emits onFinish once and stops the ticker | LhCountdownDefaultsTest.finish_emits_once_at_zero |
controlled-does-not-self-drive | Controlled mode does not self-drive | LhCountdownDefaultsTest.controlled_does_not_self_drive |
autostart-false-waits-for-start | autoStart=false waits for start() | LhCountdownDefaultsTest.autostart_false_waits_for_start |
disabled-pauses-and-greys | Disabled greys (text.disabled) and does not drive | LhCountdownDefaultsTest.disabled_pauses_and_greys |
aria-role-timer | Accessibility name falls back to the formatted text 00:01:05 | LhCountdownTest.aria_label_defaults_to_formatted_text |
./gradlew :components:countdown:test # JVM part
./gradlew :components:countdown:connectedAndroidTest # Rendering part (device required)
