-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚀 Persist results in local storage
- Loading branch information
Showing
9 changed files
with
105 additions
and
54 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Injectable, OnInit } from '@angular/core'; | ||
import { ResultsService } from './results.service'; | ||
import { Result } from './result.model'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class LocalStorageResultsService implements ResultsService, OnInit { | ||
private static readonly KEY = 'results'; | ||
private results: Result[] = []; | ||
|
||
constructor() {} | ||
|
||
ngOnInit(): void { | ||
this.load(); | ||
} | ||
|
||
public add(result: Result): void { | ||
this.results.push(result); | ||
this.persist(); | ||
} | ||
|
||
public get(): Result[] { | ||
return this.results; | ||
} | ||
|
||
private load(): void { | ||
this.results = | ||
JSON.parse( | ||
localStorage.getItem(LocalStorageResultsService.KEY) ?? '[]', | ||
) ?? []; | ||
} | ||
|
||
private persist(): void { | ||
localStorage.setItem( | ||
LocalStorageResultsService.KEY, | ||
JSON.stringify(this.results), | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export class Result { | ||
constructor( | ||
private readonly instant: Date, | ||
private readonly guess: number, | ||
private readonly actual: number, | ||
private readonly time: number, | ||
) {} | ||
|
||
getInstant(): Date { | ||
return this.instant; | ||
} | ||
|
||
getGuess(): number { | ||
return this.guess; | ||
} | ||
|
||
getActual(): number { | ||
return this.actual; | ||
} | ||
|
||
getError(): number { | ||
return this.guess - this.actual; | ||
} | ||
|
||
getTime(): number { | ||
return this.time; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { Result } from './result.model'; | ||
|
||
export const RESULTS_SERVICE = new InjectionToken<ResultsService>( | ||
'services.results', | ||
); | ||
|
||
export interface ResultsService { | ||
add(result: Result): void; | ||
get(): Result[]; | ||
} |