All files / modules toasts.ts

100% Statements 29/29
83.33% Branches 10/12
100% Functions 8/8
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 906x 6x 6x   6x                     6x 8x 8x                 8x                     4x 4x 1x     3x 3x           3x 3x 1x               4x 3x 2x 2x     3x 3x                         1x 1x           1x      
import { Injectable } from '@angular/core';
import { LoadingController } from '@ionic/angular';
import { ToastController } from '@ionic/angular';
 
import { languageService } from './language';
 
export type toastType = 'primary' | 'success' | 'warning' | 'danger';
 
/**
 * @class toastService
 * @description A service to handles displaying toast notifications and loading indicators
 */
@Injectable({
    providedIn: 'root'
})
export class toastService {
    private _waiting = false;
    private _loading: any = null;
    
    /**
     * Creates an instance of the `toastService`.
     * 
     * @param lang - The language service for retrieving localized strings.
     * @param toast - The Ionic `ToastController` for displaying toast notifications.
     * @param loadingCtr - The Ionic `LoadingController` for displaying loading indicators.
     */    
    constructor(private lang: languageService, public toast: ToastController, public loadingCtr: LoadingController) {
        // nothing to do
    }
    
    /**
     * Displays a loading indicator with an optional custom message.
     * 
     * @param message - (Optional) The message to display. If not provided, the service will use the localized message for `"@loading_wait"`.
     * @param timeout - (Optional) Duration for how long the loading indicator should be visible (in milliseconds). Defaults to 5000ms.
     * @returns A Promise that resolves once the loading indicator is presented.
     */    
    async ShowWait(message: string | null = null, timeout = 5000){
        if (this._loading){
            return;
        }
        
        this._waiting = true;
        this._loading = await this.loadingCtr.create({
            duration: timeout,
            message: this.lang.tr(message || "@loading_wait"),
            translucent: true
        });        
 
        await this._loading.present();
        if (!this._waiting){
            this.HideWait();
        }
    }
    
    /**
     * Dismisses the currently visible loading indicator after a short delay.
     */    
    HideWait(){
        setTimeout(async () => {
            if (this._loading){
                (document.activeElement as HTMLElement)?.blur();
                await this._loading.dismiss();
            }
 
            this._loading = null;
            this._waiting = false;
        }, 500);
    }
    
    /**
     * Displays a toast notification.
     * 
     * @param type - The type of the toast. It determines the color and style of the toast. 
     *               Accepts `'primary'`, `'success'`, `'warning'`, or `'danger'`.
     * @param text - The message to display in the toast notification.
     * @param duration - (Optional) The duration the toast should be visible (in milliseconds). Defaults to 5000ms.
     * @returns A Promise that resolves once the toast is presented.
     */    
    async ShowAlert(type: toastType, text: string, duration = 5000) {
        const t = await this.toast.create({
            color: type,
            message: text,
            duration: duration
        });
        
        t.present();      
    }
}