Refactor workout json loading. Fix file name matching bug

master
Paul Cockrell 2020-04-23 11:18:10 +01:00
parent edd1b73bc0
commit f8590bdcf5
3 changed files with 28 additions and 24 deletions

View File

@ -4,10 +4,10 @@ exports = class Exercise {
this.sets = [];
this.title = params.title;
this.weight = params.weight;
this.weightIncrement = params.weightIncrement;
this.unit = params.unit;
this.restPeriod = params.restPeriod;
this._originalRestPeriod = params.restPeriod;
this._weightIncrement = params.weightIncrement;
this._restTimeout = null;
this._restInterval = null;
this._state = null;
@ -50,7 +50,7 @@ exports = class Exercise {
setCompleted() {
if (!this.canSetCompleted()) throw "All sets must be completed";
if (this.canProgress()) this.weight += this._weightIncrement;
if (this.canProgress()) this.weight += this.weightIncrement;
this.completed = true;
}

View File

@ -27,8 +27,30 @@ exports = class Workout {
return !!this.completed;
}
static fromJSON(workout) {
static fromJSON(workoutJSON) {
const Set = require("buffgym-set.js");
const Exercise = require("buffgym-exercise.js");
const workout = new this({
title: workoutJSON.title,
});
const exercises = workoutJSON.exercises.map(exerciseJSON => {
const exercise = new Exercise({
title: exerciseJSON.title,
weight: exerciseJSON.weight,
weightIncrement: exerciseJSON.weightIncrement,
unit: exerciseJSON.unit,
restPeriod: exerciseJSON.restPeriod,
});
exerciseJSON.sets.forEach(setJSON => {
exercise.addSet(new Set(setJSON));
});
return exercise;
});
workout.addExercises(exercises);
return workout;
}
toJSON() {
@ -38,6 +60,7 @@ exports = class Workout {
return {
title: exercise.title,
weight: exercise.weight,
weightIncrement: exercise.weightIncrement,
unit: exercise.unit,
sets: exercise.sets.map(set => set.maxReps),
restPeriod: exercise.restPeriod,

View File

@ -246,34 +246,15 @@ function getWorkoutIndex() {
}
function buildWorkout(fName) {
const Set = require("buffgym-set.js");
const Exercise = require("buffgym-exercise.js");
const Workout = require("buffgym-workout.js");
const workoutJSON = require("Storage").readJSON(fName);
const workout = new Workout({
title: workoutJSON.title,
});
const exercises = workoutJSON.exercises.map(exerciseJSON => {
const exercise = new Exercise({
title: exerciseJSON.title,
weight: exerciseJSON.weight,
weightIncrement: exerciseJSON.weightIncrement,
unit: exerciseJSON.unit,
restPeriod: exerciseJSON.restPeriod,
});
exerciseJSON.sets.forEach(setJSON => {
exercise.addSet(new Set(setJSON));
});
return exercise;
});
workout.addExercises(exercises);
const workout = Workout.fromJSON(workoutJSON);
return workout;
}
function saveWorkout(workout) {
const fName = getWorkoutIndex().find(workout => workout.title === workout.title).file;
const fName = getWorkoutIndex().find(w => w.title === workout.title).file;
require("Storage").writeJSON(fName, workout.toJSON());
}