Created
June 30, 2017 13:10
-
-
Save jvkiran/549be8433749984b2986668ab2b2ea5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
import {ToastyService, ToastyConfig, ToastyComponent, ToastOptions, ToastData} from 'ng2-toasty'; | |
import {Subject, Observable, Subscription} from 'rxjs/Rx'; | |
@Injectable() | |
export class AlertService { | |
getTitle(num: number): string { | |
return 'Countdown: ' + num; | |
} | |
getMessage(num: number): string { | |
return 'Seconds left: ' + num; | |
} | |
constructor(private toastyService:ToastyService) { } | |
addToast() { | |
let interval = 1000; | |
let timeout = 5000; | |
let seconds = timeout / 1000; | |
let subscription: Subscription; | |
let toastOptions: ToastOptions = { | |
title: this.getTitle(seconds), | |
msg: this.getMessage(seconds), | |
showClose: true, | |
timeout: timeout, | |
onAdd: (toast: ToastData) => { | |
console.log('Toast ' + toast.id + ' has been added!'); | |
// Run the timer with 1 second iterval | |
let observable = Observable.interval(interval).take(seconds); | |
// Start listen seconds beat | |
subscription = observable.subscribe((count: number) => { | |
// Update title of toast | |
toast.title = this.getTitle(seconds - count - 1); | |
// Update message of toast | |
toast.msg = this.getMessage(seconds - count - 1); | |
}); | |
}, | |
onRemove: function(toast: ToastData) { | |
console.log('Toast ' + toast.id + ' has been removed!'); | |
// Stop listenning | |
subscription.unsubscribe(); | |
} | |
}; | |
switch (this.options.type) { | |
case 'default': this.toastyService.default(toastOptions); break; | |
case 'info': this.toastyService.info(toastOptions); break; | |
case 'success': this.toastyService.success(toastOptions); break; | |
case 'wait': this.toastyService.wait(toastOptions); break; | |
case 'error': this.toastyService.error(toastOptions); break; | |
case 'warning': this.toastyService.warning(toastOptions); break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment