fix: capture working weight automatically

This commit is contained in:
Manuel committed 2026-09-02 17:48:53 +02:00
1 parent 272bf785ee
commit 697e482033
6 files changed
+109 -41

No files matched your search

+16 -19
View File
@@ -4,14 +4,14 @@ import { useStore } from '../store/useStore.js'
import { useUI } from '../store/useUI.js'
import { exOr } from '../lib/exercises.js'
import { usesBar, barWeightFor, plateSplit } from '../lib/bar.js'
import { effectiveRoutine, lastEntryFor, bestWeightFor, buildSets, freestyleConfig, defaultConfig, setsDoneActive, supersetUnits, unitOf, setLabel, modeOf, isBw, isPerSide, sideReps, repStep, EFFORT, effortOf, stepEffort, capEffort, cascadeWeight, insertWarmupRow, removeRowAt, pairAdjacent, unpairSuperset, cleanupSg, applyIntensifierPlan, pinnedNoteFor, exNoteFor } from '../lib/history.js'
import { effectiveRoutine, lastEntryFor, bestWeightFor, bestWeightForEntry, buildSets, freestyleConfig, defaultConfig, setsDoneActive, supersetUnits, unitOf, setLabel, modeOf, isBw, isPerSide, sideReps, repStep, EFFORT, effortOf, stepEffort, capEffort, cascadeWeight, insertWarmupRow, removeRowAt, pairAdjacent, unpairSuperset, cleanupSg, applyIntensifierPlan, pinnedNoteFor, exNoteFor } from '../lib/history.js'
import { fmtNum, fmtDate, todayISO, exCount, DAYN } from '../lib/format.js'
import { beep, vibrate } from '../lib/sound.js'
import { t, exerciseNameFor } from '../lib/i18n.js'
import { api } from '../lib/api.js'
import { insertionIndexAfterCurrentUnit, nextUnfinishedUnit, setProgressHighWater, supersetFlowStep, restAfterSet, restOnRecheck, restSecFor } from '../lib/supersetFlow.js'
import Media from '../components/Media.jsx'
import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, topWeightSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet } from '../sheets.jsx'
import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet } from '../sheets.jsx'
import Icon from '../components/Icon.jsx'
import { Button, Check, NumberField } from '../components/ui.jsx'
import { nextPrescription, applyPrescription, defaultIncrement } from '../lib/progression.js'
@@ -107,8 +107,7 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
// Only worth surfacing while there is still work left: once the exercise is finished, a note
// telling you what to do in it is behind you, and the block is already long.
const pinnedNote = entry.sets.some(s => !s.done) ? pinnedNoteFor(S, entry.id) : null
// The same number the "confirm your working weight" sheet calls your best, so the two
// never disagree inside one session: heaviest logged set, or the working weight you kept.
// The number is the heaviest logged set, or the working weight you kept.
const best = cardio ? 0 : Math.max(bestWeightFor(S, entry.id), (S.exWeights[entry.id] || {}).w || 0)
// What the progression policy decided for this session, and why (issue #17). Computed when
// the session was built so the reason matches the numbers already in the rows.
@@ -530,25 +529,24 @@ function ActiveWorkout() {
const toggle = (idx, i) => {
const m = modeAt(idx)
const cardioEntry = m === 'cardio'
let askTop = false, exJustDone = false, workoutDone = false, checked = false
mutEntry(idx, e => {
let exJustDone = false, workoutDone = false, checked = false
update(s => {
const e = s.active.entries[idx]
e.sets[i].done = !e.sets[i].done
checked = e.sets[i].done
if (e.sets[i].done) {
beep(S.sound, 1040, 0.12); vibrate(30)
const unitDone = unit.every(ui => (ui === idx ? e : A.entries[ui]).sets.every(x => x.done))
if (unitDone) workoutDone = !nextUnfinishedUnit(A.entries, supersetUnits(A.entries), idx)
// Only loaded reps training has a "working weight" worth confirming — a bodyweight
// plank has nothing to put in that slider, and neither does a set of push-ups
// (issue #32: the fewest taps that still record what happened).
const loaded = m === 'reps' && !(isBw({ ...(e.target || {}), id: e.id }) && !e.sets.some(x => x.w > 0))
if (e.sets.every(x => x.done)) { exJustDone = true; if (loaded && !e.asked) { e.asked = true; askTop = true } }
if (e.sets.every(x => x.done)) {
exJustDone = true
const mx = bestWeightForEntry(e)
e.topW = mx || null
if (!s.active.backfill && mx > 0) { const cur = s.exWeights[e.id]; if (!cur || mx > cur.w) s.exWeights[e.id] = { w: mx, d: todayISO() } }
}
}
})
// reps: topWeight first (it chains into the finish/continue prompt on the last unit).
// cardio/timed or already-confirmed: go straight to the prompt.
if (askTop) topWeightSheet(idx)
else if (workoutDone) workoutCompleteSheet()
}, true)
if (workoutDone) workoutCompleteSheet()
else if (exJustDone && cardioEntry) useUI.getState().toast(t('Cardio logged'))
else if (exJustDone && m === 'time') useUI.getState().toast(t('Hold logged'))
@@ -584,7 +582,7 @@ function ActiveWorkout() {
// stopRest() first so a rest that belongs after this set replaces the one that was running.
if (freshUnitDone) stopRest()
if (!freshUnit || freshUnit.length <= 1) {
if (freshUnitDone && !askTop && nextUnit?.length) update(s => { if (s.active) s.active.cur = nextUnit[0] })
if (freshUnitDone && nextUnit?.length) update(s => { if (s.active) s.active.cur = nextUnit[0] })
if (!restBeforeWarmup && restAfterSet({ unitDone: freshUnitDone, lastUnit: freshWorkoutDone })) startRest(restSec, idx)
return
}
@@ -593,8 +591,7 @@ function ActiveWorkout() {
if (!step) return
if (step.unitDone) {
if (nextUnit?.length) {
// The top-weight sheet's explicit "Just close" path owns the choice not to advance.
if (!askTop) update(s => { if (s.active) s.active.cur = nextUnit[0] })
update(s => { if (s.active) s.active.cur = nextUnit[0] })
if (!restBeforeWarmup) startRest(restSec, idx)
}
} else {
+44 -9
View File
@@ -205,6 +205,40 @@ describe('Workout set completion flow', () => {
expect(mocks.startRest).not.toHaveBeenCalled()
})
it('auto-captures a completed weighted exercise without prompting and keeps navigation/rest', async () => {
await mount([exercise('plain-bench', [false]), exercise('next', [false])])
await toggleSet(0)
expect(mocks.S.active.entries[0].topW).toBe(60)
expect(mocks.S.exWeights['plain-bench'].w).toBe(60)
expect(mocks.topWeightSheet).not.toHaveBeenCalled()
expect(mocks.S.active.cur).toBe(1)
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
})
it('auto-captures a completed superset exercise without prompting and preserves superset flow', async () => {
const group = 'superset-1'
await mount([
exercise('superset-a', [false], { sg: group }),
exercise('superset-b', [false], { sg: group }),
exercise('next', [false]),
])
await toggleSet(0)
expect(mocks.topWeightSheet).not.toHaveBeenCalled()
expect(mocks.S.active.cur).toBe(1)
expect(mocks.startRest).not.toHaveBeenCalled()
await rerender()
await toggleSet(1)
expect(mocks.topWeightSheet).not.toHaveBeenCalled()
expect(mocks.S.active.cur).toBe(2)
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
})
it.each(['warmup', 'warm-up', 'warm_up'])(
'does not start transition rest before an incomplete %s row in the next ordinary exercise',
async phase => {
@@ -245,7 +279,7 @@ describe('Workout set completion flow', () => {
expect(mocks.startRest).not.toHaveBeenCalled()
})
it('does not start transition rest before a warm-up while top-weight confirmation owns navigation', async () => {
it('advances without transition rest before an incomplete warm-up', async () => {
await mount([
exercise('current-loaded', [false]),
exercise('next', [false, false], {
@@ -259,8 +293,8 @@ describe('Workout set completion flow', () => {
await toggleSet(0)
expect(mocks.topWeightSheet).toHaveBeenCalledWith(0)
expect(mocks.S.active.cur).toBe(0)
expect(mocks.topWeightSheet).not.toHaveBeenCalled()
expect(mocks.S.active.cur).toBe(1)
expect(mocks.startRest).not.toHaveBeenCalled()
})
@@ -284,7 +318,7 @@ describe('Workout set completion flow', () => {
expect(mocks.startRest).not.toHaveBeenCalled()
})
it('leaves a completed superset selected while its top-weight sheet owns the advance choice', async () => {
it('advances after a completed superset without opening a top-weight sheet', async () => {
const group = 'superset-1'
await mount([
exercise('superset-a', [true, true, true], { sg: group, asked: true }),
@@ -293,8 +327,8 @@ describe('Workout set completion flow', () => {
], 1)
await toggleSet(5)
expect(mocks.topWeightSheet).toHaveBeenCalledWith(1)
expect(mocks.S.active.cur).toBe(1)
expect(mocks.topWeightSheet).not.toHaveBeenCalled()
expect(mocks.S.active.cur).toBe(2)
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
})
@@ -327,7 +361,7 @@ describe('Workout set completion flow', () => {
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
})
it('keeps top-weight confirmation in control without declaring completion while work remains', async () => {
it('advances a completed ordinary exercise without declaring completion while work remains', async () => {
await mount([
exercise('current-loaded', [false]),
exercise('pending', [false], { asked: true }),
@@ -335,9 +369,10 @@ describe('Workout set completion flow', () => {
await toggleSet(0)
expect(mocks.topWeightSheet).toHaveBeenCalledWith(0)
expect(mocks.topWeightSheet).not.toHaveBeenCalled()
expect(mocks.workoutCompleteSheet).not.toHaveBeenCalled()
expect(mocks.S.active.cur).toBe(0)
expect(mocks.S.active.cur).toBe(1)
expect(mocks.startRest).toHaveBeenCalledWith(90, expect.any(Number))
})
it('shows workout completion only when no unfinished unit remains', async () => {