chore: save current changes
This commit is contained in:
@@ -14,6 +14,7 @@ import { MyCantiService } from '../../services/my-canti.service';
|
||||
import { CantiLettureService } from '../../services/canti-letture.service';
|
||||
import { ComunitaService } from '../../services/comunita.service';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { showFullscreenUpdateOverlay } from '../../app.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-settings',
|
||||
@@ -43,26 +44,6 @@ export class SettingsPage {
|
||||
|
||||
constructor() {}
|
||||
|
||||
private async forceBypassCacheAndCheck(): Promise<boolean> {
|
||||
try {
|
||||
// 1. Forza il browser mobile a controllare la rete per aggiornamenti al Service Worker nativo
|
||||
if ('serviceWorker' in navigator) {
|
||||
const registrations = await navigator.serviceWorker.getRegistrations();
|
||||
for (const registration of registrations) {
|
||||
await registration.update();
|
||||
console.log('[PWA-Update] Native Service Worker updated from settings');
|
||||
}
|
||||
}
|
||||
// 2. Forza il caricamento di ngsw.json bypassando le cache intermedie e locali
|
||||
await fetch(`/ngsw.json?cb=${Date.now()}`, { cache: 'no-store' });
|
||||
await fetch('/ngsw.json', { cache: 'reload' });
|
||||
console.log('[PWA-Update] Caches successfully busted for ngsw.json');
|
||||
} catch (e) {
|
||||
console.warn('[PWA-Update] Failed to bust cache for ngsw.json:', e);
|
||||
}
|
||||
return await this.swUpdate.checkForUpdate();
|
||||
}
|
||||
|
||||
async installApp() {
|
||||
await this.settingsService.installPwa();
|
||||
}
|
||||
@@ -79,6 +60,10 @@ export class SettingsPage {
|
||||
this.cantiLettureService.setSelectedMass(event.detail.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a full data refresh + checks for app updates.
|
||||
* Uses both the Angular SW and the version.json fallback.
|
||||
*/
|
||||
async fullRefresh() {
|
||||
// 1. Refresh JSON data
|
||||
this.cantiService.refresh();
|
||||
@@ -100,40 +85,10 @@ export class SettingsPage {
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Check for Service Worker updates
|
||||
if (this.swUpdate.isEnabled) {
|
||||
try {
|
||||
const updateFound = await this.forceBypassCacheAndCheck();
|
||||
if (updateFound) {
|
||||
const toast = await this.toastCtrl.create({
|
||||
message: 'Nuova versione disponibile! Aggiornamento in corso...',
|
||||
duration: 2000,
|
||||
color: 'secondary'
|
||||
});
|
||||
await toast.present();
|
||||
|
||||
this.swUpdate.versionUpdates
|
||||
.pipe(
|
||||
filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
|
||||
first()
|
||||
)
|
||||
.subscribe(async () => {
|
||||
console.log('[PWA-Update] FullRefresh: version ready, activating...');
|
||||
await this.swUpdate.activateUpdate();
|
||||
window.location.replace(window.location.origin + window.location.pathname + '?update=' + Date.now());
|
||||
});
|
||||
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
await this.swUpdate.activateUpdate();
|
||||
} catch(e) {}
|
||||
window.location.replace(window.location.origin + window.location.pathname + '?update=' + Date.now());
|
||||
}, 6000);
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to check for updates', err);
|
||||
}
|
||||
// 4. Check for app updates (SW + version.json fallback)
|
||||
const updateAvailable = await this.performUpdateCheck();
|
||||
if (updateAvailable) {
|
||||
return; // updateAvailable already triggered the update/reload flow
|
||||
}
|
||||
|
||||
const toast = await this.toastCtrl.create({
|
||||
@@ -162,51 +117,110 @@ export class SettingsPage {
|
||||
});
|
||||
await toastLoading.present();
|
||||
|
||||
const updateAvailable = await this.performUpdateCheck();
|
||||
|
||||
if (!updateAvailable) {
|
||||
const toast = await this.toastCtrl.create({
|
||||
message: 'L\'applicazione è già aggiornata all\'ultima versione.',
|
||||
duration: 3000,
|
||||
color: 'success'
|
||||
});
|
||||
await toast.present();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared update check logic: tries Angular SW first, falls back to version.json.
|
||||
* Returns true if an update was found and the reload flow was initiated.
|
||||
*/
|
||||
private async performUpdateCheck(): Promise<boolean> {
|
||||
try {
|
||||
const updateFound = await this.forceBypassCacheAndCheck();
|
||||
if (updateFound) {
|
||||
const toast = await this.toastCtrl.create({
|
||||
message: 'Nuova versione trovata! Installazione e attivazione in corso...',
|
||||
duration: 3000,
|
||||
color: 'success'
|
||||
});
|
||||
await toast.present();
|
||||
|
||||
// Sottoscrizione per attivare l'aggiornamento appena terminato il download
|
||||
this.swUpdate.versionUpdates
|
||||
.pipe(
|
||||
filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
|
||||
first()
|
||||
)
|
||||
.subscribe(async () => {
|
||||
console.log('[PWA-Update] Manual check: version ready, activating...');
|
||||
await this.swUpdate.activateUpdate();
|
||||
window.location.replace(window.location.origin + window.location.pathname + '?update=' + Date.now());
|
||||
});
|
||||
|
||||
// Timeout di sicurezza per forzare l'attivazione e il ricaricamento se è già scaricato
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
await this.swUpdate.activateUpdate();
|
||||
} catch(e) {}
|
||||
window.location.replace(window.location.origin + window.location.pathname + '?update=' + Date.now());
|
||||
}, 8000);
|
||||
} else {
|
||||
const toast = await this.toastCtrl.create({
|
||||
message: 'L\'applicazione è già aggiornata all\'ultima versione.',
|
||||
duration: 3000,
|
||||
color: 'success'
|
||||
});
|
||||
await toast.present();
|
||||
// Layer 1: Force the browser to re-fetch the SW script
|
||||
if ('serviceWorker' in navigator) {
|
||||
const registration = await navigator.serviceWorker.ready;
|
||||
await registration.update();
|
||||
}
|
||||
|
||||
// Layer 2: Ask Angular SW to check
|
||||
if (this.swUpdate.isEnabled) {
|
||||
const swFoundUpdate = await this.swUpdate.checkForUpdate();
|
||||
if (swFoundUpdate) {
|
||||
await this.applyUpdateAndReload();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Layer 3: Fallback — check version.json
|
||||
const versionMismatch = await this.checkVersionJson();
|
||||
if (versionMismatch) {
|
||||
console.log('[PWA-Update] version.json mismatch detected from settings');
|
||||
await this.applyUpdateAndReload();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (err) {
|
||||
console.error('Check update failed', err);
|
||||
console.error('[PWA-Update] Update check failed from settings:', err);
|
||||
const toast = await this.toastCtrl.create({
|
||||
message: 'Errore durante la ricerca di aggiornamenti.',
|
||||
duration: 3000,
|
||||
color: 'danger'
|
||||
});
|
||||
await toast.present();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async applyUpdateAndReload() {
|
||||
const overlay = showFullscreenUpdateOverlay();
|
||||
|
||||
let activated = false;
|
||||
const activateAndReload = async () => {
|
||||
if (activated) return;
|
||||
activated = true;
|
||||
try {
|
||||
if (this.swUpdate.isEnabled) {
|
||||
await this.swUpdate.activateUpdate();
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[PWA-Update] activateUpdate failed:', e);
|
||||
}
|
||||
overlay.finish();
|
||||
setTimeout(() => {
|
||||
window.location.replace(window.location.origin + window.location.pathname + '?update=' + Date.now());
|
||||
}, 600);
|
||||
};
|
||||
|
||||
// Listen for VERSION_READY + activate + reload
|
||||
if (this.swUpdate.isEnabled) {
|
||||
this.swUpdate.versionUpdates
|
||||
.pipe(
|
||||
filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
|
||||
first()
|
||||
)
|
||||
.subscribe(() => {
|
||||
console.log('[PWA-Update] Settings: version ready, activating...');
|
||||
activateAndReload();
|
||||
});
|
||||
}
|
||||
|
||||
// Safety timeout: reload after 6s regardless
|
||||
setTimeout(() => {
|
||||
console.log('[PWA-Update] Settings: safety timeout reached, activating...');
|
||||
activateAndReload();
|
||||
}, 6000);
|
||||
}
|
||||
|
||||
private async checkVersionJson(): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(`/version.json?cb=${Date.now()}`, { cache: 'no-store' });
|
||||
if (!response.ok) return false;
|
||||
const data = await response.json();
|
||||
console.log(`[PWA-Update] Settings version check: local=${VERSION}, remote=${data.version}`);
|
||||
return data.version !== VERSION;
|
||||
} catch (err) {
|
||||
console.warn('[PWA-Update] version.json check failed:', err);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user