Files
tusha/src/app/pages/modus/focus/focus.service.ts

41 lines
1.2 KiB
TypeScript

import {FocusStats} from "./focus-stats";
import {FocusState} from "./focus-state";
import {Injectable} from "@angular/core";
import {BehaviorSubject, Observable} from "rxjs";
@Injectable({ providedIn: 'root',})
export class FocusService {
current: {state: FocusState, start: Date} = {state: FocusState.STOPPED, start: new Date()};
private localStats = new FocusStats();
private statsSubject: BehaviorSubject<FocusStats> = new BehaviorSubject<FocusStats>(this.localStats);
$stats: Observable<FocusStats> = this.statsSubject.asObservable();
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.localStats.inc(this.current.state, deltaInMs);
this.statsSubject.next(this.localStats);
} else {
this.localStats.inc(this.current.state);
this.statsSubject.next(this.localStats);
}
}
}