Skip to content

Instantly share code, notes, and snippets.

@chandermani
Last active May 13, 2018 14:51
Show Gist options
  • Save chandermani/4d9ab6b62a53cd69a23dc294da242c6d to your computer and use it in GitHub Desktop.
Save chandermani/4d9ab6b62a53cd69a23dc294da242c6d to your computer and use it in GitHub Desktop.
Workout History Tracker V1
import { CoreModule } from './core.module';
import { Injectable } from '@angular/core';
import { ExercisePlan } from '../workout-runner/model';
@Injectable({
providedIn: CoreModule
})
export class WorkoutHistoryTrackerService {
private maxHistoryItems = 20; // Tracking last 20 exercises
private currentWorkoutLog: WorkoutLogEntry = null;
private workoutHistory: Array<WorkoutLogEntry> = [];
private workoutTracked: boolean;
constructor() { }
get tracking(): boolean {
return this.workoutTracked;
}
startTracking() {
this.workoutTracked = true;
this.currentWorkoutLog = new WorkoutLogEntry(new Date());
if (this.workoutHistory.length >= this.maxHistoryItems) {
this.workoutHistory.shift();
}
this.workoutHistory.push(this.currentWorkoutLog);
}
exerciseComplete(exercise: ExercisePlan) {
this.currentWorkoutLog.lastExercise = exercise.exercise.title;
++this.currentWorkoutLog.exercisesDone;
}
endTracking(completed: boolean) {
this.currentWorkoutLog.completed = completed;
this.currentWorkoutLog.endedOn = new Date();
this.currentWorkoutLog = null;
this.workoutTracked = false;
}
getHistory(): Array<WorkoutLogEntry> {
return this.workoutHistory;
}
}
export class WorkoutLogEntry {
constructor(
public startedOn: Date,
public completed: boolean = false,
public exercisesDone: number = 0,
public lastExercise?: string,
public endedOn?: Date) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment