Last active
May 13, 2018 14:51
-
-
Save chandermani/4d9ab6b62a53cd69a23dc294da242c6d to your computer and use it in GitHub Desktop.
Workout History Tracker V1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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