Merge branch 'feat/23-world-clock-seconds' into release/v1.0.0

# Conflicts:
#	app/src/main/java/de/jeanlucmakiola/clockula/backup/BackupEngine.kt
#	app/src/main/java/de/jeanlucmakiola/clockula/data/backup/BackupCodec.kt
#	app/src/main/java/de/jeanlucmakiola/clockula/data/backup/BackupDto.kt
#	app/src/main/java/de/jeanlucmakiola/clockula/data/prefs/SettingsPrefs.kt
#	app/src/main/java/de/jeanlucmakiola/clockula/domain/backup/BackupContent.kt
#	app/src/test/java/de/jeanlucmakiola/clockula/data/backup/BackupCodecTest.kt
#	app/src/test/java/de/jeanlucmakiola/clockula/ui/settings/SettingsViewModelTest.kt
#	docs/ARCHITECTURE.md
#	docs/BACKUP.md
This commit is contained in:
makiolaj committed 2026-10-26 20:54:31 +01:00
commit fd7f5e8dcd
40 files changed
+860 -110

No files matched your search

@@ -115,6 +115,7 @@ class BackupEngine @Inject constructor(
dynamicColor = settings.dynamicColor.first(),
defaults = settings.currentDefaults(),
timerPresets = settings.timerPresets.first(),
worldClockSeconds = settings.worldClockSeconds.first(),
bedtime = settings.currentBedtime(),
)
}
@@ -143,6 +143,7 @@ object BackupCodec {
alarmSilenceAfterMinutes = defaults.alarmSilenceAfterMinutes,
timerSilenceAfterMinutes = defaults.timerSilenceAfterMinutes,
volumeButtons = defaults.volumeButtons.name,
worldClockSeconds = worldClockSeconds,
timerPresets = timerPresets.map { it.inWholeMilliseconds },
bedtime = bedtime?.toDto(),
)
@@ -301,6 +302,7 @@ object BackupCodec {
volumeButtons = settingsDto.volumeButtons.toEnum(VolumeButtonAction.CONTROL_VOLUME),
),
timerPresets = settingsDto.timerPresets?.map { it.milliseconds } ?: TimerPresets.BUILT_IN,
worldClockSeconds = settingsDto.worldClockSeconds,
bedtime = settingsDto.bedtime?.toBedtime(),
)
@@ -93,6 +93,8 @@ data class SettingsDto(
*/
@SerialName("default_alarm_silence_after_minutes") val alarmSilenceAfterMinutes: Int = SilenceAfter.DEFAULT_MINUTES,
@SerialName("default_timer_silence_after_minutes") val timerSilenceAfterMinutes: Int = SilenceAfter.DEFAULT_MINUTES,
/** Absent in files from before it existed, which keeps the default: off. */
@SerialName("world_clock_show_seconds") val worldClockSeconds: Boolean = false,
/**
* Absent is not empty: an empty list is a user who removed every preset,
* while a file without the field (an older or hand-written one) keeps the
@@ -5,11 +5,13 @@ import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import de.jeanlucmakiola.clockula.data.time.AndroidBootIdProvider
import de.jeanlucmakiola.clockula.data.time.AndroidClockChanges
import de.jeanlucmakiola.clockula.data.time.RealTicker
import de.jeanlucmakiola.clockula.data.time.SystemElapsedRealtimeClock
import de.jeanlucmakiola.clockula.data.time.SystemWallClock
import de.jeanlucmakiola.clockula.data.time.SystemZoneProvider
import de.jeanlucmakiola.clockula.domain.time.BootIdProvider
import de.jeanlucmakiola.clockula.domain.time.ClockChanges
import de.jeanlucmakiola.clockula.domain.time.ElapsedRealtimeClock
import de.jeanlucmakiola.clockula.domain.time.Ticker
import de.jeanlucmakiola.clockula.domain.time.WallClock
@@ -39,4 +41,7 @@ abstract class TimeModule {
@Binds
abstract fun bindTicker(impl: RealTicker): Ticker
@Binds
abstract fun bindClockChanges(impl: AndroidClockChanges): ClockChanges
}
@@ -66,6 +66,14 @@ object ClockPrefs {
val homeZoneId: Pref<String?> = nullableStringPref("home_zone_id")
.map(decode = Zones::normalise, encode = Zones::normalise)
/**
* The world clock tab's readouts to the second, with a second hand on the
* face (#23). Off by default: it is what makes the tab wake every second
* rather than every minute. Not part of `ClockDefaults`, which alarms and
* timers inherit from, so flipping it re-emits nothing they watch.
*/
val worldClockSeconds: Pref<Boolean> = booleanPref("world_clock_show_seconds", false)
private fun Pref<Int>.clampedTo(range: IntRange): Pref<Int> =
map(decode = { it.coerceIn(range) }, encode = { it.coerceIn(range) })
@@ -118,6 +118,7 @@ class SettingsPrefs @Inject constructor(private val store: PrefStore) {
prefs[ClockPrefs.alarmSilenceAfterMinutes] = backup.defaults.alarmSilenceAfterMinutes
prefs[ClockPrefs.timerSilenceAfterMinutes] = backup.defaults.timerSilenceAfterMinutes
prefs[ClockPrefs.volumeButtons] = backup.defaults.volumeButtons
prefs[ClockPrefs.worldClockSeconds] = backup.worldClockSeconds
prefs[TimerPrefs.presets] = backup.timerPresets
// Absent from a file written before bedtime existed: keep it.
backup.bedtime?.let { prefs.putBedtime(it) }
@@ -168,6 +169,13 @@ class SettingsPrefs @Inject constructor(private val store: PrefStore) {
suspend fun setHomeZoneId(zoneId: String?) = store.set(ClockPrefs.homeZoneId, zoneId)
// --- world clock display (#23) ---
/** Its own flow, distinct-until-changed, so the clock defaults' watchers never see it move. */
val worldClockSeconds: Flow<Boolean> = store.flow(ClockPrefs.worldClockSeconds)
suspend fun setWorldClockSeconds(enabled: Boolean) = store.set(ClockPrefs.worldClockSeconds, enabled)
// --- timer presets (M6) ---
/**
@@ -0,0 +1,42 @@
package de.jeanlucmakiola.clockula.data.time
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.core.content.ContextCompat
import dagger.hilt.android.qualifiers.ApplicationContext
import de.jeanlucmakiola.clockula.domain.time.ClockChanges
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import javax.inject.Inject
import javax.inject.Singleton
/**
* `TIME_SET` and `TIMEZONE_CHANGED`, through a receiver that is registered
* only while somebody collects. Both already have their manifest receiver
* (`SystemEventReceiver`) for the alarm engine; this one is for a readout on
* screen, which must not wait out its cadence to show either.
*/
@Singleton
class AndroidClockChanges @Inject constructor(
@param:ApplicationContext private val context: Context,
) : ClockChanges {
override fun changes(): Flow<Unit> = callbackFlow {
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
trySend(Unit)
}
}
val filter = IntentFilter().apply {
addAction(Intent.ACTION_TIME_CHANGED)
addAction(Intent.ACTION_TIMEZONE_CHANGED)
}
// Protected system broadcasts still arrive at a non-exported receiver,
// and nothing else on the device gets to poke the tab.
ContextCompat.registerReceiver(context, receiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED)
awaitClose { context.unregisterReceiver(receiver) }
}
}
@@ -1,6 +1,8 @@
package de.jeanlucmakiola.clockula.data.time
import de.jeanlucmakiola.clockula.domain.time.Ticker
import de.jeanlucmakiola.clockula.domain.time.WallBoundaries
import de.jeanlucmakiola.clockula.domain.time.WallClock
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
@@ -10,7 +12,7 @@ import javax.inject.Singleton
/** The [Ticker] seam over `delay`: emit, then wait [ticks]' period, forever. */
@Singleton
class RealTicker @Inject constructor() : Ticker {
class RealTicker @Inject constructor(private val wallClock: WallClock) : Ticker {
/**
* Cold, and per-collector: two readouts on different cadences get two
@@ -24,4 +26,18 @@ class RealTicker @Inject constructor() : Ticker {
delay(period)
}
}
/**
* Cold and per-collector, as [ticks] is. The wait is re-measured against
* the wall clock after every emission rather than added up, so a `delay`
* that ran a little long does not accumulate. A wall clock set mid-wait is
* only caught up after that wait ends; a caller that cannot wait that long
* restarts the flow on `ClockChanges`, as the world clock does.
*/
override fun alignedTicks(unit: Duration): Flow<Unit> = flow {
while (true) {
emit(Unit)
delay(WallBoundaries.untilNext(wallClock.now(), unit))
}
}
}
@@ -98,8 +98,8 @@ class ZoneDirectory @Inject constructor(
/**
* One display name per id in [zoneIds], on the same memo. Batched for the
* same reason [entriesFor] is: the tab asks five times a second, and a
* dispatch and a lock per row would be a hundred and twenty-five of each.
* same reason [entriesFor] is: the tab asks on every tick (once a second with
* seconds on), and a dispatch and a lock per row would be twenty-five of each.
*/
suspend fun displayNamesOf(zoneIds: List<String>, at: Instant): Map<String, String?> =
withContext(io) {
@@ -17,8 +17,8 @@ object Zones {
/**
* Snapshotted once. `ZoneId.getAvailableZoneIds()` builds a fresh ~600-entry
* `HashSet` on every call, and the world clock asks per row per 200 ms tick
* — twenty-four cities would be a hundred and twenty-five throwaway sets a
* `HashSet` on every call, and the world clock asks per row per tick —
* with seconds on, twenty-four cities would be twenty-five throwaway sets a
* second. The device's tzdata arrives as an APEX module that takes effect
* at reboot, so the answer cannot change under a running process.
*/
@@ -61,6 +61,8 @@ data class BackupSettings(
val dynamicColor: Boolean,
val defaults: ClockDefaults,
val timerPresets: List<Duration>,
/** The world clock's "Display time with seconds" (#23). */
val worldClockSeconds: Boolean,
/** Null for a file written before bedtime existed: restoring it keeps the current schedule. */
val bedtime: BedtimeSettings? = null,
)
@@ -40,4 +40,22 @@ interface ZoneProvider {
*/
interface Ticker {
fun ticks(period: Duration): Flow<Unit>
/**
* Emits once immediately, then just after every wall-clock instant that is
* a whole multiple of [unit] ([WallBoundaries.untilNext]). For a readout of
* whole seconds or minutes: it wakes exactly when the readout changes and
* not otherwise, so it is both on time and as rare as it can be.
*/
fun alignedTicks(unit: Duration): Flow<Unit>
}
/**
* The system saying the wall clock may not read what a readout last drew: the
* time was set or the zone changed. A readout on a long cadence restarts its
* tick on this, so it does not wait out its own period to catch up. Emits
* only while collected.
*/
interface ClockChanges {
fun changes(): Flow<Unit>
}
@@ -0,0 +1,30 @@
package de.jeanlucmakiola.clockula.domain.time
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Instant
/**
* Where the next whole second (or minute) of the wall clock falls. A readout
* that shows whole units changes exactly at these instants, so waking there
* rather than on a free-running cadence is what keeps it on time without
* waking more often than it changes.
*/
object WallBoundaries {
/**
* How long from [now] until the next instant that is a whole multiple of
* [unit] since the epoch. Always in `(0, unit]`: standing exactly on a
* boundary waits for the *next* one, so a loop over this never spins.
*
* Every zone in use today is offset from UTC by whole minutes, so a
* boundary of the epoch is a boundary of every zone's local second and
* minute alike.
*/
fun untilNext(now: Instant, unit: Duration): Duration {
val unitMillis = unit.inWholeMilliseconds
require(unitMillis > 0) { "unit must be at least a millisecond: $unit" }
val into = Math.floorMod(now.toEpochMilliseconds(), unitMillis)
return (unitMillis - into).milliseconds
}
}
@@ -16,20 +16,24 @@ import java.time.format.FormatStyle
* alarm list, the editor and the world clock all read a time through it, so a
* second `DateFormat.is24HourFormat` call site would be the start of two
* answers (M5, M8 D17).
*
* [withSeconds] is the world clock's "show seconds" (#23): the same two-step,
* one unit finer — `HH:mm:ss`, or the locale's medium form, which is its short
* form with the seconds in the place that locale puts them.
*/
@Composable
internal fun rememberLocalTimeFormatter(): (LocalTime) -> String {
internal fun rememberLocalTimeFormatter(withSeconds: Boolean = false): (LocalTime) -> String {
val context = LocalContext.current
// The locale can change under a running activity, and the 12/24-hour
// choice rides the same configuration, so both are re-read with it.
val configuration = LocalConfiguration.current
val formatter = remember(configuration) {
val formatter = remember(configuration, withSeconds) {
// The device's 12/24-hour choice, then the locale's own short form —
// the same two-step the ring notification already formats with.
if (DateFormat.is24HourFormat(context)) {
DateTimeFormatter.ofPattern("HH:mm")
DateTimeFormatter.ofPattern(if (withSeconds) "HH:mm:ss" else "HH:mm")
} else {
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
DateTimeFormatter.ofLocalizedTime(if (withSeconds) FormatStyle.MEDIUM else FormatStyle.SHORT)
}
}
return remember(formatter) {
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.clockula.ui.settings
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Switch
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
@@ -11,7 +12,10 @@ import de.jeanlucmakiola.floret.components.CollapsingScaffold
import de.jeanlucmakiola.floret.components.GroupedRow
import de.jeanlucmakiola.floret.components.Position
/** The clock tab's settings. The zone picker itself is ViewModel state, drawn by the hub. */
/**
* The clock tab's settings: the home zone, and whether the tab shows seconds.
* The zone picker itself is ViewModel state, drawn by the hub.
*/
@Composable
internal fun ClockSettingsScreen(
state: SettingsUiState.Loaded,
@@ -25,8 +29,20 @@ internal fun ClockSettingsScreen(
GroupedRow(
title = stringResource(R.string.settings_home_zone),
summary = state.defaults.homeZoneId ?: stringResource(R.string.zone_picker_device_zone),
position = Position.Alone,
position = Position.Top,
onClick = viewModel::onOpenHomeZonePicker,
)
GroupedRow(
title = stringResource(R.string.settings_world_clock_seconds),
summary = stringResource(R.string.settings_world_clock_seconds_hint),
position = Position.Bottom,
trailing = {
Switch(
checked = state.worldClockSeconds,
onCheckedChange = viewModel::onWorldClockSecondsChanged,
)
},
onClick = { viewModel.onWorldClockSecondsChanged(!state.worldClockSeconds) },
)
}
}
@@ -14,6 +14,8 @@ internal sealed interface SettingsUiState {
val appearance: Appearance,
val defaults: ClockDefaults,
val selfCheckProblemCount: Int? = null,
/** The world clock's "Display time with seconds" (#23); not a clock default. */
val worldClockSeconds: Boolean,
) : SettingsUiState
}
@@ -72,8 +72,9 @@ class SettingsViewModel @Inject internal constructor(
settings.appearance,
settings.defaults,
selfCheckProblemCount,
) { appearance, defaults, problemCount ->
SettingsUiState.Loaded(appearance, defaults, problemCount)
settings.worldClockSeconds,
) { appearance, defaults, problemCount, worldClockSeconds ->
SettingsUiState.Loaded(appearance, defaults, problemCount, worldClockSeconds)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(SUBSCRIPTION_GRACE_MS), SettingsUiState.Loading)
/**
@@ -249,6 +250,9 @@ class SettingsViewModel @Inject internal constructor(
DefaultRingtoneKind.TIMER -> timerRingtoneUri
}
fun onWorldClockSecondsChanged(enabled: Boolean) =
viewModelScope.launch { settings.setWorldClockSeconds(enabled) }
// --- the home time-zone picker ---
fun onOpenHomeZonePicker() {
@@ -2,12 +2,23 @@ package de.jeanlucmakiola.clockula.ui.worldclock
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import java.time.temporal.ChronoUnit
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.toKotlinDuration
internal object WorldClockDefaults {
/** 200 ms — the second hand is never a fifth of a second behind (D18). */
val Tick: Duration = 200.milliseconds
/**
* The unit every time on the tab is truncated to: the minute, or the second
* only when the user asked for them (D18, #23).
*/
fun unitFor(showSeconds: Boolean): ChronoUnit = if (showSeconds) ChronoUnit.SECONDS else ChronoUnit.MINUTES
/**
* The tab's cadence, aligned to the wall clock through
* `Ticker.alignedTicks` so the readouts change on the boundary itself.
* Derived from [unitFor], so the readouts and the cadence cannot drift apart.
*/
fun tickFor(showSeconds: Boolean): Duration = unitFor(showSeconds).duration.toKotlinDuration()
val HeroFaceSize: Dp = 200.dp
@@ -29,7 +29,9 @@ import java.time.LocalTime
/**
* The app's one deliberate showpiece: a `MaterialShapes.Circle`↔`Sunny` dial
* whose morph progress is [dayFraction], twelve hour marks and three hands.
* whose morph progress is [dayFraction], twelve hour marks and two hands, or
* three with [showSecondHand] — the world clock's "show seconds" (#23). Without
* it the tab ticks once a minute, and a second hand there would stand still.
*
* The morph carries **information**, which is the whole reason it is here
* (`docs/PLAN.md` §8, D5): a plain circle in the middle of the night there, a
@@ -48,6 +50,7 @@ import java.time.LocalTime
internal fun WorldClockFace(
time: LocalTime,
dayFraction: Float,
showSecondHand: Boolean,
modifier: Modifier = Modifier,
) {
val dialColor = MaterialTheme.colorScheme.secondaryContainer
@@ -62,9 +65,9 @@ internal fun WorldClockFace(
val dialPath = remember { Path() }
val dialMatrix = remember { Matrix() }
// Reduce motion touches the morph, not the hands: the second hand is a
// discrete position change carrying information, and a clock whose second
// hand stops is a broken clock (D19).
// Reduce motion touches the morph, not the hands: the second hand, when
// shown, is a discrete position change carrying information, and a clock
// whose second hand stops is a broken clock (D19).
val progress by animateFloatAsState(
targetValue = dayFraction,
animationSpec = if (reduceMotion) {
@@ -82,7 +85,7 @@ internal fun WorldClockFace(
) {
drawDial(morph, progress, dialPath, dialMatrix, dialColor)
drawTicks(markColor)
drawHands(time, markColor, accentColor)
drawHands(time, showSecondHand, markColor, accentColor)
}
}
@@ -127,11 +130,13 @@ private fun DrawScope.drawTicks(color: Color) {
}
}
private fun DrawScope.drawHands(time: LocalTime, handColor: Color, accentColor: Color) {
private fun DrawScope.drawHands(time: LocalTime, showSecondHand: Boolean, handColor: Color, accentColor: Color) {
val radius = size.minDimension / 2f
hand(AnalogFace.hourAngle(time), radius * 0.46f, 5.dp.toPx(), handColor)
hand(AnalogFace.minuteAngle(time), radius * 0.68f, 4.dp.toPx(), handColor)
hand(AnalogFace.secondAngle(time), radius * 0.74f, 2.dp.toPx(), accentColor)
if (showSecondHand) {
hand(AnalogFace.secondAngle(time), radius * 0.74f, 2.dp.toPx(), accentColor)
}
drawCircle(accentColor, radius = 4.dp.toPx(), center = center)
drawCircle(handColor, radius = 2.dp.toPx(), center = center, style = Stroke(width = 1.dp.toPx()))
}
@@ -7,22 +7,31 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.DragHandle
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.CustomAccessibilityAction
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.customActions
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import de.jeanlucmakiola.clockula.R
import de.jeanlucmakiola.clockula.domain.worldclock.DayLabel
@@ -52,6 +61,8 @@ internal fun WorldClockRow(
row: WorldClockRowState,
position: Position,
formatTime: (LocalTime) -> String,
formatSpokenTime: (LocalTime) -> String,
timeStyle: RowTimeStyle,
dragHandle: Modifier,
isDragging: Boolean,
onMoveUp: () -> Unit,
@@ -63,26 +74,36 @@ internal fun WorldClockRow(
val time = row.time?.let(formatTime)
// One sentence for TalkBack, in the order the row is read aloud in. A row
// whose zone is gone has no time, and its summary already *is* the
// unknown-zone sentence, so naming it twice would be a stutter.
val description = if (time != null) {
stringResource(R.string.world_clock_row_description, row.name, time, summary)
// unknown-zone sentence, so naming it twice would be a stutter. Spoken to
// the minute even with seconds on: a description that changed every
// second would be re-announced every second while the row had focus.
val spokenTime = row.time?.let(formatSpokenTime)
val description = if (spokenTime != null) {
stringResource(R.string.world_clock_row_description, row.name, spokenTime, summary)
} else {
stringResource(R.string.world_clock_row_description_unknown, row.name, summary)
}
val actions = listOf(
CustomAccessibilityAction(stringResource(R.string.world_clock_row_move_up)) {
onMoveUp()
true
},
CustomAccessibilityAction(stringResource(R.string.world_clock_row_move_down)) {
onMoveDown()
true
},
CustomAccessibilityAction(stringResource(R.string.world_clock_row_remove)) {
onRemove()
true
},
)
val moveUpLabel = stringResource(R.string.world_clock_row_move_up)
val moveDownLabel = stringResource(R.string.world_clock_row_move_down)
val removeLabel = stringResource(R.string.world_clock_row_remove)
// Remembered, so a row that ticks every second keeps equal actions and its
// semantics only change when what TalkBack would say changes.
val actions = remember(moveUpLabel, moveDownLabel, removeLabel, onMoveUp, onMoveDown, onRemove) {
listOf(
CustomAccessibilityAction(moveUpLabel) {
onMoveUp()
true
},
CustomAccessibilityAction(moveDownLabel) {
onMoveDown()
true
},
CustomAccessibilityAction(removeLabel) {
onRemove()
true
},
)
}
GroupedSurface(
position = position,
@@ -134,8 +155,17 @@ internal fun WorldClockRow(
text = time,
// Tabular, because M7 made the headline role tabular
// app-wide: a column of times must not jitter as it ticks.
style = MaterialTheme.typography.headlineMedium,
fontWeight = FontWeight.Bold,
style = timeStyle.style,
// One line only when capped (seconds on); without seconds
// the time wraps at a huge font scale, as it always did.
maxLines = if (timeStyle.capped) 1 else Int.MAX_VALUE,
softWrap = !timeStyle.capped,
overflow = if (timeStyle.capped) TextOverflow.Ellipsis else TextOverflow.Clip,
// The row's description already says the time, to the
// minute; the ticking text itself stays out of the tree.
modifier = Modifier
.widthIn(max = timeStyle.maxWidth)
.clearAndSetSemantics {},
color = if (isDragging) {
MaterialTheme.colorScheme.primary
} else {
@@ -147,6 +177,63 @@ internal fun WorldClockRow(
}
}
/** How a row draws its time; the same for every row, so the screen builds it once. */
@Immutable
internal data class RowTimeStyle(val style: TextStyle, val maxWidth: Dp) {
val capped: Boolean get() = maxWidth != Dp.Infinity
}
private const val FitMargin = 0.95f
/**
* The least the time may take: wide enough for "12:59:59 PM" at the default
* scale. On a wider window the cap grows with it, to [SecondsMaxShare] of the
* window, so a larger font scale is honoured wherever there is room for it.
*/
private val SecondsMinCap = 168.dp
private const val SecondsMaxShare = 0.45f
/**
* Wide times in every format: a two-digit hour, eights, and each day period,
* since a locale's AM marker can be wider than its PM one.
*/
private val WidestSamples: List<LocalTime> = listOf(LocalTime.of(10, 58, 58), LocalTime.of(22, 58, 58))
/**
* Without seconds, the row's time exactly as it always was: `headlineMedium`,
* bold, uncapped. With seconds, a size down, and shrunk further only if the
* widest sample would not fit the cap, so at a large font scale the
* time cannot take the whole row and leave the city no width at all.
* Measured once per format, style and density, never per tick: the digits are
* tabular, so no time is wider than the samples.
*/
@Composable
internal fun rememberRowTimeStyle(showSeconds: Boolean, formatTime: (LocalTime) -> String): RowTimeStyle {
val base = if (showSeconds) MaterialTheme.typography.headlineSmall else MaterialTheme.typography.headlineMedium
val measurer = rememberTextMeasurer(cacheSize = 0)
val density = LocalDensity.current
val windowWidth = LocalConfiguration.current.screenWidthDp.dp
val maxWidth = maxOf(SecondsMinCap, windowWidth * SecondsMaxShare)
return remember(showSeconds, base, formatTime, density, measurer, maxWidth) {
val style = base.copy(fontWeight = FontWeight.Bold)
if (!showSeconds) return@remember RowTimeStyle(style, Dp.Infinity)
val width = WidestSamples.maxOf { sample ->
measurer.measure(formatTime(sample), style, maxLines = 1, softWrap = false).size.width
}
val cap = with(density) { maxWidth.toPx() }
// Through px and back, not by scaling the sp value: from Android 14
// large font scales are non-linear, so sp and px are not proportional.
val fitted = if (width <= cap) {
style
} else {
// A few percent spare: hinted glyph advances do not scale exactly.
style.copy(fontSize = with(density) { (style.fontSize.toPx() * FitMargin * cap / width).toSp() })
}
RowTimeStyle(fitted, maxWidth)
}
}
/**
* The offset, and the day only when it differs: "Same time as home" stands
* alone, and "Today" after it would be a sentence saying nothing twice.
@@ -11,7 +11,6 @@ import de.jeanlucmakiola.clockula.domain.worldclock.ZoneEntry
import de.jeanlucmakiola.clockula.domain.worldclock.ZoneOffsetFormat
import java.time.LocalTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import kotlin.time.Instant
import kotlin.time.toJavaInstant
@@ -20,7 +19,7 @@ internal data class WorldClockRowState(
val zoneId: String,
/** The user's label when set, else the ICU city, else the id's last segment. Never blank. */
val name: String,
/** Truncated to the minute. Non-null **exactly** when [known]. */
/** Truncated to the minute, or the second with seconds on. Non-null **exactly** when [known]. */
val time: LocalTime?,
/** Non-null exactly when [known]. */
val offset: OffsetLabel?,
@@ -36,7 +35,7 @@ internal data class HomeFaceState(
val zoneId: String,
val city: String,
val zoneName: String?,
/** Truncated to the second. */
/** Truncated to the minute, or the second with seconds on. */
val time: LocalTime,
/** 0f night … 1f day — what the dial's shape morph carries. */
val dayFraction: Float,
@@ -51,6 +50,8 @@ internal sealed interface WorldClockUiState {
/** In the repository's order — `sort_order`, then `id`. Never re-sorted here. */
val rows: List<WorldClockRowState>,
val canAdd: Boolean,
/** The user's "show seconds": the readouts' format and the face's second hand. */
val showSeconds: Boolean,
) : WorldClockUiState
}
@@ -62,8 +63,9 @@ internal object WorldClockRows {
* knows keeps its row with all three readings blank rather than being
* rewritten into something the app invented.
*
* The time is truncated to the **minute**, which is what makes the 200 ms
* tick cheap: four ticks in five change nothing a row can show.
* The time is truncated to the unit the tab ticks in, the **minute** unless
* [showSeconds]: a state carrying more precision than is shown would
* differ on every wake and change nothing anybody could see.
*/
fun from(
clocks: List<WorldClock>,
@@ -71,6 +73,7 @@ internal object WorldClockRows {
now: Instant,
entries: Map<String, ZoneEntry>,
zoneNames: Map<String, String?>,
showSeconds: Boolean,
): List<WorldClockRowState> = clocks.map { clock ->
val known = clock.isKnownZone
val comparison = if (known) {
@@ -82,7 +85,7 @@ internal object WorldClockRows {
id = clock.id,
zoneId = clock.zoneId,
name = clock.label ?: cityOf(clock.zoneId, entries),
time = comparison?.localDateTime?.toLocalTime()?.truncatedTo(ChronoUnit.MINUTES),
time = comparison?.localDateTime?.toLocalTime()?.truncatedTo(WorldClockDefaults.unitFor(showSeconds)),
offset = comparison?.let { ZoneOffsetFormat.labelFor(it.offsetMinutes) },
day = comparison?.let { ZoneOffsetFormat.dayLabelFor(it.dayDifference) },
zoneName = zoneNames[clock.zoneId],
@@ -96,11 +99,11 @@ internal object WorldClockRows {
now: Instant,
entries: Map<String, ZoneEntry>,
zoneNames: Map<String, String?>,
showSeconds: Boolean,
): HomeFaceState {
// To the second, not the millisecond: the second hand steps, so a state
// that carried more precision would differ on every tick and change
// nothing anybody could see.
val local = now.toJavaInstant().atZone(homeZone).toLocalTime().truncatedTo(ChronoUnit.SECONDS)
// To the unit shown, as the rows are. Without seconds the face has no
// second hand, so its hands and its readout move once a minute.
val local = now.toJavaInstant().atZone(homeZone).toLocalTime().truncatedTo(WorldClockDefaults.unitFor(showSeconds))
return HomeFaceState(
zoneId = homeZone.id,
city = cityOf(homeZone.id, entries),
@@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.TextAutoSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.Home
@@ -20,16 +21,21 @@ import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.text
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LifecycleResumeEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import de.jeanlucmakiola.clockula.R
import de.jeanlucmakiola.clockula.domain.worldclock.WorldClocks
@@ -65,10 +71,19 @@ fun WorldClockScreen(
viewModel: WorldClockViewModel = hiltViewModel(),
) {
val state by viewModel.state.collectAsStateWithLifecycle()
// A language change recreates the activity and nothing else tells the
// tab: without this, city names would wait for the next minute's tick.
LifecycleResumeEffect(viewModel) {
viewModel.onResumed()
onPauseOrDispose {}
}
val picker by viewModel.picker.collectAsStateWithLifecycle()
val pillInset = LocalLivePillInset.current
val formatTime = rememberLocalTimeFormatter()
val loaded = state as? WorldClockUiState.Loaded
val formatTime = rememberLocalTimeFormatter(withSeconds = loaded?.showSeconds == true)
// What TalkBack is given, always to the minute: see WorldClockRow.
val formatSpokenTime = rememberLocalTimeFormatter()
val rowTimeStyle = rememberRowTimeStyle(showSeconds = loaded?.showSeconds == true, formatTime = formatTime)
// Saveable, not remembered: a rotation mid-confirmation must not lose the
// question it was asking (D13).
@@ -112,27 +127,43 @@ fun WorldClockScreen(
},
) {
if (loaded != null) {
HomeFace(home = loaded.home, formatTime = formatTime)
HomeFace(
home = loaded.home,
showSeconds = loaded.showSeconds,
formatTime = formatTime,
formatSpokenTime = formatSpokenTime,
)
if (loaded.rows.isEmpty()) {
EmptyWorldClocks()
} else {
// The column is handed ids, not rows: it re-seeds its working
// order whenever its items change, and rows that tick (every
// second with seconds on) would snap a drag back mid-gesture.
val rowsById = loaded.rows.associateBy { it.id }
ReorderableColumn(
items = loaded.rows,
keyOf = { it.id },
onReorder = { rows -> viewModel.onReorder(rows.map { it.id }) },
items = loaded.rows.map { it.id },
keyOf = { it },
onReorder = viewModel::onReorder,
modifier = Modifier.fillMaxWidth(),
rowHeight = 80.dp,
) { row, position, dragHandle, isDragging ->
) { id, position, dragHandle, isDragging ->
// Always present while the column re-seeds in composition;
// drawn as nothing rather than crashing if that ever lags.
val row = rowsById[id] ?: return@ReorderableColumn
WorldClockRow(
row = row,
position = position,
formatTime = formatTime,
formatSpokenTime = formatSpokenTime,
timeStyle = rowTimeStyle,
dragHandle = dragHandle,
isDragging = isDragging,
onMoveUp = { viewModel.onMoveUp(row.id) },
onMoveDown = { viewModel.onMoveDown(row.id) },
onRemove = { pendingRemoval = row.id },
// Remembered per id: a row that ticks every second must
// not hand TalkBack new custom actions every second.
onMoveUp = remember(id) { { viewModel.onMoveUp(id) } },
onMoveDown = remember(id) { { viewModel.onMoveDown(id) } },
onRemove = remember(id) { { pendingRemoval = id } },
)
}
}
@@ -184,7 +215,12 @@ fun WorldClockScreen(
}
@Composable
private fun HomeFace(home: HomeFaceState, formatTime: (LocalTime) -> String) {
private fun HomeFace(
home: HomeFaceState,
showSeconds: Boolean,
formatTime: (LocalTime) -> String,
formatSpokenTime: (LocalTime) -> String,
) {
Column(
modifier = Modifier
.fillMaxWidth()
@@ -195,6 +231,7 @@ private fun HomeFace(home: HomeFaceState, formatTime: (LocalTime) -> String) {
WorldClockFace(
time = home.time,
dayFraction = home.dayFraction,
showSecondHand = showSeconds,
modifier = Modifier.padding(bottom = 12.dp),
)
// One TalkBack stop for the time and whose time it is ("14:05, Home,
@@ -206,12 +243,27 @@ private fun HomeFace(home: HomeFaceState, formatTime: (LocalTime) -> String) {
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
// Spoken to the minute: with seconds on, a merged node whose text
// changed every second would be re-announced every second. Set as
// the text, not a description, so the merge still reads "Home",
// the city and the zone name after it.
val spokenTime = formatSpokenTime(home.time)
Text(
text = formatTime(home.time),
style = MaterialTheme.typography.displaySmall,
color = MaterialTheme.colorScheme.onSurface,
textAlign = TextAlign.Center,
modifier = Modifier.padding(bottom = 8.dp),
// With seconds, one line that shrinks to fit rather than a
// readout that wraps, and moves the list, as the hour widens.
maxLines = if (showSeconds) 1 else Int.MAX_VALUE,
autoSize = if (showSeconds) {
TextAutoSize.StepBased(maxFontSize = MaterialTheme.typography.displaySmall.fontSize)
} else {
null
},
modifier = Modifier
.padding(bottom = 8.dp)
.clearAndSetSemantics { text = AnnotatedString(spokenTime) },
)
// The face has no other label saying whose time it is, and a city
// here is otherwise indistinguishable from a city in the list below.
@@ -4,6 +4,7 @@ import de.jeanlucmakiola.clockula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.clockula.data.worldclocks.WorldClockRepository
import de.jeanlucmakiola.clockula.data.zones.ZoneDirectory
import de.jeanlucmakiola.clockula.domain.WorldClock
import de.jeanlucmakiola.clockula.domain.time.ClockChanges
import de.jeanlucmakiola.clockula.domain.time.Ticker
import de.jeanlucmakiola.clockula.domain.time.WallClock
import de.jeanlucmakiola.clockula.domain.time.ZoneProvider
@@ -17,6 +18,8 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onStart
import java.time.ZoneId
import javax.inject.Inject
import javax.inject.Singleton
@@ -30,6 +33,7 @@ internal class WorldClockSource @Inject constructor(
private val zones: ZoneProvider,
private val wallClock: WallClock,
private val ticker: Ticker,
private val clockChanges: ClockChanges,
) {
/** What the picker needs before it can draw a row; null while it is still being built. */
private data class Catalog(val entries: List<ZoneEntry>, val deviceCity: String)
@@ -39,20 +43,36 @@ internal class WorldClockSource @Inject constructor(
/**
* The tab's state. Both the instant and the device zone are re-read on
* **every tick**, which is what makes `TIME_SET` and `TIMEZONE_CHANGED`
* free: the tab self-heals within one tick and `SystemEventReceiver` needs
* no new branch (D2).
* **every tick**. [ClockChanges] restarts the tick, so `TIME_SET` and
* `TIMEZONE_CHANGED` show at once rather than at the next minute, and the
* restarted ticker measures its next wait against the clock as it now
* reads. It sits outside the `flatMapLatest`, so its receiver is
* registered once per subscription rather than once per list edit, and
* `onStart` lets the first state through without waiting for a broadcast.
* `SystemEventReceiver` still needs no new branch (D2).
*
* The cost of a 200 ms cadence is paid back by the state being stable —
* the face is truncated to the second and the rows to the minute — so
* `distinctUntilChanged` collapses five emissions a second into about one
* (D18).
* [refreshes] does the same for what no broadcast announces: the screen
* sends one on every resume, which covers a per-app language change (the
* activity is recreated, the ViewModel and this flow are not) and a
* `delay` stretched by a deep sleep (the main looper's clock stops while
* the device sleeps) without waiting for the next minute.
*
* The tick is aligned to the wall clock and only as fine as the readouts:
* once a minute, or once a second when the user turned seconds on (#23).
* Everything shown is truncated to that same unit, so each tick is one new
* state and `distinctUntilChanged` only drops the odd early wake (D18).
*/
@OptIn(ExperimentalCoroutinesApi::class)
fun state(): Flow<WorldClockUiState.Loaded> =
combine(worldClocks.worldClocks(), storedHomeZoneId, ::Pair)
.flatMapLatest { (clocks, storedHome) ->
ticker.ticks(WorldClockDefaults.Tick).map { build(clocks, storedHome) }
fun state(refreshes: Flow<Unit>): Flow<WorldClockUiState.Loaded> =
combine(
worldClocks.worldClocks(),
storedHomeZoneId,
settings.worldClockSeconds,
merge(clockChanges.changes(), refreshes).onStart { emit(Unit) },
) { clocks, storedHome, seconds, _ -> Triple(clocks, storedHome, seconds) }
.flatMapLatest { (clocks, storedHome, seconds) ->
ticker.alignedTicks(WorldClockDefaults.tickFor(seconds))
.map { build(clocks, storedHome, seconds) }
}
.distinctUntilChanged()
@@ -101,6 +121,7 @@ internal class WorldClockSource @Inject constructor(
private suspend fun build(
clocks: List<WorldClock>,
storedHome: String?,
showSeconds: Boolean,
): WorldClockUiState.Loaded {
val now = wallClock.now()
val homeZone = HomeZone.resolve(storedHome, zones.current())
@@ -112,9 +133,10 @@ internal class WorldClockSource @Inject constructor(
val entries = directory.entriesFor(zoneIds)
val zoneNames = directory.displayNamesOf(zoneIds, now)
return WorldClockUiState.Loaded(
home = WorldClockRows.home(homeZone, now, entries, zoneNames),
rows = WorldClockRows.from(clocks, homeZone, now, entries, zoneNames),
home = WorldClockRows.home(homeZone, now, entries, zoneNames, showSeconds),
rows = WorldClockRows.from(clocks, homeZone, now, entries, zoneNames, showSeconds),
canAdd = clocks.size < WorldClocks.MAX,
showSeconds = showSeconds,
)
}
@@ -8,6 +8,8 @@ import de.jeanlucmakiola.clockula.data.worldclocks.WorldClockRepository
import de.jeanlucmakiola.clockula.domain.Zones
import de.jeanlucmakiola.clockula.domain.worldclock.WorldClocks
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
@@ -36,9 +38,12 @@ class WorldClockViewModel @Inject internal constructor(
private val query = MutableStateFlow("")
/** One per screen resume; see [onResumed]. Buffered, so a send never suspends. */
private val refreshes = MutableSharedFlow<Unit>(extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
/** `WhileSubscribed(5_000)`, seeded [WorldClockUiState.Loading]. */
internal val state: StateFlow<WorldClockUiState> = source.state()
// `WhileSubscribed`, not `Eagerly`: the upstream is a 200 ms ticker and
internal val state: StateFlow<WorldClockUiState> = source.state(refreshes)
// `WhileSubscribed`, not `Eagerly`: the upstream is a ticker and
// eager sharing would keep it running with nobody looking. The timeout
// is what gives rotation its resilience, as on every other tab.
.stateIn(
@@ -134,6 +139,15 @@ class WorldClockViewModel @Inject internal constructor(
}
}
/**
* The screen came back. Re-reads the clock, the zone and the names at once
* rather than at the next minute: a language change recreates the activity
* but not this ViewModel, and announces itself with no broadcast.
*/
fun onResumed() {
refreshes.tryEmit(Unit)
}
private companion object {
/** The window a rotation has to re-subscribe in before the ticker is dropped. */
const val SUBSCRIPTION_GRACE_MS = 5_000L
+3 -1
View File
@@ -350,9 +350,11 @@
<string name="settings_timer_defaults_hint">How timers ring. A timer can still pick its own sound in its editor.</string>
<string name="settings_timer_vibrate_off">Off. A timer set to Silent still vibrates</string>
<string name="settings_section_clock">Clock</string>
<string name="settings_clock_subtitle">Home time zone</string>
<string name="settings_clock_subtitle">Home time zone and seconds</string>
<string name="settings_clock_hint">World clocks show how far each city is from your home time zone.</string>
<string name="settings_home_zone">Home time zone</string>
<string name="settings_world_clock_seconds">Display time with seconds</string>
<string name="settings_world_clock_seconds_hint">The world clock shows seconds and a second hand. Uses a little more battery.</string>
<string name="settings_self_check">Why might my alarm not ring?</string>
<string name="settings_self_check_title">Why might my alarm not ring?</string>
<string name="settings_self_check_summary">Check exact-alarm, notification and battery settings</string>