Opzet businesslogica opzetten focus-stats
This commit is contained in:
66
src/app/pages/modus/focus/focus.service.ts
Normal file
66
src/app/pages/modus/focus/focus.service.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import {FocusStats} from "./focus-stats";
|
||||
import {FocusState} from "./focus-state";
|
||||
import {Injectable} from "@angular/core";
|
||||
|
||||
@Injectable({ providedIn: 'root',})
|
||||
export class FocusService {
|
||||
current: {state: FocusState, start: Date} = {state: FocusState.STOPPED, start: new Date()};
|
||||
stats: FocusStats = new FocusStats();
|
||||
|
||||
private startState(state: FocusState) {
|
||||
this.current.state = state;
|
||||
this.current.start = new Date();
|
||||
}
|
||||
|
||||
startFocus(): void {
|
||||
this.startState(FocusState.FOCUS);
|
||||
}
|
||||
|
||||
startShortBreak(): void {
|
||||
this.startState(FocusState.SHORT_BREAK);
|
||||
}
|
||||
|
||||
startLongBreak(): void {
|
||||
this.startState(FocusState.LONG_BREAK);
|
||||
}
|
||||
|
||||
stopCurrent(forced: boolean) {
|
||||
if (!forced) {
|
||||
const deltaInMs = new Date().getTime() - this.current.start.getTime();
|
||||
this.stats.inc(this.current.state, deltaInMs);
|
||||
} else {
|
||||
this.stats.inc(this.current.state);
|
||||
}
|
||||
}
|
||||
|
||||
private formatDelta(deltaInMs: number) {
|
||||
const totalSeconds = Math.floor(deltaInMs / 1000);
|
||||
const hours = Math.floor(totalSeconds / 3600);
|
||||
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
|
||||
const padZero = (num: number) => num.toString().padStart(2, '0');
|
||||
|
||||
return `${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`;
|
||||
}
|
||||
|
||||
getFocusCycles(): number {
|
||||
return this.stats.focusCycles;
|
||||
}
|
||||
|
||||
getFocusTime(): string {
|
||||
return this.formatDelta(this.stats.focusTimeInMs);
|
||||
}
|
||||
|
||||
getShortBreakCycles(): number {
|
||||
return this.stats.shortBreakCycles;
|
||||
}
|
||||
|
||||
getLongBreakCycles(): number {
|
||||
return this.stats.longBreakCycles;
|
||||
}
|
||||
|
||||
getBreakTime(): string {
|
||||
return this.formatDelta(this.stats.breakTimeInMs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user