From 3abba8f11083d6de65938c5456af60d8ba43ede5 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Fri, 22 Mar 2019 17:12:32 +0100
Subject: [PATCH] =?UTF-8?q?Nouveau=20service=20de=20notifications=20multip?=
 =?UTF-8?q?les;=20l'utilisateur=20est=20averti=20lorsque=20des=20r=C3=A9su?=
 =?UTF-8?q?ltats=20sont=20invalid=C3=A9s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/app/app.module.ts                         |  2 +
 .../services/formulaire/formulaire.service.ts | 13 ++++-
 .../notifications/notifications.service.ts    | 49 +++++++++++++++++++
 src/locale/messages.en.json                   |  1 +
 src/locale/messages.fr.json                   |  1 +
 5 files changed, 64 insertions(+), 2 deletions(-)
 create mode 100644 src/app/services/notifications/notifications.service.ts

diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index a1f172ab0..da7f30bba 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -44,6 +44,7 @@ import { FormulaireService } from "./services/formulaire/formulaire.service";
 import { I18nService } from "./services/internationalisation/internationalisation.service";
 import { HttpService } from "./services/http/http.service";
 import { ApplicationSetupService } from "./services/app-setup/app-setup.service";
+import { NotificationsService } from "./services/notifications/notifications.service";
 
 import { AppComponent } from "./app.component";
 import { NgParamInputComponent } from "./components/ngparam-input/ngparam-input.component";
@@ -191,6 +192,7 @@ const appRoutes: Routes = [
     FormulaireService,
     HttpService,
     I18nService,
+    NotificationsService,
     {
       provide: ErrorStateMatcher,
       useClass: ImmediateErrorStateMatcher
diff --git a/src/app/services/formulaire/formulaire.service.ts b/src/app/services/formulaire/formulaire.service.ts
index 0bbc9aece..5bd1cf92a 100644
--- a/src/app/services/formulaire/formulaire.service.ts
+++ b/src/app/services/formulaire/formulaire.service.ts
@@ -22,6 +22,7 @@ import { FormulaireParallelStructure } from "../../formulaire/definition/concret
 import { NgParameter } from "../../formulaire/ngparam";
 import { FieldsetContainer } from "../..//formulaire/fieldset-container";
 import { ApplicationSetupService } from "../app-setup/app-setup.service";
+import { NotificationsService } from "../notifications/notifications.service";
 
 @Injectable()
 export class FormulaireService extends Observable {
@@ -36,7 +37,9 @@ export class FormulaireService extends Observable {
     constructor(
         private i18nService: I18nService,
         private appSetupService: ApplicationSetupService,
-        private httpService: HttpService
+        private httpService: HttpService,
+        private intlService: I18nService,
+        private notificationsService: NotificationsService
     ) {
         super();
         this._formulaires = [];
@@ -630,7 +633,13 @@ export class FormulaireService extends Observable {
         for (const dn of dependingNubs) {
             if (! visited.includes(dn.uid)) {
                 const form = this.getFormulaireFromNubId(dn.uid);
-                form.resetResults(visited);
+                if (form.hasResults) {
+                    form.resetResults(visited);
+                    this.notificationsService.notify(
+                        this.intlService.localizeText("INFO_SNACKBAR_RESULTS_INVALIDATED") + " " + form.calculatorName,
+                        2000
+                    );
+                }
             }
         }
     }
diff --git a/src/app/services/notifications/notifications.service.ts b/src/app/services/notifications/notifications.service.ts
new file mode 100644
index 000000000..9caf0d7ff
--- /dev/null
+++ b/src/app/services/notifications/notifications.service.ts
@@ -0,0 +1,49 @@
+import { Injectable } from "@angular/core";
+import { MatSnackBar } from "@angular/material";
+
+/**
+ * Displays a notifications queue as consecutive snackbars
+ */
+@Injectable()
+export class NotificationsService {
+
+    /** FIFO queue for notifications */
+    private notifications: any[];
+
+    private isOpen: boolean;
+
+    public constructor(
+        private snackBar: MatSnackBar
+    ) {
+        this.notifications = [];
+        this.isOpen = false;
+    }
+
+    /** Push a notification and display it as soon as possible */
+    public notify(message: string, duration: number, action: string = "OK") {
+        this.notifications.push({
+            message: message,
+            duration: duration,
+            action: action
+        });
+        this.show();
+    }
+
+    /** Show all messages in the FIFO queue one after another */
+    public show() {
+        if (! this.isOpen) {
+            // process next notification
+            if (this.notifications.length > 0) {
+                const notif = this.notifications.shift();
+                this.isOpen = true;
+                const ref = this.snackBar.open(notif.message, notif.action, {
+                    duration: notif.duration
+                });
+                ref.afterDismissed().subscribe(() => {
+                    this.isOpen = false;
+                    this.show();
+                });
+            }
+        }
+    }
+}
diff --git a/src/locale/messages.en.json b/src/locale/messages.en.json
index d265692b8..859d160ba 100644
--- a/src/locale/messages.en.json
+++ b/src/locale/messages.en.json
@@ -204,6 +204,7 @@
     "INFO_SETUP_PRECISION_AFFICHAGE": "Display accuracy",
     "INFO_SETUP_PRECISION_CALCUL": "Computation accuracy",
     "INFO_SETUP_TITLE": "Application setup",
+    "INFO_SNACKBAR_RESULTS_INVALIDATED": "Results invalidated for",
     "INFO_SNACKBAR_SETTINGS_SAVED": "Settings saved on this device",
     "INFO_SNACKBAR_DEFAULT_SETTINGS_RESTORED": "Default settings restored",
     "INFO_THEME_CREDITS": "Credit",
diff --git a/src/locale/messages.fr.json b/src/locale/messages.fr.json
index 5c40ca5d0..350dd11ad 100644
--- a/src/locale/messages.fr.json
+++ b/src/locale/messages.fr.json
@@ -204,6 +204,7 @@
     "INFO_SETUP_PRECISION_AFFICHAGE": "Précision d'affichage",
     "INFO_SETUP_PRECISION_CALCUL": "Précision de calcul",
     "INFO_SETUP_TITLE": "Paramètres de l'application",
+    "INFO_SNACKBAR_RESULTS_INVALIDATED": "Résultats invalidés pour",
     "INFO_SNACKBAR_SETTINGS_SAVED": "Paramètres enregistrés sur cet appareil",
     "INFO_SNACKBAR_DEFAULT_SETTINGS_RESTORED": "Paramètres par défaut restaurés",
     "INFO_THEME_CREDITS": "Crédit",
-- 
GitLab