From f0ceb8d3983bdc6ed33228e0aa6778efa7f22002 Mon Sep 17 00:00:00 2001 From: Isahann Hanacleto Date: Sun, 3 Mar 2024 23:08:22 -0300 Subject: [PATCH] #50 fixing promises --- src/app/main-window/main-window.component.ts | 27 +++++----- .../question-window.component.html | 4 +- .../question-window.component.ts | 49 +++++++++++-------- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/app/main-window/main-window.component.ts b/src/app/main-window/main-window.component.ts index e4aa417..e8f2fe2 100644 --- a/src/app/main-window/main-window.component.ts +++ b/src/app/main-window/main-window.component.ts @@ -34,30 +34,27 @@ export class MainWindowComponent implements OnInit { await this.router.navigateByUrl(route); } - private startCountdown(): void { + private async startCountdown(): Promise { const appStorage: AppStorage = this.appStorageService.retrieveAppStorage(); if (appStorage.lastQuizResponseDate === null) return; const nextResponseMinimumDate: Moment = moment(appStorage.lastQuizResponseDate).add(3, "hours"); - new Promise(async (resolve): Promise => { - while (true) { - const now: Moment = moment(); + while (true) { + const now: Moment = moment(); - if (now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate)) { - this.quizCanBeAnswered = true; - this.appStorageService.clearLastAnsweredDate(); - resolve(); - break; - } + if (now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate)) { + this.quizCanBeAnswered = true; + this.appStorageService.clearLastAnsweredDate(); + break; + } - const timeLeft: Duration = moment.duration(nextResponseMinimumDate.valueOf() - now.valueOf()); + const timeLeft: Duration = moment.duration(nextResponseMinimumDate.valueOf() - now.valueOf()); - this.remainingTime = `${timeLeft.hours()} hours, ${timeLeft.minutes()} minutes, ${timeLeft.seconds()} seconds` + this.remainingTime = `${timeLeft.hours()} hours, ${timeLeft.minutes()} minutes, ${timeLeft.seconds()} seconds` - await new Promise(f => setTimeout(f, 1000)); - } - }); + await new Promise(f => setTimeout(f, 1000)); + } } } diff --git a/src/app/question-window/question-window.component.html b/src/app/question-window/question-window.component.html index 228c080..7cef758 100644 --- a/src/app/question-window/question-window.component.html +++ b/src/app/question-window/question-window.component.html @@ -2,14 +2,14 @@
- +
- +
diff --git a/src/app/question-window/question-window.component.ts b/src/app/question-window/question-window.component.ts index 2d550c6..69c2dec 100644 --- a/src/app/question-window/question-window.component.ts +++ b/src/app/question-window/question-window.component.ts @@ -1,4 +1,4 @@ -import {Component, OnInit} from '@angular/core'; +import {Component, OnDestroy, OnInit} from '@angular/core'; import {TriviaService} from "../../service/trivia.service"; import {TriviaResponse} from "../../model/TriviaResponse"; import {Router} from "@angular/router"; @@ -6,15 +6,17 @@ import {PathsEnum} from "../../model/enums/PathsEnum"; import {AppStorageService} from "../../service/app-storage.service"; import {QuestionResultTemplateParams} from "../../model/Template"; import {EncryptionService} from "../../service/encryption.service"; +import {Subscription} from "rxjs"; @Component({ selector: 'app-question-window', templateUrl: './question-window.component.html', styleUrls: ['./question-window.component.sass'] }) -export class QuestionWindowComponent implements OnInit { +export class QuestionWindowComponent implements OnInit, OnDestroy { public questionLoaded: boolean = false; + public showQuestion: boolean = false; public question: string = ''; public questionPoints: number = 0; public answers: string[] = []; @@ -27,6 +29,7 @@ export class QuestionWindowComponent implements OnInit { private questionReadySound: HTMLAudioElement = new Audio('assets/sounds/logon.wav'); private confirmAnswerSound: HTMLAudioElement = new Audio('assets/sounds/exclamation.wav'); private correctAnswer: string = ''; + private getQuizzesSubscription: Subscription | undefined; constructor( private readonly triviaService: TriviaService, @@ -43,7 +46,11 @@ export class QuestionWindowComponent implements OnInit { } this.startLoadingProgressBar(); - await this.loadQuestion(); + this.loadQuestion(); + } + + public ngOnDestroy() { + this.getQuizzesSubscription?.unsubscribe(); } public async onClickAnswer(selectedAnswer: string) { @@ -57,8 +64,8 @@ export class QuestionWindowComponent implements OnInit { return selectedAnswer ? `> ${answer} <` : answer; } - private async loadQuestion() { - this.triviaService.getQuizzes().subscribe(async (response: TriviaResponse[]): Promise => { + private loadQuestion(): void { + this.getQuizzesSubscription = this.triviaService.getQuizzes().subscribe((response: TriviaResponse[]): void => { const singleQuiz: TriviaResponse = response[0]; this.question = singleQuiz.question.text; @@ -68,13 +75,8 @@ export class QuestionWindowComponent implements OnInit { .sort((a, b) => a.sort - b.sort) .map(({value}) => value); - await new Promise(f => setTimeout(f, 3000)); - - this.questionLoaded = true; - - await this.questionReadySound.play(); - this.questionPoints = this.sumQuestionPoints(singleQuiz.difficulty, singleQuiz.isNiche); + this.questionLoaded = true; }); } @@ -124,19 +126,26 @@ export class QuestionWindowComponent implements OnInit { return questionPoints + nichePoints; } - private startLoadingProgressBar(): void { - new Promise(async (resolve, reject): Promise => { - while (true) { - this.loadingProgressBar += 10; + private async startLoadingProgressBar(): Promise { + let revertProgressBar: boolean = false; - if (this.loadingProgressBar === this.progressBarMax) { - resolve(); + while (true) { + if(this.loadingProgressBar === 100) { + revertProgressBar = true; + + if(this.questionLoaded){ + this.showQuestion = true; + await this.questionReadySound.play(); break; } - - await new Promise(f => setTimeout(f, 300)); + } else if(this.loadingProgressBar === 0) { + revertProgressBar = false; } - }); + + this.loadingProgressBar += revertProgressBar ? -10 : 10; + + await new Promise(f => setTimeout(f, 300)); + } } private async returnHome() {