Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Rotation: one How you train switch for Plan and Settings (chooseRotation, chooseFixedWeek)

Duarte Santos4a589cc26c22
diff --git a/frontend/src/lib/rotation.js b/frontend/src/lib/rotation.js
index d8a53fa23..0bca1f10a 100644
--- a/frontend/src/lib/rotation.js
+++ b/frontend/src/lib/rotation.js
@@ -112,6 +112,30 @@ export function stopPass(s, today = todayISO()) {
s.queue = null
}
+/**
+ * "How you train: Rotation", the one switch Plan and Settings share. Holds Rotation selected
+ * (S.scheduleMode) and starts a pass from the saved sequence when there is one to start. A
+ * malformed queue is not a live pass: Plan's recovery is the honest answer there, not a new pass
+ * written over data another client may still fix. Returns true when a pass started.
+ */
+export function chooseRotation(s, today = todayISO(), now = Date.now()) {
+ s.scheduleMode = 'rotation'
+ if (queueRecovery(s) || !rotationIds(s).length) return false
+ return startPass(s, today, now)
+}
+
+/**
+ * "How you train: Fixed week". Stops this app's own pass and keeps the saved sequence for a
+ * later start; the weekday plan was never touched. A planner's queue (no matching rotationId)
+ * is never this app's to drop, so only the mode flag moves for it.
+ */
+export function chooseFixedWeek(s, today = todayISO()) {
+ const q = queueOf(s)
+ const external = !!q && (!s.rotation || q.rotationId !== s.rotation.id)
+ if (!external) stopPass(s, today)
+ s.scheduleMode = 'week'
+}
+
/** The latest day a workout could have credited the current pass, or null. */
const lastCreditDay = s => {
const q = queueOf(s)
diff --git a/frontend/src/lib/rotation.test.js b/frontend/src/lib/rotation.test.js
index 96dc06655..60fe6fa75 100644
--- a/frontend/src/lib/rotation.test.js
+++ b/frontend/src/lib/rotation.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
-import { scheduleModeOf, queueRecovery, rotationIds, startPass, saveRotation, startNewPass, stopPass, refillAfter } from './rotation.js'
+import { scheduleModeOf, queueRecovery, rotationIds, startPass, saveRotation, startNewPass, stopPass, refillAfter, chooseRotation, chooseFixedWeek } from './rotation.js'
import { queueNext, queueRemaining, queueView } from './queue.js'
import { effectiveRoutineIds } from './history.js'
@@ -302,3 +302,43 @@ describe('the finished-workout boundary', () => {
expect(s.dayPlan).toEqual({})
})
})
+
+describe('the How you train switch Plan and Settings share', () => {
+ const saved = { id: 'r1', sequence: ['a', 'b'], label: 'Rotation' }
+
+ it('Rotation starts a pass from the saved sequence, today, owned by this app', () => {
+ const s = S({ rotation: saved })
+ expect(chooseRotation(s, TODAY, NOW)).toBe(true)
+ expect(s.scheduleMode).toBe('rotation')
+ expect(s.queue).toMatchObject({ ids: ['a', 'b'], startsOn: TODAY, rotationId: 'r1' })
+ })
+
+ it('Rotation with nothing saved only holds the choice', () => {
+ const s = S()
+ expect(chooseRotation(s, TODAY, NOW)).toBe(false)
+ expect(s.scheduleMode).toBe('rotation')
+ expect(s.queue).toBe(null)
+ })
+
+ it('Rotation never writes a pass over a malformed queue', () => {
+ const s = S({ rotation: saved, queue: { ids: ['gone'], since: NOW } })
+ expect(chooseRotation(s, TODAY, NOW)).toBe(false)
+ expect(s.queue.ids).toEqual(['gone'])
+ })
+
+ it('Fixed week stops this app’s pass, keeps the sequence and the weekdays', () => {
+ const s = S({ rotation: saved, queue: pass(['a', 'b'], { rotationId: 'r1' }), week: { 1: ['own'] }, scheduleMode: 'rotation' })
+ chooseFixedWeek(s, TODAY)
+ expect(s.queue).toBe(null)
+ expect(s.scheduleMode).toBe('week')
+ expect(s.rotation).toEqual(saved)
+ expect(s.week).toEqual({ 1: ['own'] })
+ })
+
+ it('Fixed week leaves a planner’s queue alone', () => {
+ const s = S({ queue: pass(['a', 'b']) })
+ chooseFixedWeek(s, TODAY)
+ expect(s.queue.ids).toEqual(['a', 'b'])
+ expect(s.scheduleMode).toBe('week')
+ })
+})
diff --git a/frontend/src/views/Settings.jsx b/frontend/src/views/Settings.jsx
index c5efa904f..e4b75c050 100644
--- a/frontend/src/views/Settings.jsx
+++ b/frontend/src/views/Settings.jsx
@@ -9,7 +9,7 @@ import { ACCENTS, ACCENT_NAMES, todayISO, localTZ, weekStartOf, MONDAY, SUNDAY,
import { inventoryFor, ownsPlates } from '../lib/plates.js'
import { effortOf } from '../lib/history.js'
import { unlock, playOnSilentSupported, vibrateSupported } from '../lib/sound.js'
-import { scheduleModeOf, queueRecovery, rotationIds, startPass, stopPass } from '../lib/rotation.js'
+import { scheduleModeOf, chooseFixedWeek, chooseRotation } from '../lib/rotation.js'
import { queueOf } from '../lib/queue.js'
import { api, webauthnOK, passkeyRegister, IS_ANDROID } from '../lib/api.js'
import { pushSupported, enablePush, disablePush, sendTestPush, syncPushSubscription } from '../lib/push.js'
@@ -91,15 +91,15 @@ export default function Settings() {
title: t('Switch to Fixed Week?'),
message: t('The rotation stops and its current pass is dropped. Your weekday plan is untouched, and the sequence is kept so you can start a new pass later.'),
confirmText: t('Use Fixed Week'),
- onConfirm: () => update(s => { if (!externalQ) stopPass(s); s.scheduleMode = 'week' }),
+ onConfirm: () => update(s => { chooseFixedWeek(s) }),
})
return
}
- update(s => { s.scheduleMode = 'rotation' })
- // A malformed queue is not a live pass: Plan's recovery is the honest answer, not a new pass
- // written over data another client may still fix.
- if (!queueRecovery(S) && rotationIds(S).length) update(s => { startPass(s) })
- else nav('/plan')
+ // The same switch as Plan's "How you train" (lib/rotation.js). With nothing to start (no
+ // saved sequence, or a malformed queue Plan has to sort out) Plan is where to go next.
+ let started = false
+ update(s => { started = chooseRotation(s) })
+ if (!started) nav('/plan')
}
// --- update check state ---