Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
Added PWA update service (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarivera authored Mar 6, 2022
1 parent 47f3bd8 commit 746eaeb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HttpClientModule } from '@angular/common/http';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { TestBed, waitForAsync } from '@angular/core/testing';
import { ServiceWorkerModule } from '@angular/service-worker';

import { AppComponent } from './app.component';

Expand All @@ -11,7 +12,10 @@ describe('AppComponent', () => {
TestBed.configureTestingModule({
declarations: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [HttpClientTestingModule],
imports: [
HttpClientTestingModule,
ServiceWorkerModule.register('ngsw-worker.js', { enabled: false }),
],
}).compileComponents();
}),
);
Expand Down
7 changes: 6 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Component } from '@angular/core';
import { AuthService } from './services/auth.service';
import { PWAUpdateService } from './services/pwa/pwa-update.service';

@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss'],
})
export class AppComponent {
constructor(private authService: AuthService) {
constructor(
private authService: AuthService,
private pwaUpdateService: PWAUpdateService,
) {
this.pwaUpdateService.checkForUpdates();
this.authService.verify();
}
}
22 changes: 22 additions & 0 deletions src/app/services/pwa/pwa-update.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { ServiceWorkerModule } from '@angular/service-worker';

import { PWAUpdateService } from './pwa-update.service';

describe('PWAUpdateService', () => {
let service: PWAUpdateService;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
ServiceWorkerModule.register('ngsw-worker.js', { enabled: false }),
],
});
service = TestBed.inject(PWAUpdateService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
48 changes: 48 additions & 0 deletions src/app/services/pwa/pwa-update.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Injectable } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
import { AlertController } from '@ionic/angular';
import { interval } from 'rxjs';

@Injectable({
providedIn: 'root',
})
export class PWAUpdateService {
constructor(
private swUpdate: SwUpdate,
private alertController: AlertController,
) {}

public checkForUpdates(): void {
if (this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe((event) => this.promptUser());

interval(60 * 60 * 1000).subscribe(async () => {
await this.swUpdate.checkForUpdate();
});
}
}

private promptUser(): void {
this.swUpdate.activateUpdate().then(async () => {
await this.alertController.create({
header: 'May panibagong update ang app',
subHeader: '',
backdropDismiss: false,
message:
'Kailangan i-refresh ang app para makuha ang panibagong updates',
buttons: [
{
text: 'Update',
handler: () => {
this.installUpdate();
},
},
],
});
});
}

private installUpdate(): void {
window.location.reload();
}
}

0 comments on commit 746eaeb

Please sign in to comment.