From e23609f73014db358e9156ef5399478eb78155b7 Mon Sep 17 00:00:00 2001 From: David Donges <david.donges@mni.thm.de> Date: Tue, 6 Mar 2018 10:58:29 +0100 Subject: [PATCH] Implement NotificationService Added NotificationService displaying messages using the MatSnackBar. --- src/app/app.module.ts | 3 ++- src/app/notification.service.spec.ts | 15 +++++++++++++++ src/app/notification.service.ts | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/app/notification.service.spec.ts create mode 100644 src/app/notification.service.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 94caa4028..0cce70a2c 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -44,6 +44,7 @@ import { MatToolbarModule, MatTooltipModule } from '@angular/material'; +import { NotificationService } from './notification.service'; @NgModule({ declarations: [ @@ -90,7 +91,7 @@ import { MatToolbarModule, MatTooltipModule ], - providers: [], + providers: [NotificationService], bootstrap: [AppComponent] }) export class AppModule { diff --git a/src/app/notification.service.spec.ts b/src/app/notification.service.spec.ts new file mode 100644 index 000000000..44bc1ec88 --- /dev/null +++ b/src/app/notification.service.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { NotificationService } from './notification.service'; + +describe('NotificationService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [NotificationService] + }); + }); + + it('should be created', inject([NotificationService], (service: NotificationService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/src/app/notification.service.ts b/src/app/notification.service.ts new file mode 100644 index 000000000..ed66238e0 --- /dev/null +++ b/src/app/notification.service.ts @@ -0,0 +1,17 @@ +import { Injectable } from '@angular/core'; +import { MatSnackBar, MatSnackBarConfig } from '@angular/material'; + +@Injectable() +export class NotificationService { + private defaultConfig = { + duration: 2000 + }; + + constructor(private snackBar: MatSnackBar) { + } + + show(message: string, config?: MatSnackBarConfig) { + // Delegate the message and merge the (optionally) passed config with the default config + this.snackBar.open(message, '', Object.assign({}, this.defaultConfig, config)); + } +} -- GitLab