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 @@ -215,6 +215,17 @@ object RestMath {
if (total <= 0) return 0f
return ((end - now).toFloat() / total).coerceIn(0f, 1f)
}

/** The ±15 pill's running total: a change of the end adds up, a jump of over 2 minutes (a resync, a new rest) is not a tap. */
fun pillSum(sum: Int, oldEnd: Long, newEnd: Long): Int {
val d = Math.round((newEnd - oldEnd) / 1000.0).toInt()
return if (d == 0 || kotlin.math.abs(d) > 120) sum else sum + d
}

fun pillText(sum: Int): String? = if (sum == 0) null else (if (sum > 0) "+" else "−") + kotlin.math.abs(sum) + " s"

/** The last three seconds, as shown (the clock rounds up). */
fun warn(leftSec: Double): Boolean = kotlin.math.ceil(leftSec) in 1.0..3.0
}

object Effort {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ch.duartesantos.opengym.wear.ui

import androidx.compose.animation.AnimatedContent
import ch.duartesantos.opengym.wear.RestMath
import ch.duartesantos.opengym.wear.Stills
import androidx.compose.foundation.layout.offset
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.draw.alpha
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.Image
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
Expand Down Expand Up @@ -39,6 +41,8 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
Expand All @@ -54,6 +58,7 @@ import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
Expand Down Expand Up @@ -728,34 +733,67 @@ fun RestView(rest: Rest) {
val now = rememberNow()
val left = ((rest.endsAt - now) / 1000.0).coerceAtLeast(0.0)
val round = LocalRound.current
// The ±15 pill adds up quick taps for a moment; a rest's own first end is not a change.
var sum by remember(rest.startedAt) { mutableIntStateOf(0) }
var lastEnd by remember(rest.startedAt) { mutableLongStateOf(rest.endsAt) }
LaunchedEffect(rest.endsAt) {
sum = RestMath.pillSum(sum, lastEnd, rest.endsAt)
lastEnd = rest.endsAt
kotlinx.coroutines.delay(1_100)
sum = 0
}
BoxWithConstraints(Modifier.fillMaxSize()) {
// Round watches lose their corners: the bottom row sits higher, the extra lines go first.
val roomy = maxHeight.value >= (if (round) 260 else 200)
// Measured: up next and the effort chips together need 222 dp at the normal text size (and ~60 dp of it is text).
// Measured: up next and the effort chips together need 224 dp at the normal text size (~62 dp of it is text, at 12 sp).
// Without that (a 40/41 mm watch) the question comes first, then what's next.
val both = roomy || maxHeight.value >= 222 + (LocalDensity.current.fontScale - 1f) * 60
Column(Modifier.fillMaxSize().padding(start = if (round) 20.dp else 6.dp, end = if (round) 20.dp else 6.dp, top = 22.dp, bottom = if (round) 26.dp else 6.dp),
horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(2.dp)) {
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
val both = roomy || maxHeight.value >= 224 + (LocalDensity.current.fontScale - 1f) * 62
// Always On: no chips to tap, so Up next takes their place on a watch without room for both
// (40 mm emulator: the hidden chips left the screen empty where release-1.4.5 showed what's next).
val rate = WearStore.canRateLast() && (both || (rated == null && !dim))
val next = s.settings.show?.showNext != false && (both || !rate)
// Round: the clock's arc reaches lower at its ends, where ‹ sits: 6 dp more under it.
Column(Modifier.fillMaxSize().padding(start = if (round) 20.dp else 6.dp, end = if (round) 20.dp else 6.dp, top = if (round) 28.dp else 22.dp, bottom = if (round) 26.dp else 6.dp),
horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(3.dp)) {
// The time left, the screen's one number (#1100): in the accent, as big as the room the rest leaves.
// Never shorter than ‹, and on a round screen ‹ steps in from the curve (seen on a 40 mm watch: it
// was cut at its corner under the clock once Up next took the chips' place).
val inset = if (round) 6.dp else 0.dp
Row(Modifier.fillMaxWidth().weight(1f).heightIn(min = Og.Tap), verticalAlignment = Alignment.CenterVertically) {
Spacer(Modifier.width(inset))
if (last != null && !dim) ArrowButton(true, true) { WearStore.fixLast() } else Spacer(Modifier.width(32.dp))
Column(Modifier.weight(1f), horizontalAlignment = Alignment.CenterHorizontally) {
Quiet(stringResource(R.string.rest), 11.sp, lines = 1)
Text(clock(kotlin.math.ceil(left).toLong()), style = Og.Number, fontSize = if (roomy) 42.sp else 36.sp, maxLines = 1,
color = Og.Text.copy(alpha = if (dim) 0.75f else 1f))
val text = clock(kotlin.math.ceil(left).toLong())
BoxWithConstraints(Modifier.weight(1f).fillMaxHeight(), contentAlignment = Alignment.Center) {
val tint = if (RestMath.warn(left)) Og.Orange else a.accent
// Digits are ~0.62 of the font size wide: the number fits the box both ways.
// A line of text is ~1.1 of its size tall (not just the digits): it never runs into the bar.
val px = minOf(maxHeight.value / 1.1f, maxWidth.value / (text.length * 0.62f)).coerceIn(28f, 76f)
Text(text, style = Og.Number, fontSize = with(LocalDensity.current) { px.dp.toSp() }, maxLines = 1, fontWeight = FontWeight.SemiBold,
color = tint.copy(alpha = if (dim) 0.75f else 1f),
modifier = Modifier.semantics { contentDescription = text })
RestMath.pillText(sum)?.takeIf { !dim }?.let { pill ->
val c = if (sum > 0) a.accent else Og.Orange
Box(Modifier.align(Alignment.TopCenter).height(20.dp).clip(CircleShape).background(Color.Black).background(c.copy(alpha = 0.18f))
.padding(horizontal = 8.dp).clearAndSetSemantics {}, contentAlignment = Alignment.Center) {
Text(pill, color = c, fontSize = 12.sp, fontWeight = FontWeight.Bold, maxLines = 1, style = Og.Number)
}
}
}
Spacer(Modifier.width(32.dp))
Spacer(Modifier.width(32.dp + inset))
}
if (s.settings.show?.rest != "plain") TimeBar(rest.startedAt, rest.endsAt, a.accent, Modifier.padding(horizontal = 8.dp))
val rate = WearStore.canRateLast() && !dim && (both || rated == null)
if (s.settings.show?.showNext != false && (both || !rate)) UpNext(roomy)
Spacer(Modifier.weight(1f))
if (rate) EffortChips(roomy)
// Round: the row sits low, where the circle narrows, so −15 and +15 step in from the edge.
if (!dim) Row(Modifier.fillMaxWidth().padding(horizontal = if (LocalRound.current) 8.dp else 0.dp), horizontalArrangement = Arrangement.spacedBy(4.dp), verticalAlignment = Alignment.CenterVertically) {
EdgeButton("−15 s", text = "−15", size = 40.dp) { WearStore.minusRest() }
RowButton(stringResource(R.string.skip), Modifier.weight(1f).height(40.dp), icon = G.NEXT, iconWhenTight = true) { WearStore.skipRest() }
EdgeButton("+15 s", text = "+15", size = 40.dp) { WearStore.addRest(15) }
} else Spacer(Modifier.height(40.dp))
// −15, Skip, +15 mid-screen, where a round screen is widest. Always On hides them but keeps their place.
Row(Modifier.fillMaxWidth().alpha(if (dim) 0f else 1f), horizontalArrangement = Arrangement.spacedBy(4.dp), verticalAlignment = Alignment.CenterVertically) {
EdgeButton("−15 s", text = "−15", size = 40.dp, enabled = !dim) { WearStore.minusRest() }
RowButton(stringResource(R.string.skip), Modifier.weight(1f).height(40.dp), icon = G.NEXT, iconWhenTight = true, enabled = !dim) { WearStore.skipRest() }
EdgeButton("+15 s", text = "+15", size = 40.dp, enabled = !dim) { WearStore.addRest(15) }
}
// A fixed place for the effort, so rating it moves nothing below.
// Round: the chips sit low, where the circle narrows; at a large font their outer corners met the edge (45 mm emulator).
if (rate) Box(Modifier.fillMaxWidth().padding(horizontal = if (round) 10.dp else 0.dp).height(if (roomy) 46.dp else 30.dp).alpha(if (dim) 0f else 1f), contentAlignment = Alignment.Center) {
if (!dim) EffortChips(roomy)
}
if (next) UpNext(roomy)
}
}
}
Expand All @@ -768,10 +806,37 @@ private fun UpNext(roomy: Boolean) {
val e = WearStore.entry() ?: return
val si = WearStore.setIndex() ?: return
val side = e.sets[si].sideUp
val line = stringResource(R.string.up_next) + ": " + stringResource(R.string.up_next_set, e.name, SetNav.place(e, si).first) +
val a = LocalAccent.current
val dim = LocalAmbient.current
val set = stringResource(R.string.up_next_set, e.name, SetNav.place(e, si).first) +
(side?.let { " · " + stringResource(if (it == "L") R.string.left else R.string.right) } ?: "")
Quiet(line, 11.sp, lines = if (roomy) 2 else 1)
nextSetNumbers(e, si, w.unit)?.let { Text(it, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, maxLines = 1, style = Og.Number) }
val numbers = nextSetNumbers(e, si, w.unit)
// The workout's time (#1100), standing still while the phone has the workout paused.
val time: @Composable () -> Unit = {
WearStore.workoutStart()?.let { start ->
val now = rememberNow()
val at = w.pausedAt?.takeIf { it > 0 }?.toLong() ?: now
Text(clock(((at - start) / 1000).coerceAtLeast(0)), color = Og.Label2.copy(alpha = if (dim) 0.75f else 1f), fontSize = 12.sp,
fontWeight = FontWeight.Bold, style = Og.Number, maxLines = 1)
}
}
if (roomy) {
// "Up next" in the accent with the time at its end, then the set, then what to load.
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
Text(stringResource(R.string.up_next), color = a.accent.copy(alpha = if (dim) 0.75f else 1f), fontSize = 12.sp, fontWeight = FontWeight.Bold,
maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f))
time()
}
Quiet(set, 12.sp, lines = 2)
numbers?.let { Text(it, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, maxLines = 1, style = Og.Number) }
} else {
// A 40/41 mm watch has two lines for it (measured on the emulator): "Up next: the set", then what to load and the time.
Quiet(stringResource(R.string.up_next) + ": " + set, 12.sp, lines = 1)
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically) {
numbers?.let { Text(it, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, maxLines = 1, overflow = TextOverflow.Ellipsis, style = Og.Number, modifier = Modifier.weight(1f, fill = false)); Spacer(Modifier.width(8.dp)) }
time()
}
}
}

/** "RIR · How hard was that set?" and four chips; once rated, the rating in a small capsule that opens the chips again. */
Expand All @@ -795,7 +860,7 @@ private fun EffortChips(roomy: Boolean) {
Spacer(Modifier.width(4.dp)); Glyph(G.CHECK, c, 10.dp)
}
} else Column(horizontalAlignment = Alignment.CenterHorizontally) {
if (roomy) Quiet("${Effort.name(kind)} · " + stringResource(R.string.how_hard), 10.sp, lines = 1)
if (roomy) Quiet("${Effort.name(kind)} · " + stringResource(R.string.how_hard), 12.sp, lines = 1)
Row(Modifier.fillMaxWidth().padding(horizontal = 6.dp), horizontalArrangement = Arrangement.spacedBy(4.dp)) {
for (v in Effort.chips(kind)) {
val c = Og.effortColor(Effort.band(kind, v), a.accent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ class WatchLogicTest {
assertEquals("a backwards rest is over", 0f, RestMath.left(end, start, now), 0f)
}

@Test fun restPillAddsQuickTapsAndIgnoresJumps() {
assertEquals(15, RestMath.pillSum(0, 60_000, 75_000))
assertEquals(30, RestMath.pillSum(15, 75_000, 90_000))
assertEquals(0, RestMath.pillSum(15, 90_000, 75_000))
assertEquals("a clamp shows the real change", 12, RestMath.pillSum(0, 60_000, 72_000))
assertEquals("a jump over 120 s is ignored", 15, RestMath.pillSum(15, 75_000, 300_000))
assertEquals(15, RestMath.pillSum(15, 75_000, 75_000))
}

@Test fun restPillText() {
assertEquals("+30 s", RestMath.pillText(30))
assertEquals("−15 s", RestMath.pillText(-15))
assertNull(RestMath.pillText(0))
}

@Test fun restWarnsInTheLastThreeSeconds() {
assertTrue(RestMath.warn(3.0))
assertTrue(RestMath.warn(0.6))
assertFalse(RestMath.warn(3.4))
assertFalse(RestMath.warn(0.0))
}

@Test fun chipsAreRatingsThePhoneLogs() {
assertEquals(listOf(0.0, 1.0, 2.0, 3.0), Effort.chips("rir"))
assertEquals(listOf(10.0, 9.0, 8.0, 7.0), Effort.chips("rpe"))
Expand Down
Loading