47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
const STORAGE_KEY = 'gym_tracker_v5';
|
|
|
|
export const state = {
|
|
workouts: [],
|
|
exerciseRef: [],
|
|
bodyMetrics: [],
|
|
expandedWorkoutId: null,
|
|
expandedExerciseId: null,
|
|
nextId: 1,
|
|
};
|
|
|
|
export function loadState() {
|
|
const saved = localStorage.getItem(STORAGE_KEY);
|
|
if (saved) {
|
|
try {
|
|
const parsed = JSON.parse(saved);
|
|
Object.assign(state, parsed);
|
|
if (!state.exerciseRef) state.exerciseRef = [];
|
|
if (!state.bodyMetrics) state.bodyMetrics = [];
|
|
} catch (e) {
|
|
console.error('Failed to load state', e);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function saveState() {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
|
|
updateSyncStatus();
|
|
}
|
|
|
|
export function genId() {
|
|
return (state.nextId++).toString();
|
|
}
|
|
|
|
export function updateSyncStatus() {
|
|
const dirty = state.workouts.filter(w => !w.synced);
|
|
const el = document.getElementById('sync-status');
|
|
if (!el) return;
|
|
if (dirty.length === 0) {
|
|
el.textContent = '● Всё синхронизировано';
|
|
el.className = 'sync-status synced';
|
|
} else {
|
|
el.textContent = `● ${dirty.length} не сохран.`;
|
|
el.className = 'sync-status pending';
|
|
}
|
|
}
|