How my application can wait an answer?


#1

Hello,
I’m trying to interpret Qr_code with Alert but i would like that when i close the alert, the app go again on the scan interface.
I’m trying with a loop but if i loop this with " this.results = await this.barcode.scan(this.options); " at the beginning of the loop, the app doesn’t show the alert. So i need that the app wait the answer of the alert.
Sorry for my bad english i’m french and i’m not the best in English ^^

import { Component } from '@angular/core';
import { AlertController, NavController } from 'ionic-angular';
import { BarcodeScanner, BarcodeScannerOptions } from '@ionic-native/barcode-scanner';
import { Suite } from '../suite/suite';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  options: BarcodeScannerOptions;
  results: {text: any, format: any};
  letter:any;
  reponse: any;
  passage:number = 0;

  constructor(private barcode: BarcodeScanner, public navCtrl: NavController, private alertCtrl: AlertController) {
  }

  async scanBarcode(){

    this.options = {
      prompt: 'Scan a QR_Code to see a result!'
    }

    this.results = await this.barcode.scan(this.options);

    while(this.results){
      this.passage += 1;
      if(this.results.format == "QR_CODE"){
        let code: number = +this.results.text.slice(0,12);
        let code13: number = +this.results.text[12];
        let codeModule: number = +this.results.text[0];
        if(!isNaN(code) && (code13 == 0 || isNaN(code13)) && this.results.text.length > 11){
          if(codeModule == 1){
              let alert = this.alertCtrl.create({
              title: '<img  src="assets/LH.png" alt="LafargeHolcim" /><div class="bold"><span class="bold">Module N°1</span></div>',
              subTitle: '<span class="bold">Id: </span>'+code+'<div><span class="bold">Description: </span><div> Description Text ... </div></div>',
              buttons: [
                          {
                            text: 'Cancel',
                            role: 'cancel',
                            handler: () => {
                              console.log('Cancel clicked');
                            }
                          },
                          {
                            text: 'Take It',
                            handler: () => {
                              this.reponse = true;
                            }
                          }
                        ],
              cssClass: 'formatError'
            });
            alert.present();
          }
          else if(codeModule == 2){
            let alert = this.alertCtrl.create({
              title: '<img  src="assets/LH.png" alt="LafargeHolcim" /><div class="bold"><span class="bold">Module N°2</span></div>',
              subTitle: '<span class="bold">Id: </span>'+code+'<div><span class="bold">Description: </span><div> Description Text ... </div></div>',
              buttons: [
                          {
                            text: 'Cancel',
                            role: 'cancel',
                            handler: () => {
                              console.log('Cancel clicked');
                            }
                          },
                          {
                            text: 'Take It',
                            handler: () => {
                              this.reponse = true;
                            }
                          }
                        ],
              cssClass: 'formatError'
            });
            alert.present();

          }
          else if(codeModule == 3){
            let alert = this.alertCtrl.create({
              title: '<img  src="assets/LH.png" alt="LafargeHolcim" /><div class="bold"><span class="bold">Module N°3</span></div>',
              subTitle: '<span class="bold">Id: </span>'+code+'<div><span class="bold">Description: </span><div> Description Text ... </div></div>',
               buttons: [
                          {
                            text: 'Cancel',
                            role: 'cancel',
                            handler: () => {
                              console.log('Cancel clicked');
                            }
                          },
                          {
                            text: 'Take It',
                            handler: () => {
                              this.reponse = true;
                            }
                          }
                        ],
              cssClass: 'formatError'
            });
            alert.present();

          }
          else{
            let alert = this.alertCtrl.create({
              title: '<img class="errorIcon" src="assets/Error.png" alt="LafargeHolcim" /><div class="errorTitle">Unknown Error</div>',
              buttons: ['Dismiss'],
              cssClass: 'formatError'
            });
            alert.present();
          }
        }
        else{
          let alert = this.alertCtrl.create({
            title: '<img class="errorIcon" src="assets/Error.png" alt="LafargeHolcim" /><div class="errorTitle">Number Error</div>',
            subTitle: '<span class="orange">Problem occurred: <span class="bold">Bad Number</span></span><div><span class="bold">Result:</span> <div class="res">'+this.results.text+'</div></div>',
            buttons: ['Dismiss'],
            cssClass: 'formatError'
          });
          alert.present();
        }
      }
      else{
        let alert = this.alertCtrl.create({
          title: '<div class="errorTitle">Format Error</div>',
          subTitle: '<span class="bold">Good Format:</span> <span class="green">QR_CODE</span><div><span class="bold">Actual Format: </span><span class="orange">'+this.results.format+'</span></div>',
          buttons: ['Dismiss'],
          cssClass: 'formatError'
        });
        alert.present();
      }
      break;
    }

}



}