Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: app not automatically updating on iOS #97

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]));
}
Loading