import { Injectable } from '@angular/core'; import { Base64 } from '../base64/base64'; import { LoadingController } from 'ionic-angular'; import { AlertController } from 'ionic-angular'; import { ToastController } from 'ionic-angular'; @Injectable() export class AppUtil { private loadingBar: any; constructor(public alertController: AlertController, public loading: LoadingController, public base64: Base64, public toastController: ToastController) { } public isNull(val) { return val === undefined || val === 'undefined' || val === "null" || val === null; } public isNotNull(val) { return !this.isNull(val); } public isBlank(val) { return val === undefined || val === 'undefined' || val === "null" || val === null || val === ""; } public isNotBlank(val) { return !this.isBlank(val); } public encodeJsonTOBase64(val) { return "data:;base64," + this.base64.encode(JSON.stringify(val)); } public decodeBase64ToJson(val) { return JSON.parse(this.base64.decode((val).replace(/(\r\n|\n|\r)/gm, ""))); } public showLoading() { this.loadingBar = this.loading.create({ content: 'Please Wait...' }); this.loadingBar.present(); } public hideLoading() { this.loadingBar.dismiss(); } public alert(msg, title) { let promise = new Promise((resolve, reject) => { let temp_alert = this.alertController.create({ title: title || 'Alert', subTitle: msg, buttons: [{ text: 'OK', handler: () => { resolve(); } }] }); temp_alert.present(); }); return promise; } public confirm(msg, title) { let promise = new Promise((resolve, reject) => { let confirm = this.alertController.create({ title: title || 'Confirm', message: msg, buttons: [ { text: 'OK', handler: () => resolve() }, { text: 'CANCEL', handler: () => reject() } ] }); confirm.present(); }); return promise; } public popUp(msg, title, ok, cancel) { let promise = new Promise((resolve, reject) => { let confirm = this.alertController.create({ title: title, message: msg, buttons: [ { text: ok, handler: () => resolve() }, { text: cancel, handler: () => reject() } ] }); confirm.present(); }); return promise; }; public toast(message, time, position) { let toast = this.toastController.create({ message: message, duration: time || 3000, position: position || 'bottom' }); toast.present(); } // public isDeviceOnline () { // let isOnline = null; // if (window.cordova) { // document.addEventListener("deviceready", function() { // isOnline = $cordovaNetwork.isOnline(); // }, false); // } else { // isOnline = navigator.onLine; // } // return isOnline; // } // public isDeviceOffline () { // let isOffline = null; // if (window.cordova) { // document.addEventListener("deviceready", function() { // isOffline = $cordovaNetwork.isOffline(); // }, false); // } else { // isOffline = !navigator.onLine; // } // return isOffline; // } // public getNetwork () { // let networkState = null; // document.addEventListener("deviceready", function() { // networkState = $cordovaNetwork.getNetwork(); // }, false); // return networkState; // } }