-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkout_timer.js
More file actions
206 lines (178 loc) · 6.22 KB
/
workout_timer.js
File metadata and controls
206 lines (178 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// DOM elements
const elements = {
restInput: document.getElementById('rest'),
intervalInput: document.getElementById('interval'),
setsInput: document.getElementById('sets'),
exerciseNameInput: document.getElementById('exercise-name'),
repsInput: document.getElementById('reps'),
addWorkoutBtn: document.getElementById('add-workout'),
workoutList: document.getElementById('workout-list'),
timerDisplay: document.getElementById('timer-display'),
startBtn: document.getElementById('start'),
pauseBtn: document.getElementById('pause'),
clearWorkoutsBtn: document.getElementById('clear-workouts'),
currentExerciseDisplay: document.getElementById('current-exercise'),
currentSetDisplay: document.getElementById('current-set'),
completionMessage: document.getElementById('completion-message'),
noWorkoutsWarning: document.getElementById('no-workouts-warning'),
workoutStateDisplay: document.getElementById('workout-state'),
};
// Constants
const WORKOUT_STATES = {
WORKOUT: 'Workout',
REST: 'Rest',
COMPLETED: 'All workouts completed.',
};
// App state
let state = {
workouts: [],
currentWorkoutIndex: 0,
currentSet: 1,
isRestPeriod: false,
timer: null,
currentTime: 0,
isPaused: false
};
// Event listeners
elements.addWorkoutBtn.addEventListener('click', addWorkout);
elements.clearWorkoutsBtn.addEventListener('click', clearWorkouts);
elements.startBtn.addEventListener('click', startTimer);
elements.pauseBtn.addEventListener('click', pauseTimer);
function addWorkout() {
const workout = {
exerciseName: elements.exerciseNameInput.value.trim(),
reps: parseInt(elements.repsInput.value),
sets: parseInt(elements.setsInput.value),
interval: parseInt(elements.intervalInput.value),
rest: parseInt(elements.restInput.value),
};
if (isValidWorkout(workout)) {
state.workouts.push(workout);
displayWorkout(workout);
elements.exerciseNameInput.value = '';
}
}
function isValidWorkout(workout) {
return workout.exerciseName && workout.reps && workout.sets && workout.interval && workout.rest;
}
function displayWorkout(workout) {
const li = document.createElement('li');
li.textContent = `${workout.exerciseName} (${workout.reps} reps) x ${workout.sets} sets - ${workout.interval}s interval - ${workout.rest}s rest`;
li.dataset.completed = 'false';
elements.workoutList.appendChild(li);
}
function resetDisplay() {
clearInterval(state.timer);
elements.timerDisplay.textContent = '00:00';
elements.completionMessage.classList.add('d-none');
elements.currentSetDisplay.textContent = '';
elements.currentExerciseDisplay.textContent = '';
elements.workoutStateDisplay.textContent = '';
elements.startBtn.disabled = false;
elements.pauseBtn.disabled = true;
}
function clearWorkouts() {
elements.workoutList.innerHTML = '';
state.workouts = [];
state.currentWorkoutIndex = 0;
state.currentSet = 1;
resetDisplay();
}
function startTimer() {
if (state.workouts.length > 0) {
if (state.isPaused) {
resumeTimer();
} else {
initializeNewWorkout();
}
elements.startBtn.disabled = true;
elements.pauseBtn.disabled = false;
elements.completionMessage.classList.add('d-none');
elements.noWorkoutsWarning.classList.add('d-none');
state.timer = setInterval(timerTick, 1000);
} else {
elements.noWorkoutsWarning.classList.remove('d-none');
}
}
function initializeNewWorkout() {
state.currentWorkoutIndex = 0;
state.currentSet = 1;
state.isRestPeriod = false;
const currentWorkout = state.workouts[state.currentWorkoutIndex];
state.currentTime = currentWorkout.interval;
updateWorkoutState(WORKOUT_STATES.WORKOUT);
}
function resumeTimer() {
updateWorkoutState(state.isRestPeriod ? WORKOUT_STATES.REST : WORKOUT_STATES.WORKOUT);
}
function pauseTimer() {
elements.startBtn.disabled = false;
elements.pauseBtn.disabled = true;
clearInterval(state.timer);
state.isPaused = true;
}
function updateWorkoutState(newState) {
elements.workoutStateDisplay.textContent = newState;
speak(newState);
}
function timerTick() {
state.currentTime--;
if (state.currentTime < 0) {
nextStep();
} else {
updateDisplay();
}
}
function nextStep() {
state.isRestPeriod = !state.isRestPeriod;
const currentWorkout = state.workouts[state.currentWorkoutIndex];
if (state.isRestPeriod) {
handleRestPeriod(currentWorkout);
} else {
handleWorkoutPeriod(currentWorkout);
}
updateDisplay();
}
function handleRestPeriod(currentWorkout) {
updateWorkoutState(WORKOUT_STATES.REST);
if (state.currentSet < currentWorkout.sets) {
state.currentSet++;
} else {
moveToNextWorkout();
}
state.currentTime = currentWorkout.rest;
}
function handleWorkoutPeriod(currentWorkout) {
updateWorkoutState(WORKOUT_STATES.WORKOUT);
state.currentTime = currentWorkout.interval;
}
function moveToNextWorkout() {
state.currentSet = 1;
const currentExercise = elements.workoutList.children[state.currentWorkoutIndex];
currentExercise.classList.add('completed');
currentExercise.dataset.completed = 'true';
state.currentWorkoutIndex++;
if (state.currentWorkoutIndex >= state.workouts.length) {
completeWorkout();
}
}
function completeWorkout() {
resetDisplay();
elements.completionMessage.classList.remove('d-none');
speak(WORKOUT_STATES.COMPLETED);
}
function updateDisplay() {
const currentWorkout = state.workouts[state.currentWorkoutIndex];
elements.timerDisplay.textContent = formatTime(state.currentTime);
elements.currentSetDisplay.textContent = `Current Set: ${state.currentSet}`;
elements.currentExerciseDisplay.textContent = `Current Exercise: ${currentWorkout.exerciseName}`;
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}
function speak(text) {
const utterance = new SpeechSynthesisUtterance(text);
window.speechSynthesis.speak(utterance);
}