From 95d07b15c0948bd26b4192f70e1b02ef8804f393 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lukas=20Mau=C3=9F?= <lukas.mauss@mni.thm.de>
Date: Tue, 28 May 2019 18:14:15 +0200
Subject: [PATCH] Add update notification

---
 ngsw-config.json             |  1 +
 src/app/app.component.ts     | 30 +++++++++++++++++++++++++-----
 src/assets/i18n/home/de.json |  4 +++-
 src/assets/i18n/home/en.json |  4 +++-
 4 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/ngsw-config.json b/ngsw-config.json
index ccb8dea0a..be36d5d04 100644
--- a/ngsw-config.json
+++ b/ngsw-config.json
@@ -17,6 +17,7 @@
     {
       "name": "app",
       "installMode": "prefetch",
+      "updateMode": "prefetch",
       "resources": {
         "files": [
           "/favicon.ico",
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 39c8d57d7..c06e749db 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -1,20 +1,40 @@
-import { Component } from '@angular/core';
+import { Component, OnInit } from '@angular/core';
 import { TranslateService } from '@ngx-translate/core';
-import { ThemeService } from '../theme/theme.service';
+import { SwUpdate } from '@angular/service-worker';
+import { NotificationService } from './services/util/notification.service';
 
 @Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.scss']
 })
-export class AppComponent {
+export class AppComponent implements OnInit {
 
   constructor(private translationService: TranslateService,
-              private themeService: ThemeService) {
-
+              private update: SwUpdate,
+              public notification: NotificationService) {
     translationService.setDefaultLang(this.translationService.getBrowserLang());
     sessionStorage.setItem('currentLang', this.translationService.getBrowserLang());
   }
 
   title = 'ARSnova';
+
+  ngOnInit(): void {
+    this.update.available.subscribe(update => {
+      let install: string;
+      this.translationService.get('home-page.install').subscribe(msg => {
+        install = msg;
+      });
+      this.translationService.get('home-page.update-available').subscribe(msg => {
+       this.notification.show(msg, install, {
+          duration: 10000
+        });
+      });
+      this.notification.snackRef.afterDismissed().subscribe(info => {
+        if (info.dismissedByAction === true) {
+          window.location.reload();
+        }
+      });
+    });
+  }
 }
diff --git a/src/assets/i18n/home/de.json b/src/assets/i18n/home/de.json
index a5a5dbfe2..7d5899df5 100644
--- a/src/assets/i18n/home/de.json
+++ b/src/assets/i18n/home/de.json
@@ -29,7 +29,9 @@
 		"no-empty-name": "Bitte geben Sie einen Namen ein",
 		"created-1": "Session '",
 		"created-2": "' erfolgreich erstellt",
-		"only-numbers": "Eine Session-ID besteht aus Ziffern"
+		"only-numbers": "Eine Session-ID besteht aus Ziffern",
+        "update-available": "Eine neue Version ist verfügbar.",
+        "install": "Installieren"
 	},
 	"login": {
 		"email": "E-mail",
diff --git a/src/assets/i18n/home/en.json b/src/assets/i18n/home/en.json
index 8acaf4efc..3691c26bf 100644
--- a/src/assets/i18n/home/en.json
+++ b/src/assets/i18n/home/en.json
@@ -29,7 +29,9 @@
 		"no-empty-name": "Please enter a name.",
 		"created-1": "Session '",
 		"created-2": "' successfully created",
-		"only-numbers": "A session-ID only contains digits"
+		"only-numbers": "A session-ID only contains digits",
+        "update-available": "A newer version is available.",
+        "install": "Install"
 	},
 	"login": {
 		"email": "E-mail",
-- 
GitLab