Add progressive web app companion for cross-platform access
Vite + TypeScript PWA that mirrors the Android app's core features: - Pre-processed shelter data (build-time UTM33N→WGS84 conversion) - Leaflet map with shelter markers, user location, and offline tiles - Canvas compass arrow (ported from DirectionArrowView.kt) - IndexedDB shelter cache with 7-day staleness check - Service worker with CacheFirst tiles and precached app shell - i18n for en, nb, nn (ported from Android strings.xml) - iOS/Android compass handling with low-pass filter - Respects user map interaction (no auto-snap on pan/zoom) - Build revision cache-breaker for reliable SW updates Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
46365b713b
commit
e8428de775
12051 changed files with 1799735 additions and 0 deletions
73
pwa/src/data/shelter-db.ts
Normal file
73
pwa/src/data/shelter-db.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* IndexedDB wrapper for shelter data using the idb library.
|
||||
* Ported from Room database in the Android app.
|
||||
*/
|
||||
|
||||
import { openDB, type IDBPDatabase } from 'idb';
|
||||
import type { Shelter } from '../types';
|
||||
|
||||
const DB_NAME = 'tilfluktsrom';
|
||||
const DB_VERSION = 1;
|
||||
const SHELTER_STORE = 'shelters';
|
||||
const META_STORE = 'metadata';
|
||||
|
||||
const META_KEY_LAST_UPDATE = 'lastUpdate';
|
||||
|
||||
type TilfluktsromDB = IDBPDatabase;
|
||||
|
||||
let dbPromise: Promise<TilfluktsromDB> | null = null;
|
||||
|
||||
function getDb(): Promise<TilfluktsromDB> {
|
||||
if (!dbPromise) {
|
||||
dbPromise = openDB(DB_NAME, DB_VERSION, {
|
||||
upgrade(db) {
|
||||
if (!db.objectStoreNames.contains(SHELTER_STORE)) {
|
||||
db.createObjectStore(SHELTER_STORE, { keyPath: 'lokalId' });
|
||||
}
|
||||
if (!db.objectStoreNames.contains(META_STORE)) {
|
||||
db.createObjectStore(META_STORE);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
return dbPromise;
|
||||
}
|
||||
|
||||
/** Get all cached shelters. */
|
||||
export async function getAllShelters(): Promise<Shelter[]> {
|
||||
const db = await getDb();
|
||||
return db.getAll(SHELTER_STORE);
|
||||
}
|
||||
|
||||
/** Check if any shelter data is cached. */
|
||||
export async function hasCachedData(): Promise<boolean> {
|
||||
const db = await getDb();
|
||||
const count = await db.count(SHELTER_STORE);
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
/** Replace all shelter data (clear + insert). */
|
||||
export async function replaceShelters(shelters: Shelter[]): Promise<void> {
|
||||
const db = await getDb();
|
||||
const tx = db.transaction(SHELTER_STORE, 'readwrite');
|
||||
await tx.store.clear();
|
||||
for (const shelter of shelters) {
|
||||
await tx.store.put(shelter);
|
||||
}
|
||||
await tx.done;
|
||||
|
||||
// Update last-update timestamp
|
||||
const metaTx = db.transaction(META_STORE, 'readwrite');
|
||||
await metaTx.store.put(Date.now(), META_KEY_LAST_UPDATE);
|
||||
await metaTx.done;
|
||||
}
|
||||
|
||||
/** Check if cached data is older than the given max age (default 7 days). */
|
||||
export async function isDataStale(
|
||||
maxAgeMs = 7 * 24 * 60 * 60 * 1000,
|
||||
): Promise<boolean> {
|
||||
const db = await getDb();
|
||||
const lastUpdate = await db.get(META_STORE, META_KEY_LAST_UPDATE);
|
||||
if (lastUpdate == null) return true;
|
||||
return Date.now() - (lastUpdate as number) > maxAgeMs;
|
||||
}
|
||||
33
pwa/src/data/shelter-repository.ts
Normal file
33
pwa/src/data/shelter-repository.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Repository that manages shelter data: fetches pre-processed JSON,
|
||||
* caches in IndexedDB, and handles staleness checks.
|
||||
*
|
||||
* Unlike the Android app, no ZIP handling or coordinate conversion
|
||||
* is needed at runtime — the data is pre-processed at build time.
|
||||
*/
|
||||
|
||||
import type { Shelter } from '../types';
|
||||
import {
|
||||
getAllShelters,
|
||||
hasCachedData,
|
||||
replaceShelters,
|
||||
isDataStale,
|
||||
} from './shelter-db';
|
||||
|
||||
const SHELTERS_JSON_PATH = './data/shelters.json';
|
||||
|
||||
/** Fetch shelters.json and cache in IndexedDB. Returns success. */
|
||||
export async function refreshData(): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(SHELTERS_JSON_PATH);
|
||||
if (!response.ok) return false;
|
||||
|
||||
const shelters: Shelter[] = await response.json();
|
||||
await replaceShelters(shelters);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export { getAllShelters, hasCachedData, isDataStale };
|
||||
Loading…
Add table
Add a link
Reference in a new issue