From a5c805cd98b5b2c412526a5d7d5e828681ccabe2 Mon Sep 17 00:00:00 2001 From: 4gray Date: Sun, 29 Jan 2023 00:10:24 +0100 Subject: [PATCH] refactor: update service worker update method --- src/app/services/pwa.service.ts | 38 +++++++++++---------------------- src/assets/i18n/en.json | 4 +++- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/app/services/pwa.service.ts b/src/app/services/pwa.service.ts index 8aefa5cfc..83b6bee2a 100644 --- a/src/app/services/pwa.service.ts +++ b/src/app/services/pwa.service.ts @@ -2,14 +2,12 @@ import { HttpClient } from '@angular/common/http'; import { ApplicationRef, inject, Injectable } from '@angular/core'; import { MatSnackBar } from '@angular/material/snack-bar'; import { SwUpdate } from '@angular/service-worker'; +import { TranslateService } from '@ngx-translate/core'; import { parse } from 'iptv-playlist-parser'; import { NgxIndexedDBService } from 'ngx-indexed-db'; import { catchError, combineLatest, - concat, - first, - interval, map, of, switchMap, @@ -57,6 +55,7 @@ export class PwaService extends DataService { appRef = inject(ApplicationRef); snackBar = inject(MatSnackBar); swUpdate = inject(SwUpdate); + translateService = inject(TranslateService); /** * Creates an instance of PwaService. @@ -75,30 +74,17 @@ export class PwaService extends DataService { * Uses service worker mechanism to check for available application updates */ checkUpdates() { - if (this.swUpdate.isEnabled) { - const appIsStable$ = this.appRef.isStable.pipe( - first((isStable) => isStable === true) - ); - const everySixHours$ = interval(6 * 60 * 60 * 1000); - const everySixHoursOnceAppIsStable$ = concat( - appIsStable$, - everySixHours$ - ); - - everySixHoursOnceAppIsStable$.subscribe(() => { - this.swUpdate.checkForUpdate(); - }); - - this.swUpdate.versionUpdates.subscribe(() => { - const snackBarRef = this.snackBar.open( - 'Update available', - 'Refresh' - ); - snackBarRef.onAction().subscribe(() => { - document.location.reload(); + this.swUpdate.versionUpdates.subscribe(() => { + this.snackBar + .open( + this.translateService.instant('UPDATE_AVAILABLE'), + this.translateService.instant('REFRESH') + ) + .onAction() + .subscribe(() => { + window.location.reload(); }); - }); - } + }); } /** diff --git a/src/assets/i18n/en.json b/src/assets/i18n/en.json index b20538857..8d3be4a19 100644 --- a/src/assets/i18n/en.json +++ b/src/assets/i18n/en.json @@ -149,5 +149,7 @@ "TITLE": "About IPTVnator", "DESCRIPTION": "Cross-platform IPTV player application with multiple features, such as support of m3u and m3u8 playlists, favorites, TV guide, TV archive/catchup and more.", "VERSION": "Version" - } + }, + "UPDATE_AVAILABLE": "Update available", + "REFRESH": "Refresh" }