From 85ecf3563e13545bc177a65830e4822c5bfc15d4 Mon Sep 17 00:00:00 2001 From: Space_Hermes Date: Thu, 27 Aug 2026 16:50:59 +0200 Subject: [PATCH] feat(workout): swipe between active units --- frontend/src/index.css | 1 + frontend/src/views/Workout.jsx | 53 +++++- frontend/src/views/Workout.test.jsx | 268 +++++++++++++++++++++++++++- 3 files changed, 315 insertions(+), 7 deletions(-) diff --git a/frontend/src/index.css b/frontend/src/index.css index a348459..333f537 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -930,6 +930,7 @@ h4.sec{ border-radius:var(--r-lg);padding:14px; background:var(--surface);box-shadow:inset 0 0 0 1.5px var(--acc-line); } +.workout-swipe-surface{touch-action:pan-y} /* A superset nests a card inside the page's own gutter, so its inset is paid twice. On a narrow phone that left the weight and reps steppers too tight to show a three-digit number — reported as "the reps textbox becomes too small when superset on mobile". The diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index d0a6279..393c659 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -17,6 +17,10 @@ import { nextPrescription, applyPrescription, defaultIncrement } from '../lib/pr import { glyphOf } from '../lib/glyphs.js' import { isWarmupRow, isDropSet, isRestPauseSet, dropsOf, clustersOf, addDrop, addCluster, removeDropAt, removeClusterAt, setDropAt, setClusterAt, nextDropWeight, nextBurstReps } from '../lib/workout-model.js' +const SWIPE_MIN_DISTANCE = 48 +const SWIPE_AXIS_RATIO = 1.25 +const SWIPE_IGNORED_TARGETS = 'button,input,textarea,select,a,[role="button"],[role="checkbox"],[role="switch"],[role="slider"],[contenteditable="true"],.exmedia,[data-swipe-ignore]' + /* ---------- start chooser (no active workout) ---------- */ function StartChooser() { const nav = useNavigate() @@ -278,13 +282,16 @@ function ActiveWorkout() { const { startRest, stopRest, work } = useUI() const A = S.active const units = supersetUnits(A.entries) - const cur = Math.min(A.cur, Math.max(0, A.entries.length - 1)) + const cur = Number.isInteger(A.cur) + ? Math.min(Math.max(A.cur, 0), Math.max(0, A.entries.length - 1)) + : 0 const unit = A.entries.length ? unitOf(units, cur) : [] const unitIdx = units.findIndex(u => u === unit) const isSuperset = unit.length > 1 // Superset flow: keep the active exercise in view - completing a set scrolls to the // next exercise in the group, then back up to the first exercise of the next round. const exRefs = useRef({}) + const swipe = useRef(null) const progressHighWater = useRef(A.entries.map(e => e.sets.filter(s => s.done).length)) // The marks are index-keyed, and removing an exercise shifts every index above it down // (removeActiveExercise splices). Re-baseline whenever the list length changes, otherwise a @@ -335,6 +342,38 @@ function ActiveWorkout() { const onPairPrev = !isSuperset && cur > 0 ? () => pairAt(cur - 1, cur) : null const onPairNext = !isSuperset && cur < A.entries.length - 1 ? () => pairAt(cur, cur + 1) : null + const navigateUnit = direction => { + const targetFor = active => { + if (!active || !Array.isArray(active.entries) || !Number.isInteger(active.cur)) return null + if (active.cur < 0 || active.cur >= active.entries.length) return null + const freshUnits = supersetUnits(active.entries) + const freshUnitIdx = freshUnits.findIndex(candidate => candidate.includes(active.cur)) + return freshUnitIdx < 0 ? null : freshUnits[freshUnitIdx + direction]?.[0] ?? null + } + if (targetFor(useStore.getState().S.active) == null) return + update(s => { + const target = targetFor(s.active) + if (target != null) s.active.cur = target + }) + } + const onSwipePointerDown = event => { + if (swipe.current || (event.pointerType && event.pointerType !== 'touch' && event.pointerType !== 'pen')) return + if (event.target.closest?.(SWIPE_IGNORED_TARGETS)) return + swipe.current = { id: event.pointerId, x: event.clientX, y: event.clientY } + event.currentTarget.setPointerCapture?.(event.pointerId) + } + const finishSwipe = (event, navigate) => { + const start = swipe.current + if (!start || start.id !== event.pointerId) return + swipe.current = null + event.currentTarget.releasePointerCapture?.(event.pointerId) + if (!navigate) return + const dx = event.clientX - start.x + const dy = event.clientY - start.y + if (Math.abs(dx) < SWIPE_MIN_DISTANCE || Math.abs(dx) < Math.abs(dy) * SWIPE_AXIS_RATIO) return + navigateUnit(dx < 0 ? 1 : -1) + } + // Remove a whole exercise from the session. The confirmation always asks first; in a // superset it asks WHICH exercise of the group to remove. const removeExercise = removeActiveExercise @@ -487,6 +526,13 @@ function ActiveWorkout() { {A.entries.length ? <>
{isSuperset ? t('Superset {0} / {1}', unitIdx + 1, units.length) : t('Exercise {0} / {1}', unitIdx + 1, units.length)}
+
finishSwipe(event, true)} + onPointerCancel={event => finishSwipe(event, false)} + onLostPointerCapture={event => { + if (swipe.current?.id === event.pointerId) swipe.current = null + }}> {isSuperset ? (
@@ -502,12 +548,13 @@ function ActiveWorkout() { ) : ( toggle(cur, i)} onField={(i, f, v) => setField(cur, i, f, v)} onAddSet={() => addSet(cur)} onRemoveSet={() => removeSet(cur)} onAddWarmup={() => addWarmup(cur)} onRemoveSetAt={i => removeSetAt(cur, i)} onStartTimed={i => startTimed(cur, i)} onPairPrev={onPairPrev} onPairNext={onPairNext} /> )} +
:
{t('Freestyle workout — add your first exercise.')}
}
- - + +