From 37600fabce1b3e38465c9fb93653e12d856f0289 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Thu, 23 Feb 2023 16:25:35 +0100
Subject: [PATCH] refactor: service worker: display confirmation dialog when
 application update is ready

refs #604
---
 .../services/service-worker-update.service.ts | 25 ++++++++++++++-----
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/app/services/service-worker-update.service.ts b/src/app/services/service-worker-update.service.ts
index 47b47c9ed..95b3cdd66 100644
--- a/src/app/services/service-worker-update.service.ts
+++ b/src/app/services/service-worker-update.service.ts
@@ -1,27 +1,29 @@
 import { Injectable } from "@angular/core";
-import { SwUpdate, VersionEvent } from '@angular/service-worker';
+import { SwUpdate } from '@angular/service-worker';
 import { I18nService } from "./internationalisation.service";
 import { NotificationsService } from "./notifications.service";
+import { UserConfirmationService } from "./user-confirmation.service";
 
 @Injectable()
 export class ServiceWorkerUpdateService {
     constructor(
         private swUpdate: SwUpdate,
         private notificationService: NotificationsService,
-        private i18nService: I18nService
+        private i18nService: I18nService,
+        private userConfirmationService: UserConfirmationService
     ) {
         swUpdate.versionUpdates.subscribe(evt => {
             switch (evt.type) {
                 case 'VERSION_DETECTED':
                     let ver = (evt as any).version?.appData?.version ?? "<NA>";
-                    console.log("SwUpdate VERSION_DETECTED", ver);
+                    console.log("ServiceWorkerUpdateService: VERSION_DETECTED", ver);
                     notificationService.notify(i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_DETECTED", { "ver": ver }), 10000);
                     break;
 
                 case 'VERSION_READY':
                     const currVer = (evt as any).currentVersion?.appData?.version ?? "<NA>";
                     const newVer = (evt as any).latestVersion?.appData?.version ?? "<NA>";
-                    console.log("SwUpdate VERSION_READY", currVer, newVer);
+                    console.log("ServiceWorkerUpdateService: VERSION_READY", currVer, "->", newVer);
 
                     notificationService.notify(i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_READY", { "ver": newVer }), 10000);
                     // PLANTE si on stocke le message dans une variable !!!!
@@ -29,17 +31,28 @@ export class ServiceWorkerUpdateService {
                     // notificationService.notify(msg, 10000);
                     // -> ReferenceError: can't access lexical declaration 'xxx' before initialization
                     // avec xxx qui varie d'une fois à l'autre !!!
+
+                    userConfirmationService.askUserConfirmation("Confirmation",
+                        i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_READY", { "ver": newVer })).then(data => {
+                            if (data["confirm"]) {
+                                console.log("ServiceWorkerUpdateService: application update confirmed");
+                                window.location.reload();
+                            }
+                            else {
+                                console.log("ServiceWorkerUpdateService: application update canceled");
+                            }
+                        });
                     break;
 
                 case 'VERSION_INSTALLATION_FAILED':
                     ver = (evt as any).version?.appData?.version ?? "NA";
-                    console.log("SwUpdate VERSION_INSTALLATION_FAILED", ver);
+                    console.log("ServiceWorkerUpdateService: VERSION_INSTALLATION_FAILED", ver);
                     notificationService.notify(i18nService.localizeText("ERROR_SERVICE_WORKER_INSTALL_FAILED", { "ver": ver }), 10000);
                     break;
             }
         });
         swUpdate.unrecoverable.subscribe(event => {
-            console.log("SwUpdate.unrecoverable", event.reason, event.type);
+            console.log("SwUpdate.unrecoverable reason", event.reason, "type", event.type);
             notificationService.notify("SwUpdate: unrecoverable state. Reason=" + event.reason + ", type=" + event.type, 10000);
         });
     }
-- 
GitLab