Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ object RewardsCopy {
return strings.format(part, t.format(Formatters.of("EEEE, d MMMM yyyy", locale)), sportName.lowercase(locale))
}

/** "Thursday, 30 October 2026": the day a badge was earned, or a best set by a day rather than a workout. */
fun dayLong(date: Long, zone: ZoneId = ZoneId.systemDefault(), locale: Locale = Locale.getDefault()): String =
Instant.ofEpochSecond(date).atZone(zone).format(Formatters.of("EEEE, d MMMM yyyy", locale))

/** "27 October to 2 November": a week from its first day. */
fun weekRange(first: LocalDate, locale: Locale = Locale.getDefault()): String {
val f = Formatters.of("d MMMM", locale)
Expand Down
23 changes: 21 additions & 2 deletions app/src/main/kotlin/org/openstrap/app/ui/dashboard/DayViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.openstrap.app.data.SleepHistory
import org.openstrap.app.data.Summaries
import org.openstrap.app.data.Today
import org.openstrap.app.data.catching
import org.openstrap.app.data.db.DailyPai
import org.openstrap.app.data.db.HealthAlertRow
import org.openstrap.app.data.db.OsaEventRow
import org.openstrap.app.data.db.OsaResultRow
Expand Down Expand Up @@ -170,6 +171,11 @@ data class ActivityHistory(
val steps: Map<LocalDate, Int> = emptyMap(),
/** The most steps of any day before [date]: the stored personal best when it's older, else read from the history. */
val stepsBest: Int? = null,
/**
* The days with Heart points earned, from [STRIP_DAYS_BEFORE] days before [date] to the later of [date] and today: the
* days of a detail's strip, whichever of them is shown, so a day's dot is the same whichever day is open.
*/
val heartPointsDays: Set<LocalDate> = emptySet(),
) {
/** [heartPoints] when they're [day]'s, else none yet. */
fun heartPointsOn(day: LocalDate): Map<LocalDate, HeartPointsDay> = if (date == day) heartPoints else emptyMap()
Expand Down Expand Up @@ -197,6 +203,9 @@ data class ActivityHistory(
/** Heart points' month. */
const val HEART_POINTS_DAYS = 30

/** A detail's strip starts this many days before its last day. */
const val STRIP_DAYS_BEFORE = 6L

/** Calories' year and the year before it, for the change against it. */
val CALORIES_DAYS = 2 * Range.Year.days
}
Expand Down Expand Up @@ -347,22 +356,32 @@ class DayViewModel(
/** Each day's Heart points and stored calories up to the day shown, for the Activity tab and its details. */
val activityHistory: StateFlow<ActivityHistory> = selected.flatMapLatest { d ->
combine(
repo.dailyPai(d.minusDays(ActivityHistory.HEART_POINTS_DAYS - 1L), d),
// The days after [d] up to today, for the strip's dots; nothing is read for today.
stripLater(d),
repo.dailyCalories(d.minusDays(ActivityHistory.CALORIES_DAYS - 1L), d),
stepsAndBest(d),
) { pai, calories, (steps, best) ->
repo.dailyPai(d.minusDays(ActivityHistory.HEART_POINTS_DAYS - 1L), d),
) { later, calories, (steps, best), pai ->
ActivityHistory(
date = d,
heartPoints = pai.mapValues { (_, p) -> HeartPointsDay(p.daily, p.total) },
calories = calories.mapValues { (_, e) -> CaloriesDay(e.activeKcal, e.totalKcal, e.workoutKcal, e.partial) },
steps = steps,
stepsBest = best,
heartPointsDays = (pai.filterKeys { it >= d.minusDays(ActivityHistory.STRIP_DAYS_BEFORE) } + later)
.filterValues { it.daily > 0 }.keys,
)
}
}
.flowOn(work)
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), ActivityHistory())

/** The Heart points of the days after [d] up to today, for the strip's later days; none when [d] is today. */
private fun stripLater(d: LocalDate): Flow<Map<LocalDate, DailyPai>> {
val today = Today.on(clock())
return if (d < today) repo.dailyPai(d.plusDays(1), today) else flowOf(emptyMap())
}

/**
* [d]'s steps from [ActivityHistory.STEPS_DAYS] days before it to its week's end, and the most steps of a day
* before it: the stored personal best when it's from an earlier day (with the loaded days, as it can lag a sync),
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/kotlin/org/openstrap/app/ui/dashboard/Frame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fun peekDay(date: LocalDate, locale: Locale = Locale.getDefault()): String =
* What the strap chip in the header shows: a [dot] and, only when they matter, a few words ([text] may be empty).
* [description] is read out by a screen reader.
*/
data class StrapChip(val text: String, val dot: Dot, val description: String) {
data class StrapChip(val text: String, val dot: Dot, val description: String, val battery: Int? = null) {
enum class Dot {
/** Connected and all is well. */
Fine,
Expand All @@ -51,7 +51,10 @@ data class StrapChip(val text: String, val dot: Dot, val description: String) {
/** Connected and charging. */
Charging,

/** Something needs a look: not synced, or not seen for a while. Amber. */
/** The strap is there but its last sync failed. Orange. */
Unsynced,

/** The strap isn't connected: not seen for a while. Shown as the strap, broken. */
Warn,

/** Nothing to report yet (connecting), or nothing paired. Dim. */
Expand Down Expand Up @@ -106,19 +109,20 @@ fun strapChip(
when {
connection.charging && battery != null -> StrapChip(
strings.format(R.string.day_chip_charging, battery), StrapChip.Dot.Charging,
strings.plural(R.plurals.day_chip_charging_description, battery, battery),
strings.plural(R.plurals.day_chip_charging_description, battery, battery), battery,
)
sync is SyncState.Failed -> StrapChip(
strings.get(R.string.day_chip_not_synced), StrapChip.Dot.Warn, strings.get(R.string.day_chip_not_synced_description),
strings.get(R.string.day_chip_not_synced), StrapChip.Dot.Unsynced,
strings.get(R.string.day_chip_not_synced_description),
)
battery == null -> StrapChip("", StrapChip.Dot.Fine, strings.get(R.string.day_chip_connected_description))
StrapBands.lowBattery(battery, connection.charging) -> StrapChip(
strings.format(R.string.day_chip_battery, battery), StrapChip.Dot.Low,
strings.plural(R.plurals.day_chip_battery_low_description, battery, battery),
strings.plural(R.plurals.day_chip_battery_low_description, battery, battery), battery,
)
else -> StrapChip(
strings.format(R.string.day_chip_battery, battery), StrapChip.Dot.Fine,
strings.plural(R.plurals.day_chip_battery_description, battery, battery),
strings.plural(R.plurals.day_chip_battery_description, battery, battery), battery,
)
}
}
Expand Down
38 changes: 4 additions & 34 deletions app/src/main/kotlin/org/openstrap/app/ui/dashboard/MomentHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import org.openstrap.app.ui.theme.ControlSurface
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.CircleShape
Expand Down Expand Up @@ -103,7 +104,7 @@ fun MomentHeader(
)
Spacer(Modifier.width(8.dp))
}
growClick(onOpenStrap).let { g -> StrapChipButton(chip, g.onClick, g.modifier) }
growClick(onOpenStrap).let { g -> StrapPill(chip, g.onClick, g.modifier) }
}
},
)
Expand Down Expand Up @@ -200,37 +201,6 @@ private fun Peek(text: String) {
Text(text, Modifier.width(PeekWidth), color = Color.White.copy(alpha = 0.22f), style = GlowType.body, maxLines = 1, softWrap = false)
}

/** The strap chip: a dot, and words only when they matter. */
@Composable
private fun StrapChipButton(chip: StrapChip, onClick: () -> Unit, modifier: Modifier = Modifier) {
val shape = CircleShape
Row(
modifier.clip(shape).background(ChipSurface).border(1.dp, Color.White.copy(alpha = 0.07f), shape)
.clickable(onClickLabel = stringResource(R.string.day_header_open_strap), onClick = onClick)
.semantics { contentDescription = chip.description }
.padding(horizontal = 12.dp, vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Box(Modifier.size(7.dp).background(chipDotColor(chip.dot), CircleShape))
if (chip.text.isNotEmpty()) {
Spacer(Modifier.width(7.dp))
Text(
chip.text, maxLines = 1, softWrap = false,
color = if (chip.dot == StrapChip.Dot.Warn) MetricColors.warn else Color.White,
style = GlowType.secondary,
)
}
}
}

private fun chipDotColor(dot: StrapChip.Dot): Color = when (dot) {
StrapChip.Dot.Fine -> MetricColors.good
StrapChip.Dot.Low -> MetricColors.battery(StrapChip.LOW_BATTERY)
StrapChip.Dot.Charging -> MetricColors.good
StrapChip.Dot.Warn -> MetricColors.warn
StrapChip.Dot.Quiet -> Color.White.copy(alpha = 0.3f)
}

private val Avatar = Color(0xFF1C1C1E)
private val ChipSurface = Color(0xFF16161A)
// The Control surface (spec 2.2): the avatar is a control.
private val Avatar = ControlSurface
private val PeekWidth = 34.dp
201 changes: 201 additions & 0 deletions app/src/main/kotlin/org/openstrap/app/ui/dashboard/StrapPill.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package org.openstrap.app.ui.dashboard

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.minimumInteractiveComponentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.clipRect
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import org.openstrap.app.R
import org.openstrap.app.ui.components.Motion
import org.openstrap.app.ui.components.rememberAnimationsOn
import org.openstrap.app.ui.glow.GlowType
import org.openstrap.app.ui.glow.StrapDrawing
import org.openstrap.app.ui.theme.AppIcons
import org.openstrap.app.ui.theme.ControlSurface
import org.openstrap.app.ui.theme.MetricColors

/*
* The strap pill in the header (spec 3.1). The pill is the strap's state, not a dot with words:
* - connected: green as far as the battery, fading out into the pill, the percent in the middle (amber when low)
* - charging: the same green, flowing, with a bolt before the number
* - not synced: orange all over, with the (i)
* - not connected, connecting or not paired: the strap as the Strap page draws it, dimmed, with its words ("2 h ago",
* "Not connected"), which tell the three apart. Not connected is mostly out of range or Bluetooth off and passes, so
* it isn't drawn as damage.
* Green and amber are for the strap and the sync only, never a health value (spec 2.2).
*/

/** A fill's two colours: rich where it starts, softer where it runs out. */
private class Tint(val rich: Color, val soft: Color)

/** The orange of a sync that failed: the sync amber, deepened. */
private val OrangeDeep = Color(0xFFE5672A)

private fun tintOf(dot: StrapChip.Dot): Tint? = when (dot) {
StrapChip.Dot.Fine, StrapChip.Dot.Charging -> Tint(MetricColors.good.copy(alpha = 0.62f), MetricColors.good.copy(alpha = 0.3f))
StrapChip.Dot.Low -> Tint(MetricColors.warn.copy(alpha = 0.62f), MetricColors.warn.copy(alpha = 0.3f))
StrapChip.Dot.Unsynced -> Tint(MetricColors.warn.copy(alpha = 0.55f), OrangeDeep.copy(alpha = 0.85f))
StrapChip.Dot.Warn, StrapChip.Dot.Quiet -> null
}

/** How much of the pill is filled: the battery, or all of it when there's no reading or the colour is the point. */
private fun fillOf(chip: StrapChip): Float = when (chip.dot) {
StrapChip.Dot.Fine, StrapChip.Dot.Low, StrapChip.Dot.Charging -> (chip.battery?.toFloat() ?: FULL) / FULL
StrapChip.Dot.Unsynced -> 1f
StrapChip.Dot.Warn, StrapChip.Dot.Quiet -> 0f
}

private const val FULL = 100f

@Composable
internal fun StrapPill(chip: StrapChip, onClick: () -> Unit, modifier: Modifier = Modifier) {
val tint = tintOf(chip.dot)
// A change of level moves the fill rather than cutting.
val fill by animateFloatAsState(fillOf(chip), tween(FILL_MS, easing = Motion.easing), label = "strap fill")
val flow = rememberChargeFlow(chip.dot == StrapChip.Dot.Charging)
Row(
// 48 dp tall to tap, the pill drawn 32 dp tall in the middle of it; the header row is 60 dp, so nothing moves.
// [modifier] comes after, so the Strap page grows out of the pill as drawn, not the room round it.
Modifier.minimumInteractiveComponentSize().then(modifier)
.clip(CircleShape).background(ControlSurface)
// Read where it is drawn, so the fill and its flow redraw without composing the pill again.
.drawBehind { if (tint != null) drawFill(tint, fill, flow.value) }
.border(1.dp, Color.White.copy(alpha = 0.07f), CircleShape)
.clickable(onClickLabel = stringResource(R.string.day_header_open_strap), onClick = onClick)
.semantics { contentDescription = chip.description }
.defaultMinSize(minWidth = MIN_WIDTH, minHeight = HEIGHT)
.padding(horizontal = 12.dp, vertical = 4.dp),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
) { PillContent(chip) }
}

/**
* The colour from the left edge to [fill] of the width, then fading out into the pill's own surface over [FADE], so it
* ends in a soft edge like the lit cards, never a line. While charging ([flow] 0 to 1, -1 for still) the colour flows
* along the pill, swelling and thinning, and the fade holds it in.
*/
private fun DrawScope.drawFill(tint: Tint, fill: Float, flow: Float) {
val level = size.width * fill.coerceIn(0f, 1f)
if (level <= 0f) return
val full = fill >= 1f
val end = if (full) size.width else minOf(size.width, level + FADE.toPx())
val body = if (flow < 0f) {
Brush.horizontalGradient(listOf(tint.rich, tint.soft), startX = 0f, endX = end)
} else {
// One swell is a period long and repeats, so sliding it along never shows a seam.
val period = size.width * FLOW_PERIOD
Brush.horizontalGradient(
listOf(tint.soft, tint.rich, tint.soft), startX = period * (flow - 1f), endX = period * flow, tileMode = TileMode.Repeated,
)
}
clipRect(0f, 0f, end, size.height) { drawRect(body) }
if (!full) {
val from = maxOf(0f, level - FADE.toPx() * FADE_LEAD)
drawRect(
Brush.horizontalGradient(listOf(Color.Transparent, ControlSurface), startX = from, endX = end),
topLeft = Offset(from, 0f), size = Size(end - from, size.height),
)
}
}

/** How far along the pill the flow has come, 0 to 1, round and round while charging; -1 when it isn't flowing. */
@Composable
private fun rememberChargeFlow(charging: Boolean): State<Float> {
val animate = rememberAnimationsOn()
if (!charging || !animate) return NoFlow
return rememberInfiniteTransition(label = "strap charge").animateFloat(
0f, 1f, infiniteRepeatable(tween(FLOW_MS, easing = LinearEasing)), label = "strap charge flow",
)
}

private val NoFlow: State<Float> = mutableFloatStateOf(-1f)

@Composable
private fun PillContent(chip: StrapChip) {
when (chip.dot) {
StrapChip.Dot.Fine, StrapChip.Dot.Low ->
if (chip.text.isNotEmpty()) PillText(chip.text) else Box(Modifier.size(6.dp).background(Color.White, CircleShape))
StrapChip.Dot.Charging -> Row(verticalAlignment = Alignment.CenterVertically) {
Icon(AppIcons.Bolt, null, Modifier.size(13.dp), tint = Color.White)
Spacer(Modifier.width(2.dp))
PillText(chip.battery?.toString() ?: chip.text)
}
StrapChip.Dot.Unsynced -> Row(verticalAlignment = Alignment.CenterVertically) {
Icon(AppIcons.Info, null, Modifier.size(15.dp), tint = Color.White)
Spacer(Modifier.width(6.dp))
PillText(chip.text)
}
StrapChip.Dot.Warn, StrapChip.Dot.Quiet -> StrapAndWords(chip.text)
}
}

/** The Strap page's strap drawing, small (its light out and the drawing dimmed, as it is when the strap isn't there). */
@Composable
private fun StrapAndWords(text: String) {
Row(verticalAlignment = Alignment.CenterVertically) {
StrapDrawing(
led = Color.White, lit = false, syncing = false, modifier = Modifier.size(width = MARK_WIDTH, height = MARK_HEIGHT),
lineScale = MARK_LINES,
)
if (text.isNotEmpty()) {
Spacer(Modifier.width(7.dp))
PillText(text)
}
}
}

@Composable
private fun PillText(text: String) {
Text(
text, maxLines = 1, softWrap = false, color = Color.White,
style = GlowType.secondary.copy(fontWeight = FontWeight.SemiBold, fontFeatureSettings = "tnum"),
)
}

private const val FILL_MS = 600
private const val FLOW_MS = 2600
private const val FLOW_PERIOD = 0.9f
private const val FADE_LEAD = 0.5f
private const val MARK_LINES = 4f
private val MIN_WIDTH = 56.dp
private val HEIGHT = 32.dp
private val FADE = 22.dp
private val MARK_WIDTH = 19.dp
private val MARK_HEIGHT = 24.dp
Loading