Skip to content

Commit 1ac7a74

Browse files
committed
make last route restore on installed apps
1 parent 64318a0 commit 1ac7a74

5 files changed

Lines changed: 62 additions & 3 deletions

File tree

app/core/reactive-match-media.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { assert } from "@ember/debug";
12
import { trackedObject } from "@ember/reactive/collections";
23

4+
const HasQuery = new Set<string>();
35
const MediaQueries: Record<string, boolean> = trackedObject({});
46
function addMediaQueryListener(query: string): boolean {
7+
assert(`matchMedia query must be a non-empty string`, typeof query === 'string' && query.length > 0);
58
// Initialize on first access
6-
if (MediaQueries[query] === undefined) {
9+
if (!HasQuery.has(query)) {
10+
HasQuery.add(query);
711
const mediaQueryList = window.matchMedia(query);
812
MediaQueries[query] = mediaQueryList.matches;
913

@@ -15,8 +19,10 @@ function addMediaQueryListener(query: string): boolean {
1519
mediaQueryList.addEventListener('change', listener);
1620
return mediaQueryList.matches;
1721
}
18-
return MediaQueries[query];
22+
23+
return MediaQueries[query]!;
1924
}
25+
2026
/**
2127
* Decorator which marks a field as being populated via `window.matchMedia`.
2228
*

app/core/site-theme.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ class SiteTheme {
2323
@matchMedia('(prefers-color-scheme: dark)')
2424
systemPrefersDarkMode: boolean = false;
2525

26+
@matchMedia('(display-mode: minimal-ui)')
27+
isMinimalUI: boolean = false;
28+
29+
@matchMedia('(display-mode: standalone)')
30+
isStandalone: boolean = false;
31+
32+
@matchMedia('(display-mode: fullscreen)')
33+
isFullscreen: boolean = false;
34+
35+
isIOSStandalone: boolean = Boolean('standalone' in navigator && navigator.standalone);
36+
37+
get isRunningAsInstalledApp(): boolean {
38+
return this.isIOSStandalone || this.isMinimalUI || this.isStandalone;
39+
}
40+
2641
get isDarkMode(): boolean {
2742
if (this.explicitThemePreference === 'dark') {
2843
return true;

app/services/history.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Service, { service } from '@ember/service';
2+
import type RouterService from '@ember/routing/router-service';
3+
import { field, PersistedResource } from '#app/core/persisted-resource.ts';
4+
import { getTheme } from '#app/core/site-theme.ts';
5+
6+
@PersistedResource('route-history')
7+
class HistoryService extends Service {
8+
@service declare router: RouterService;
9+
10+
@field
11+
latestRoute: string | null = null;
12+
13+
track() {
14+
if (getTheme().isRunningAsInstalledApp) {
15+
if (this.latestRoute) {
16+
// Navigate to the last route
17+
this.router.replaceWith(this.latestRoute);
18+
}
19+
}
20+
21+
// Listen to Ember's route changes and read from window
22+
this.router.on('routeDidChange', () => {
23+
// store the url minus the hostname
24+
const url = location.pathname + location.search + location.hash;
25+
this.latestRoute = url;
26+
});
27+
}
28+
}
29+
30+
export default HistoryService;

app/templates/application.gts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ import Component from '@glimmer/component';
33
import { checkServiceWorker } from '#app/core/preferences.ts';
44
import { service } from '@ember/service';
55
import type PortalsService from '#app/services/ux/portals.ts';
6+
import type HistoryService from '#app/services/history.ts';
7+
import type Owner from '@ember/owner';
68

79
void checkServiceWorker();
810

911
export default class Application extends Component {
1012
@service('ux/portals') portals!: PortalsService;
13+
@service history!: HistoryService;
14+
15+
constructor(owner: Owner, args: object) {
16+
super(owner, args);
17+
this.history.track();
18+
}
1119

1220
<template>
1321
{{this.portals.takeover}}

vite.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default defineConfig({
4444
description: 'Find Your Trail Friends! The Bandits are a Trail Running Community based in the SF Bay Area.',
4545
theme_color: '#000000',
4646
background_color: '#000000',
47-
display: 'standalone',
47+
display: 'minimal-ui',
4848
start_url: '/',
4949
icons: [
5050
{

0 commit comments

Comments
 (0)