@@ -12,6 +12,8 @@ import { ForegroundGeofenceService } from './services/foreground-geofence.servic
1212import { LocationService } from './services/location/location.service' ;
1313import { ThemeService } from './services/theme/theme.service' ;
1414import { WebPushNotificationService } from './services/web-push-notification.service' ;
15+ import { SwUpdate , VersionEvent , VersionReadyEvent } from '@angular/service-worker' ;
16+ import { filter } from 'rxjs' ;
1517
1618@Component ( {
1719 selector : 'app-root' ,
@@ -44,8 +46,10 @@ export class AppComponent {
4446 public locationService : LocationService ,
4547 public authService : AuthService ,
4648 private storage : Storage ,
49+ private update : SwUpdate ,
4750 ) {
4851 this . initializeApp ( ) ;
52+ this . updateClient ( ) ;
4953 }
5054
5155 async initializeApp ( ) {
@@ -80,4 +84,23 @@ export class AppComponent {
8084 ) ;
8185 } ) ;
8286 }
87+
88+ updateClient ( ) {
89+ if ( ! this . update . isEnabled ) {
90+ console . log ( "SwUpdate not enabled" ) ;
91+ return ;
92+ }
93+
94+ if ( this . update . isEnabled ) {
95+ this . update . checkForUpdate ( ) . catch ( ( ) => { } ) ;
96+ }
97+
98+ this . update . versionUpdates . pipe ( filter ( ( e : VersionEvent ) : e is VersionReadyEvent => e . type === 'VERSION_READY' ) )
99+ . subscribe ( async ( event ) => {
100+ console . log ( "sw update available" ) ;
101+ if ( confirm ( "There's an update available, reload the app to apply?" ) ) {
102+ this . update . activateUpdate ( ) . then ( ( ) => location . reload ( ) ) ;
103+ }
104+ } ) ;
105+ }
83106}
0 commit comments