Skip to content

Commit

Permalink
fix: app not automatically updating on iOS (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaSch0212 authored Jun 23, 2024
1 parent ec59020 commit 76d3d4d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/client/src/app/components/app/menu/menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import { MenuModule } from 'primeng/menu';
import { MenubarModule } from 'primeng/menubar';
import { OverlayPanelModule } from 'primeng/overlaypanel';
import { TooltipModule } from 'primeng/tooltip';
import { filter, fromEvent, map, merge } from 'rxjs';
import { filter, map, merge } from 'rxjs';

import { selectAppTitle } from '../../../+state/app';
import { AuthService } from '../../../services/auth.service';
import { RealtimeEventsService } from '../../../services/realtime-events.service';
import { TranslateService, TranslationKey } from '../../../services/translate.service';
import { onDocumentVisibilityChange$ } from '../../../utils/event.utils';
import { chainSignals } from '../../../utils/signal.utils';

@Component({
Expand Down Expand Up @@ -102,13 +103,14 @@ export class MenuComponent {

constructor() {
if (this._swUpdate.isEnabled) {
merge(fromEvent(document, 'visibilitychange'), this._realtimeEventsService.onReconnected$)
merge(
onDocumentVisibilityChange$().pipe(filter(isVisible => isVisible)),
this._realtimeEventsService.onReconnected$
)
.pipe(takeUntilDestroyed())
.subscribe(() => {
if (!document.hidden) {
console.info('Checking for updates...');
this._swUpdate.checkForUpdate().then(x => console.info('Update check result:', x));
}
console.info('Checking for updates...');
this._swUpdate.checkForUpdate().then(x => console.info('Update check result:', x));
});
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/client/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { Router } from '@angular/router';
import { fromEvent } from 'rxjs';
import { filter } from 'rxjs';

import {
AuthTokenInfo,
Expand All @@ -20,6 +20,7 @@ import {
} from './storage';
import { AuthenticationService } from '../api/services';
import { environment } from '../environments/environment';
import { onDocumentVisibilityChange$ } from '../utils/event.utils';
import { assertBody } from '../utils/http.utils';

export type SignInResult = 'success' | 'invalid-token' | 'error';
Expand Down Expand Up @@ -50,12 +51,13 @@ export class AuthService implements OnDestroy {
}
});

fromEvent(document, 'visibilitychange')
.pipe(takeUntilDestroyed())
onDocumentVisibilityChange$()
.pipe(
takeUntilDestroyed(),
filter(isVisible => isVisible)
)
.subscribe(() => {
if (!document.hidden) {
this.ensureTokenNotExpired();
}
this.ensureTokenNotExpired();
});
}

Expand Down
20 changes: 20 additions & 0 deletions src/client/src/app/utils/event.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { EMPTY, fromEvent, map } from 'rxjs';

export function onDocumentVisibilityChange$() {
let hidden: string;
let visibilityChange: string;
if ('hidden' in document) {
hidden = 'hidden';
visibilityChange = 'visibilitychange';
} else if ('msHidden' in document) {
hidden = 'msHidden';
visibilityChange = 'msvisibilitychange';
} else if ('webkitHidden' in document) {
hidden = 'webkitHidden';
visibilityChange = 'webkitvisibilitychange';
} else {
return EMPTY;
}

return fromEvent(document, visibilityChange).pipe(map(() => !(document as any)[hidden]));
}

0 comments on commit 76d3d4d

Please sign in to comment.