import { Injectable } from '@angular/core'; import { SlimLoadingBarService } from 'ng2-slim-loading-bar'; import { ToastyService, ToastyConfig, ToastOptions, ToastData } from 'ng2-toasty'; import { Subject } from 'rxjs/Subject'; import { Observable } from 'rxjs/Rx'; import { Subscription } from 'rxjs/Subscription'; @Injectable() export class NotifService{ constructor( private t: ToastyService, private slimLoaderService: SlimLoadingBarService ) {} title: string; message: string; toast(title: string, message: string, type: 'default' | 'info' | 'warning' | 'success' | 'wait' | 'error' = 'default') { this.title = title; this.message = message; let interval = 1000; let timeout = 5000; let seconds = timeout / 1000; let subscription: Subscription; let toastOptions: ToastOptions = { title: this.title, msg: this.message, showClose: true, timeout: timeout, onAdd: (toast: ToastData) => { // Run the timer with 1 second interval // Start listen seconds beat let observable = Observable.interval(interval).take(seconds); subscription = observable.subscribe((count: number) => { // Update toast toast.title = this.title; toast.msg = this.message; }); }, onRemove: function(toast: ToastData) { subscription.unsubscribe(); } }; switch (type) { case 'default': this.t.default(toastOptions); break; case 'info': this.t.info(toastOptions); break; case 'success': this.t.success(toastOptions); break; case 'wait': this.t.wait(toastOptions); break; case 'error': this.t.error(toastOptions); break; case 'warning': this.t.warning(toastOptions); break; } } start() { this.slimLoaderService.start(() => { }); } stop() { this.slimLoaderService.stop(); } reset() { this.slimLoaderService.reset(); } complete() { this.slimLoaderService.complete(); } }