import { Injectable } from '@angular/core'; import { MatSnackBar } from '@angular/material/snack-bar'; import { Subject } from 'rxjs'; export interface NotificationServiceOptions { multiline?: boolean; verticalPosition?: 'top' | 'bottom'; } @Injectable({ // we declare that this service should be created // by the root application injector. providedIn: 'root', }) export class MwNotificationService { afterShow: Subject; constructor(private readonly snackBar: MatSnackBar) { this.afterShow = new Subject(); } clear(): void { this.snackBar.dismiss(); } exception(msg: string): void { this.snackBar.open(msg, undefined, { horizontalPosition: 'center', verticalPosition: 'bottom', panelClass: 'mw-notification__exception', }); this.afterShow.next(true); } error(msg: string, opts?: NotificationServiceOptions): void { this.show(msg, ['mw-notification__error', 'mw-full-width'], opts); } success(msg: string, opts?: NotificationServiceOptions): void { this.show(msg, ['mw-notification__success', 'mw-full-width'], opts); } warning(msg: string, opts?: NotificationServiceOptions): void { this.show(msg, ['mw-notification__warning', 'mw-full-width'], opts); } private show( msg: string, classNames: string[], opts?: NotificationServiceOptions ): void { const localClassNames = [...classNames]; if (opts?.multiline) { localClassNames.push('mw-notification--multiline'); } const show = (localMsg: string): void => { this.snackBar.open(msg, undefined, { horizontalPosition: 'center', verticalPosition: 'top', panelClass: localClassNames, duration: 5000, }); this.afterShow.next(true); }; show(msg); } }