All files / src/providers/prompt-service prompt-service.ts

96.43% Statements 27/28
100% Branches 14/14
85.71% Functions 6/7
96% Lines 24/25
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          1x         40x 40x     5x 3x     3x       3x     1x 3x 1x       8x 8x 8x 8x           8x 1x         8x                   8x 3x 3x 1x       8x         1x     1x  
import { Loading, LoadingController, Toast, ToastController } from 'ionic-angular';
import { Injectable } from '@angular/core';
import { GlobalService } from '../global-service/global-service';
 
@Injectable()
export class PromptService {
 
    private loader: Loading;
    private messageToast: Toast;
 
    constructor ( private loadingCtrl: LoadingController,
                 private toastCtrl: ToastController ) {
    }
 
    public presentLoading ( forRadio = false ) {
        const message = GlobalService.getRandomMessageIn(
            forRadio ? GlobalService.loadingMsgRadio : GlobalService.loadingMsgPosts
        );
        this.loader = this.loadingCtrl.create( {
            content: message,
            spinner: 'dots'
        } );
        this.loader.present().catch( this.onError );
    }
 
    public dismissLoading () {
        if ( this.loader ) {
            this.loader.dismiss().catch( this.onError );
        }
    }
 
    public presentMessage ( { message,
                               classNameCss,
                               duration,
                               callback
    }: { message: string,
        classNameCss?: string,
        duration?: number,
        callback?: () => void } ) {
 
        if ( this.messageToast ) {
            this.messageToast
                .dismiss()
                .catch( this.onError );
        }
 
        this.messageToast = this.toastCtrl.create( {
            closeButtonText: 'x',
            cssClass: classNameCss || '',
            dismissOnPageChange: true,
            duration: duration || 5000,
            message,
            position: 'bottom',
            showCloseButton: true
        } );
 
        this.messageToast.onDidDismiss( () => {
            console.log( 'Dismissed toast' );
            if ( callback ) {
                callback();
            }
        } );
 
        this.messageToast
            .present()
            .catch( this.onError );
    }
 
    private onError( reason ) {
        console.log( `[PromptService] Error: ${ reason }` );
    }
}