1- import { Component , OnInit } from '@angular/core' ;
1+ import { Component , OnDestroy , OnInit } from '@angular/core' ;
22import { TriviaService } from "../../service/trivia.service" ;
33import { TriviaResponse } from "../../model/TriviaResponse" ;
44import { Router } from "@angular/router" ;
55import { PathsEnum } from "../../model/enums/PathsEnum" ;
66import { AppStorageService } from "../../service/app-storage.service" ;
77import { QuestionResultTemplateParams } from "../../model/Template" ;
88import { EncryptionService } from "../../service/encryption.service" ;
9+ import { Subscription } from "rxjs" ;
910
1011@Component ( {
1112 selector : 'app-question-window' ,
1213 templateUrl : './question-window.component.html' ,
1314 styleUrls : [ './question-window.component.sass' ]
1415} )
15- export class QuestionWindowComponent implements OnInit {
16+ export class QuestionWindowComponent implements OnInit , OnDestroy {
1617
1718 public questionLoaded : boolean = false ;
19+ public showQuestion : boolean = false ;
1820 public question : string = '' ;
1921 public questionPoints : number = 0 ;
2022 public answers : string [ ] = [ ] ;
@@ -27,6 +29,7 @@ export class QuestionWindowComponent implements OnInit {
2729 private questionReadySound : HTMLAudioElement = new Audio ( 'assets/sounds/logon.wav' ) ;
2830 private confirmAnswerSound : HTMLAudioElement = new Audio ( 'assets/sounds/exclamation.wav' ) ;
2931 private correctAnswer : string = '' ;
32+ private getQuizzesSubscription : Subscription | undefined ;
3033
3134 constructor (
3235 private readonly triviaService : TriviaService ,
@@ -43,7 +46,11 @@ export class QuestionWindowComponent implements OnInit {
4346 }
4447
4548 this . startLoadingProgressBar ( ) ;
46- await this . loadQuestion ( ) ;
49+ this . loadQuestion ( ) ;
50+ }
51+
52+ public ngOnDestroy ( ) {
53+ this . getQuizzesSubscription ?. unsubscribe ( ) ;
4754 }
4855
4956 public async onClickAnswer ( selectedAnswer : string ) {
@@ -57,8 +64,8 @@ export class QuestionWindowComponent implements OnInit {
5764 return selectedAnswer ? `> ${ answer } <` : answer ;
5865 }
5966
60- private async loadQuestion ( ) {
61- this . triviaService . getQuizzes ( ) . subscribe ( async ( response : TriviaResponse [ ] ) : Promise < void > => {
67+ private loadQuestion ( ) : void {
68+ this . getQuizzesSubscription = this . triviaService . getQuizzes ( ) . subscribe ( ( response : TriviaResponse [ ] ) : void => {
6269 const singleQuiz : TriviaResponse = response [ 0 ] ;
6370
6471 this . question = singleQuiz . question . text ;
@@ -68,13 +75,8 @@ export class QuestionWindowComponent implements OnInit {
6875 . sort ( ( a , b ) => a . sort - b . sort )
6976 . map ( ( { value} ) => value ) ;
7077
71- await new Promise ( f => setTimeout ( f , 3000 ) ) ;
72-
73- this . questionLoaded = true ;
74-
75- await this . questionReadySound . play ( ) ;
76-
7778 this . questionPoints = this . sumQuestionPoints ( singleQuiz . difficulty , singleQuiz . isNiche ) ;
79+ this . questionLoaded = true ;
7880 } ) ;
7981 }
8082
@@ -124,19 +126,26 @@ export class QuestionWindowComponent implements OnInit {
124126 return questionPoints + nichePoints ;
125127 }
126128
127- private startLoadingProgressBar ( ) : void {
128- new Promise < void > ( async ( resolve , reject ) : Promise < void > => {
129- while ( true ) {
130- this . loadingProgressBar += 10 ;
129+ private async startLoadingProgressBar ( ) : Promise < void > {
130+ let revertProgressBar : boolean = false ;
131131
132- if ( this . loadingProgressBar === this . progressBarMax ) {
133- resolve ( ) ;
132+ while ( true ) {
133+ if ( this . loadingProgressBar === 100 ) {
134+ revertProgressBar = true ;
135+
136+ if ( this . questionLoaded ) {
137+ this . showQuestion = true ;
138+ await this . questionReadySound . play ( ) ;
134139 break ;
135140 }
136-
137- await new Promise ( f => setTimeout ( f , 300 ) ) ;
141+ } else if ( this . loadingProgressBar === 0 ) {
142+ revertProgressBar = false ;
138143 }
139- } ) ;
144+
145+ this . loadingProgressBar += revertProgressBar ? - 10 : 10 ;
146+
147+ await new Promise ( f => setTimeout ( f , 300 ) ) ;
148+ }
140149 }
141150
142151 private async returnHome ( ) {
0 commit comments