Skip to content

Commit 1ffefbf

Browse files
committed
feat: update button
1 parent 1c2b2f9 commit 1ffefbf

File tree

7 files changed

+70
-1
lines changed

7 files changed

+70
-1
lines changed

src/app/components/update-button/update-button.css

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@if (updateFound$ | async) {
2+
<button matIconButton (click)="updateHandler()">
3+
<mat-icon>update</mat-icon>
4+
</button>
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Component, inject } from '@angular/core'
2+
import { AppUpdate } from '../../services/app-update'
3+
import { AsyncPipe } from '@angular/common'
4+
import { MatIconButton } from '@angular/material/button'
5+
import { MatIconModule } from '@angular/material/icon'
6+
7+
@Component({
8+
selector: 'app-update-button',
9+
imports: [AsyncPipe, MatIconModule, MatIconButton],
10+
templateUrl: './update-button.html',
11+
styleUrl: './update-button.css',
12+
})
13+
export class UpdateButton {
14+
private readonly appUpdates = inject(AppUpdate)
15+
16+
updateFound$ = this.appUpdates.updateFound
17+
18+
updateHandler() {
19+
this.appUpdates.reloadApp()
20+
}
21+
}

src/app/pages/welcome-page/welcome-page.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@
1313
margin: 0;
1414
gap: 1rem;
1515
}
16+
17+
.header {
18+
display: flex;
19+
align-items: center;
20+
justify-content: space-between;
21+
}

src/app/pages/welcome-page/welcome-page.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<mat-card>
2-
<mat-card-header>
2+
<mat-card-header class="header">
33
<mat-card-title>
44
<h1>
55
RSS Reader
66
</h1>
77
</mat-card-title>
8+
<app-update-button/>
89
</mat-card-header>
910
<mat-card-content class="content">
1011
<ul class="details">

src/app/pages/welcome-page/welcome-page.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { MatIconModule } from '@angular/material/icon'
1212
import { HealthService } from '../../services/health-service'
1313
import { Subscription } from 'rxjs'
1414
import { environment } from '../../../environments/environment'
15+
import { UpdateButton } from '../../components/update-button/update-button'
1516

1617
@Component({
1718
selector: 'app-welcome-page',
@@ -25,12 +26,14 @@ import { environment } from '../../../environments/environment'
2526
MatIconModule,
2627
MatFabButton,
2728
MatAccordion,
29+
UpdateButton,
2830
],
2931
templateUrl: './welcome-page.html',
3032
styleUrl: './welcome-page.css',
3133
})
3234
export class WelcomePage implements OnInit, OnDestroy {
3335
private readonly healthService = inject(HealthService)
36+
3437
subscription: Subscription[] = []
3538

3639
ngOnInit() {

src/app/services/app-update.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ApplicationRef, inject, Injectable } from '@angular/core'
2+
import { SwUpdate } from '@angular/service-worker'
3+
import { BehaviorSubject, concat, first, interval } from 'rxjs'
4+
5+
@Injectable({
6+
providedIn: 'root',
7+
})
8+
export class AppUpdate {
9+
private appRef = inject(ApplicationRef)
10+
private swu = inject(SwUpdate)
11+
12+
updateFound = new BehaviorSubject<boolean>(false)
13+
14+
constructor() {
15+
const appIsStable$ = this.appRef.isStable.pipe(first((isStable) => isStable))
16+
const schedule$ = interval(6 * 60 * 60 * 1000) // 6h
17+
const scheduledEvent$ = concat(appIsStable$, schedule$)
18+
19+
scheduledEvent$.subscribe(async () => {
20+
try {
21+
console.info(`[${new Date().toLocaleString()}] Checking for updates...`)
22+
const updateFound = await this.swu.checkForUpdate()
23+
this.updateFound.next(updateFound)
24+
} catch (err) {
25+
console.error('Failed to check for updates:', err)
26+
}
27+
})
28+
}
29+
30+
reloadApp() {
31+
document.location.reload()
32+
}
33+
}

0 commit comments

Comments
 (0)