diff --git a/src/app/correct-answer-window/correct-answer-window.component.ts b/src/app/correct-answer-window/correct-answer-window.component.ts index 6bf2bd6..1f91843 100644 --- a/src/app/correct-answer-window/correct-answer-window.component.ts +++ b/src/app/correct-answer-window/correct-answer-window.component.ts @@ -2,7 +2,7 @@ import {Component, OnInit} from '@angular/core'; import {Router} from "@angular/router"; import {PathsEnum} from "../../model/PathsEnum"; import {StorageService} from "../../service/storage.service"; -import {StorageKeyEnum} from "../../model/StorageKeyEnum"; +import {AppStorage} from "../../model/AppStorage"; @Component({ selector: 'app-correct-answer-window', @@ -31,12 +31,14 @@ export class CorrectAnswerWindowComponent implements OnInit { } private saveCurrentScore(): void { - const currentScore: string | null = this.storageService.get(StorageKeyEnum.CURRENT_SCORE); - let currentScoreNumber: number = (currentScore === null || currentScore === '') ? 0 : parseInt(currentScore); - - currentScoreNumber += this.questionScore; - - this.storageService.save(StorageKeyEnum.CURRENT_SCORE, currentScoreNumber.toString()); - this.storageService.save(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE, new Date().getTime().toString()); + const appStorage: AppStorage = this.storageService.get(); + + this.storageService.save( + { + ...appStorage, + currentScore: appStorage.currentScore + this.questionScore, + lastQuizResponseDate: new Date() + } + ) } } diff --git a/src/app/main-window/main-window.component.ts b/src/app/main-window/main-window.component.ts index b0aff4f..d02f298 100644 --- a/src/app/main-window/main-window.component.ts +++ b/src/app/main-window/main-window.component.ts @@ -1,8 +1,8 @@ import {Component, OnInit} from '@angular/core'; import {StorageService} from "../../service/storage.service"; -import {StorageKeyEnum} from "../../model/StorageKeyEnum"; import {Router} from "@angular/router"; import {PathsEnum} from "../../model/PathsEnum"; +import {AppStorage} from "../../model/AppStorage"; @Component({ selector: 'app-main-window', @@ -23,7 +23,8 @@ export class MainWindowComponent implements OnInit { } public async ngOnInit(): Promise { - const lastQuizResponseDate: string | null = this.storageService.get(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE); + const appStorage: AppStorage = this.storageService.get(); + const lastQuizResponseDate: Date | null = appStorage.lastQuizResponseDate; this.quizCanBeAnswered = this.checkIfQuizCanBeAnswered(lastQuizResponseDate); @@ -31,25 +32,29 @@ export class MainWindowComponent implements OnInit { this.startCountdown(lastQuizResponseDate); } - private checkIfQuizCanBeAnswered(lastQuizResponseDate: string | null): boolean { - if (lastQuizResponseDate === null || lastQuizResponseDate === '') return true; + public async redirectTo(route: PathsEnum): Promise { + await this.router.navigateByUrl(route); + } + + private checkIfQuizCanBeAnswered(lastQuizResponseDate: Date | null): boolean { + if (lastQuizResponseDate === null) return true; const now: Date = new Date(); const lastAnsweredDate: Date = new Date(); const threeHoursInMs: number = 1000 * 60 * 60 * 3; - lastAnsweredDate.setTime(parseInt(lastQuizResponseDate) + threeHoursInMs); + lastAnsweredDate.setTime(new Date(lastQuizResponseDate).getTime() + threeHoursInMs); return now.getTime() >= lastAnsweredDate.getTime(); } - private startCountdown(lastQuizResponseDate: string | null): void { - if (lastQuizResponseDate === null || lastQuizResponseDate === '') return; + private startCountdown(lastQuizResponseDate: Date | null): void { + if (lastQuizResponseDate === null) return; const nextAnswerDate: Date = new Date(); const threeHoursInMs: number = 1000 * 60 * 60 * 3; - nextAnswerDate.setTime(parseInt(lastQuizResponseDate) + threeHoursInMs); + nextAnswerDate.setTime(new Date(lastQuizResponseDate).getTime() + threeHoursInMs); new Promise(async (resolve, reject): Promise => { while (true) { @@ -61,7 +66,7 @@ export class MainWindowComponent implements OnInit { if (sameHour && sameMinute && sameSecond) { this.quizCanBeAnswered = true; - this.storageService.clear(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE); + this.clearLastAnsweredDate(); resolve(); break; } @@ -81,7 +86,14 @@ export class MainWindowComponent implements OnInit { }); } - public async redirectTo(route: PathsEnum): Promise { - await this.router.navigateByUrl(route); + private clearLastAnsweredDate(): void { + const appStorage: AppStorage = this.storageService.get(); + + this.storageService.save( + { + ...appStorage, + lastQuizResponseDate: null + } + ); } } diff --git a/src/app/question-window/question-window.component.ts b/src/app/question-window/question-window.component.ts index 058168d..7fe4e86 100644 --- a/src/app/question-window/question-window.component.ts +++ b/src/app/question-window/question-window.component.ts @@ -3,7 +3,6 @@ import {TriviaService} from "../../service/trivia.service"; import {TriviaResponse} from "../../model/TriviaResponse"; import {Router} from "@angular/router"; import {PathsEnum} from "../../model/PathsEnum"; -import {StorageKeyEnum} from "../../model/StorageKeyEnum"; @Component({ selector: 'app-question-window', diff --git a/src/app/score-window/score-window.component.ts b/src/app/score-window/score-window.component.ts index e7dbc7e..f4bf883 100644 --- a/src/app/score-window/score-window.component.ts +++ b/src/app/score-window/score-window.component.ts @@ -1,8 +1,8 @@ import {Component, OnInit} from '@angular/core'; import {StorageService} from "../../service/storage.service"; import {Router} from "@angular/router"; -import {StorageKeyEnum} from "../../model/StorageKeyEnum"; import {PathsEnum} from "../../model/PathsEnum"; +import {AppStorage} from "../../model/AppStorage"; @Component({ selector: 'app-score-window', @@ -19,9 +19,8 @@ export class ScoreWindowComponent implements OnInit { } public ngOnInit(): void { - const currentScore: string | null = this.storageService.get(StorageKeyEnum.CURRENT_SCORE); - - this.score = (currentScore === null || currentScore === '') ? 0 : parseInt(currentScore); + const appStorage: AppStorage = this.storageService.get(); + this.score = appStorage.currentScore; } public async returnHome(): Promise { diff --git a/src/app/wrong-answer-window/wrong-answer-window.component.ts b/src/app/wrong-answer-window/wrong-answer-window.component.ts index 67065c9..b7445d1 100644 --- a/src/app/wrong-answer-window/wrong-answer-window.component.ts +++ b/src/app/wrong-answer-window/wrong-answer-window.component.ts @@ -2,7 +2,7 @@ import {Component, OnInit} from '@angular/core'; import {ActivatedRoute, Router} from "@angular/router"; import {PathsEnum} from "../../model/PathsEnum"; import {StorageService} from "../../service/storage.service"; -import {StorageKeyEnum} from "../../model/StorageKeyEnum"; +import {AppStorage} from "../../model/AppStorage"; @Component({ selector: 'app-wrong-answer-window', @@ -24,11 +24,20 @@ export class WrongAnswerWindowComponent implements OnInit { public async ngOnInit(): Promise { await this.wrongAnswerSound.play(); + this.updateStorage(); } public async returnHome(): Promise { - this.storageService.save(StorageKeyEnum.LAST_QUIZ_RESPONSE_DATE, new Date().getTime().toString()); await this.router.navigateByUrl(PathsEnum.HOME); } + private updateStorage() { + const appStorage: AppStorage = this.storageService.get(); + + this.storageService.save({ + ...appStorage, + currentScore: appStorage.currentScore, + lastQuizResponseDate: new Date() + }); + } } diff --git a/src/model/AppStorage.ts b/src/model/AppStorage.ts new file mode 100644 index 0000000..1e069c3 --- /dev/null +++ b/src/model/AppStorage.ts @@ -0,0 +1,4 @@ +export interface AppStorage { + currentScore: number, + lastQuizResponseDate: Date | null +} diff --git a/src/model/StorageKeyEnum.ts b/src/model/StorageKeyEnum.ts deleted file mode 100644 index 64e897e..0000000 --- a/src/model/StorageKeyEnum.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum StorageKeyEnum { - LAST_QUIZ_RESPONSE_DATE = 'last_quiz_response_date', - CURRENT_SCORE = 'current_score' -} diff --git a/src/service/storage.service.ts b/src/service/storage.service.ts index 5d17ea1..b042cc7 100644 --- a/src/service/storage.service.ts +++ b/src/service/storage.service.ts @@ -1,6 +1,6 @@ import {Injectable} from '@angular/core'; -import {StorageKeyEnum} from "../model/StorageKeyEnum"; -import { AES, enc } from 'crypto-js'; +import {AES, enc} from 'crypto-js'; +import {AppStorage} from "../model/AppStorage"; @Injectable({ providedIn: 'root' @@ -8,33 +8,37 @@ import { AES, enc } from 'crypto-js'; export class StorageService { private readonly localStorage: Storage; - private readonly KEY: string = 'ENCRYPTION_KEY'; + private readonly storageKey: string = 'app_storage' + private readonly encryptionkey: string = 'ENCRYPTION_KEY_XPQUIZ'; constructor() { this.localStorage = window.localStorage; } - public save(key: StorageKeyEnum, value: string): void { - this.localStorage.setItem(key, this.encrypt(value)); - } - - public get(key: StorageKeyEnum): string | null { - const encryptedItem: string | null = this.localStorage.getItem(key); + public save(value: AppStorage): void { + const stringfiedObject: string = JSON.stringify(value); + const encryptedObject: string = this.encrypt(stringfiedObject); - return encryptedItem === null ? '' : this.decrypt(encryptedItem); + this.localStorage.setItem(this.storageKey, encryptedObject); } - public clear(key: StorageKeyEnum) { - this.localStorage.removeItem(key); + public get(): AppStorage { + const encryptedItem: string | null = this.localStorage.getItem(this.storageKey); + return (encryptedItem === null || encryptedItem === '') ? + { + currentScore: 0, + lastQuizResponseDate: null + } : + JSON.parse(this.decrypt(encryptedItem)); } private encrypt(value: string): string { - const encrypted = AES.encrypt(value, this.KEY); + const encrypted = AES.encrypt(value, this.encryptionkey); return encrypted.toString(); } private decrypt(value: string): string { - const decrypted = AES.decrypt(value, this.KEY); + const decrypted = AES.decrypt(value, this.encryptionkey); return decrypted.toString(enc.Utf8); } }