diff --git a/apps/buffgym/.eslintrc.json b/apps/buffgym/.eslintrc.json index c91a72544..087c94da6 100644 --- a/apps/buffgym/.eslintrc.json +++ b/apps/buffgym/.eslintrc.json @@ -15,7 +15,8 @@ "rules": { "indent": [ "error", - 2 + 2, + { "SwitchCase": 1 } ], "linebreak-style": [ "error", diff --git a/apps/buffgym/buffgym-exercise.js b/apps/buffgym/buffgym-exercise.js index ea20aa132..103ff99b7 100644 --- a/apps/buffgym/buffgym-exercise.js +++ b/apps/buffgym/buffgym-exercise.js @@ -1,153 +1,153 @@ -exports = class Exercise { - constructor(params) { - this.completed = false; - 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._restTimeout = null; - this._restInterval = null; - this._state = null; - } - - get humanTitle() { - return `${this.title} ${this.weight}${this.unit}`; - } - - get subTitle() { - const totalSets = this.sets.length; - const uncompletedSets = this.sets.filter((set) => !set.isCompleted()).length; - const currentSet = (totalSets - uncompletedSets) + 1; - return `Set ${currentSet} of ${totalSets}`; - } - - decRestPeriod() { - this.restPeriod--; - } - - addSet(set) { - this.sets.push(set); - } - - currentSet() { - return this.sets.filter(set => !set.isCompleted())[0]; - } - - isLastSet() { - return this.sets.filter(set => !set.isCompleted()).length === 1; - } - - isCompleted() { - return !!this.completed; - } - - canSetCompleted() { - return this.sets.filter(set => set.isCompleted()).length === this.sets.length; - } - - setCompleted() { - if (!this.canSetCompleted()) throw "All sets must be completed"; - if (this.canProgress()) this.weight += this.weightIncrement; - this.completed = true; - } - - canProgress() { - let completedRepsTotalSum = 0; - let targetRepsTotalSum = 0; - this.sets.forEach(set => completedRepsTotalSum += set.reps); - this.sets.forEach(set => targetRepsTotalSum += set.maxReps); - - return (targetRepsTotalSum - completedRepsTotalSum) === 0; - } - - startRestTimer(workout) { - this._restTimeout = setTimeout(() => { - this.next(workout); - }, 1000 * this.restPeriod); - - this._restInterval = setInterval(() => { - this.decRestPeriod(); - - if (this.restPeriod < 0) { - this.resetRestTimer(); - this.next(); - - return; - } - - workout.emit("redraw"); - }, 1000 ); - } - - resetRestTimer() { - clearTimeout(this._restTimeout); - clearInterval(this._restInterval); - this._restTimeout = null; - this._restInterval = null; - this.restPeriod = this._originalRestPeriod; - } - - isRestTimerRunning() { - return this._restTimeout != null; - } - - setupStartedButtons(workout) { - clearWatch(); - - setWatch(() => { - this.currentSet().incReps(); - workout.emit("redraw"); - }, BTN1, {repeat: true}); - - setWatch(workout.next.bind(workout), BTN2, {repeat: false}); - - setWatch(() => { - this.currentSet().decReps(); - workout.emit("redraw"); - }, BTN3, {repeat: true}); - } - - setupRestingButtons(workout) { - clearWatch(); - setWatch(workout.next.bind(workout), BTN2, {repeat: false}); - } - - next(workout) { - const STARTED = 1; - const RESTING = 2; - const COMPLETED = 3; - - switch(this._state) { - case null: - this._state = STARTED; - this.setupStartedButtons(workout); - break; - case STARTED: - this._state = RESTING; - this.startRestTimer(workout); - this.setupRestingButtons(workout); - break; - case RESTING: - this.resetRestTimer(); - this.currentSet().setCompleted(); - - if (this.canSetCompleted()) { - this._state = COMPLETED; - this.setCompleted(); - } else { - this._state = null; - } - // As we are changing state and require it to be reprocessed - // invoke the next step of workout - workout.next(); - break; - default: - throw "Exercise: Attempting to move to an unknown state"; - } - - workout.emit("redraw"); - } -} \ No newline at end of file +exports = class Exercise { + constructor(params) { + this.completed = false; + 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._restTimeout = null; + this._restInterval = null; + this._state = null; + } + + get humanTitle() { + return `${this.title} ${this.weight}${this.unit}`; + } + + get subTitle() { + const totalSets = this.sets.length; + const uncompletedSets = this.sets.filter((set) => !set.isCompleted()).length; + const currentSet = (totalSets - uncompletedSets) + 1; + return `Set ${currentSet} of ${totalSets}`; + } + + decRestPeriod() { + this.restPeriod--; + } + + addSet(set) { + this.sets.push(set); + } + + currentSet() { + return this.sets.filter(set => !set.isCompleted())[0]; + } + + isLastSet() { + return this.sets.filter(set => !set.isCompleted()).length === 1; + } + + isCompleted() { + return !!this.completed; + } + + canSetCompleted() { + return this.sets.filter(set => set.isCompleted()).length === this.sets.length; + } + + setCompleted() { + if (!this.canSetCompleted()) throw "All sets must be completed"; + if (this.canProgress()) this.weight += this.weightIncrement; + this.completed = true; + } + + canProgress() { + let completedRepsTotalSum = 0; + let targetRepsTotalSum = 0; + this.sets.forEach(set => completedRepsTotalSum += set.reps); + this.sets.forEach(set => targetRepsTotalSum += set.maxReps); + + return (targetRepsTotalSum - completedRepsTotalSum) === 0; + } + + startRestTimer(workout) { + this._restTimeout = setTimeout(() => { + this.next(workout); + }, 1000 * this.restPeriod); + + this._restInterval = setInterval(() => { + this.decRestPeriod(); + + if (this.restPeriod < 0) { + this.resetRestTimer(); + this.next(); + + return; + } + + workout.emit("redraw"); + }, 1000 ); + } + + resetRestTimer() { + clearTimeout(this._restTimeout); + clearInterval(this._restInterval); + this._restTimeout = null; + this._restInterval = null; + this.restPeriod = this._originalRestPeriod; + } + + isRestTimerRunning() { + return this._restTimeout != null; + } + + setupStartedButtons(workout) { + clearWatch(); + + setWatch(() => { + this.currentSet().incReps(); + workout.emit("redraw"); + }, BTN1, {repeat: true}); + + setWatch(workout.next.bind(workout), BTN2, {repeat: false}); + + setWatch(() => { + this.currentSet().decReps(); + workout.emit("redraw"); + }, BTN3, {repeat: true}); + } + + setupRestingButtons(workout) { + clearWatch(); + setWatch(workout.next.bind(workout), BTN2, {repeat: false}); + } + + next(workout) { + const STARTED = 1; + const RESTING = 2; + const COMPLETED = 3; + + switch(this._state) { + case null: + this._state = STARTED; + this.setupStartedButtons(workout); + break; + case STARTED: + this._state = RESTING; + this.startRestTimer(workout); + this.setupRestingButtons(workout); + break; + case RESTING: + this.resetRestTimer(); + this.currentSet().setCompleted(); + + if (this.canSetCompleted()) { + this._state = COMPLETED; + this.setCompleted(); + } else { + this._state = null; + } + // As we are changing state and require it to be reprocessed + // invoke the next step of workout + workout.next(); + break; + default: + throw "Exercise: Attempting to move to an unknown state"; + } + + workout.emit("redraw"); + } +}; \ No newline at end of file diff --git a/apps/buffgym/buffgym-icon.js b/apps/buffgym/buffgym-icon.js index 523ed35b6..c41e9cd85 100644 --- a/apps/buffgym/buffgym-icon.js +++ b/apps/buffgym/buffgym-icon.js @@ -1 +1 @@ -require("heatshrink").decompress(atob("mEwxH+ACPI5AUSADAtB5vNGFQtBAIfNF95hoF4wwoF5AwmF5BhmXYbAEF/6QbF1QwIF04qB54ADAwIwoF4oRKBoIvsB4gvZ58kkgCDFxoxaF5wuHGDQcMF5IwXDZwLDGDmlDIWlkgJDSwIABCRAwPDQohCFgIABDQIOCFwYABr4RCCQIvQDYguEAAwtFF5owJDZAvHFw4vFOYQvKFAowMBxIvFMQwvPAB4wFUQ4vJGDYvUGC4vNdgyuEGDIsNFwYwGNAgAPExAvMGIdfTIovfTpYvrfRCOkZ44ugF44NGF05gUFyQvKGIoueGKIufGJ4uhG5oupGItfr4vvAAgvlGAQvt/wrEF9oEGF841IF9QGHX0oGIAD8kAAYJOFzwEBBQoMFACA=")) +require("heatshrink").decompress(atob("mEwxH+ACPI5AUSADAtB5vNGFQtBAIfNF95hoF4wwoF5AwmF5BhmXYbAEF/6QbF1QwIF04qB54ADAwIwoF4oRKBoIvsB4gvZ58kkgCDFxoxaF5wuHGDQcMF5IwXDZwLDGDmlDIWlkgJDSwIABCRAwPDQohCFgIABDQIOCFwYABr4RCCQIvQDYguEAAwtFF5owJDZAvHFw4vFOYQvKFAowMBxIvFMQwvPAB4wFUQ4vJGDYvUGC4vNdgyuEGDIsNFwYwGNAgAPExAvMGIdfTIovfTpYvrfRCOkZ44ugF44NGF05gUFyQvKGIoueGKIufGJ4uhG5oupGItfr4vvAAgvlGAQvt/wrEF9oEGF841IF9QGHX0oGIAD8kAAYJOFzwEBBQoMFACA=")); diff --git a/apps/buffgym/buffgym-set.js b/apps/buffgym/buffgym-set.js index dc0c05671..efb3c2582 100644 --- a/apps/buffgym/buffgym-set.js +++ b/apps/buffgym/buffgym-set.js @@ -1,28 +1,28 @@ -exports = class Set { - constructor(maxReps) { - this.completed = false; - this.minReps = 0; - this.reps = 0; - this.maxReps = maxReps; - } - - isCompleted() { - return !!this.completed; - } - - setCompleted() { - this.completed = true; - } - - incReps() { - if (this.completed) return; - if (this.reps >= this.maxReps) return; - this.reps++; - } - - decReps() { - if (this.completed) return; - if (this.reps <= this.minReps) return; - this.reps--; - } -} \ No newline at end of file +exports = class Set { + constructor(maxReps) { + this.completed = false; + this.minReps = 0; + this.reps = 0; + this.maxReps = maxReps; + } + + isCompleted() { + return !!this.completed; + } + + setCompleted() { + this.completed = true; + } + + incReps() { + if (this.completed) return; + if (this.reps >= this.maxReps) return; + this.reps++; + } + + decReps() { + if (this.completed) return; + if (this.reps <= this.minReps) return; + this.reps--; + } +}; \ No newline at end of file diff --git a/apps/buffgym/buffgym-workout.js b/apps/buffgym/buffgym-workout.js index 124c27f4b..770ed5ffa 100644 --- a/apps/buffgym/buffgym-workout.js +++ b/apps/buffgym/buffgym-workout.js @@ -1,83 +1,84 @@ -exports = class Workout { - constructor(params) { - this.title = params.title; - this.exercises = []; - this.completed = false; - this.on("redraw", redraw.bind(null, this)); - } - - addExercises(exercises) { - exercises.forEach(exercise => this.exercises.push(exercise)); - } - - currentExercise() { - return this.exercises.filter(exercise => !exercise.isCompleted())[0]; - } - - canComplete() { - return this.exercises.filter(exercise => exercise.isCompleted()).length === this.exercises.length; - } - - setCompleted() { - if (!this.canComplete()) throw "All exercises must be completed"; - this.completed = true; - } - - isCompleted() { - return !!this.completed; - } - - 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() { - return { - title: this.title, - exercises: this.exercises.map(exercise => { - return { - title: exercise.title, - weight: exercise.weight, - weightIncrement: exercise.weightIncrement, - unit: exercise.unit, - sets: exercise.sets.map(set => set.maxReps), - restPeriod: exercise.restPeriod, - }; - }), - }; - } - - // State machine - next() { - if (this.canComplete()) { - this.setCompleted(); - this.emit("redraw"); - return; - } - - // Call current exercise state machine - this.currentExercise().next(this); - } -} \ No newline at end of file +exports = class Workout { + constructor(params) { + this.title = params.title; + this.exercises = []; + this.completed = false; + this.on("redraw", params.redraw.bind(null, this)); + } + + addExercises(exercises) { + exercises.forEach(exercise => this.exercises.push(exercise)); + } + + currentExercise() { + return this.exercises.filter(exercise => !exercise.isCompleted())[0]; + } + + canComplete() { + return this.exercises.filter(exercise => exercise.isCompleted()).length === this.exercises.length; + } + + setCompleted() { + if (!this.canComplete()) throw "All exercises must be completed"; + this.completed = true; + } + + isCompleted() { + return !!this.completed; + } + + static fromJSON(workoutJSON, redraw) { + const Set = require("buffgym-set.js"); + const Exercise = require("buffgym-exercise.js"); + const workout = new this({ + title: workoutJSON.title, + redraw: redraw, + }); + 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() { + return { + title: this.title, + exercises: this.exercises.map(exercise => { + return { + title: exercise.title, + weight: exercise.weight, + weightIncrement: exercise.weightIncrement, + unit: exercise.unit, + sets: exercise.sets.map(set => set.maxReps), + restPeriod: exercise.restPeriod, + }; + }), + }; + } + + // State machine + next() { + if (this.canComplete()) { + this.setCompleted(); + this.emit("redraw"); + return; + } + + // Call current exercise state machine + this.currentExercise().next(this); + } +}; \ No newline at end of file diff --git a/apps/buffgym/buffgym.app.js b/apps/buffgym/buffgym.app.js index fc2be83f9..7ed7db1bd 100755 --- a/apps/buffgym/buffgym.app.js +++ b/apps/buffgym/buffgym.app.js @@ -248,7 +248,7 @@ function getWorkoutIndex() { function buildWorkout(fName) { const Workout = require("buffgym-workout.js"); const workoutJSON = require("Storage").readJSON(fName); - const workout = Workout.fromJSON(workoutJSON); + const workout = Workout.fromJSON(workoutJSON, redraw); return workout; }