From a600ca1dec57aad0de12821b96b7ab38fac21e1e Mon Sep 17 00:00:00 2001 From: rene Date: Sat, 6 Jun 2026 12:15:34 +0200 Subject: [PATCH] Offline: Giftkoeder + vermisste Hunde offline sichtbar, Korridor auf Detailkarte MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Geraetetest-Befunde Runde 2: - Giftkoeder verschwanden offline: /api/places kam aus dem SW-Cache (feste URL) und verhinderte den allFailed-Fallback, waehrend /api/poison?lat=... (Bbox-URL) scheiterte -> jetzt faellt jede Quelle EINZELN auf den letzten guten Stand zurueck (localStorage) + Merge aus dem Offline-Region-Snapshot - Region-Download speichert jetzt auch /api/poison + /api/lost der Gegend (p/_poison, p/_lost, anonym; MapOffline.alerts(kind,bbox) als Reader) — Sicherheitsdaten muessen auch am vorab gespeicherten Urlaubsort da sein - lost.js Offline-Pfad merged den Region-Snapshot in Cache- und Leer-Fall - Routen-Korridor war 'unsichtbar' (lag im schon gespeicherten Gebiet): nach dem Speichern werden die gespeicherten Bereiche blau auf der Detailkarte eingeblendet; Logik per Node-Stub-Test verifiziert Bump v1227 --- VERSION | 2 +- backend/static/index.html | 24 ++++++------ backend/static/js/app.js | 2 +- backend/static/js/map-offline.js | 62 +++++++++++++++++++++++-------- backend/static/js/pages/lost.js | 23 +++++++++++- backend/static/js/pages/map.js | 43 +++++++++++++-------- backend/static/js/pages/routes.js | 14 +++++++ backend/static/landing.html | 2 +- backend/static/sw.js | 2 +- 9 files changed, 125 insertions(+), 49 deletions(-) diff --git a/VERSION b/VERSION index 872362f..7ff9fa9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1226 \ No newline at end of file +1227 \ No newline at end of file diff --git a/backend/static/index.html b/backend/static/index.html index 6d70e7a..bf78b9c 100644 --- a/backend/static/index.html +++ b/backend/static/index.html @@ -86,14 +86,14 @@ Ban Yaro - + - - - - - + + + + + @@ -612,11 +612,11 @@ - - - - - + + + + + @@ -626,7 +626,7 @@ - + diff --git a/backend/static/js/app.js b/backend/static/js/app.js index ab7739f..e792a8b 100644 --- a/backend/static/js/app.js +++ b/backend/static/js/app.js @@ -3,7 +3,7 @@ Router, State-Management, Navigation, Initialisierung. ============================================================ */ -const APP_VER = '1226'; // ← bei jedem Deploy mit Frontend-Änderungen erhöhen +const APP_VER = '1227'; // ← bei jedem Deploy mit Frontend-Änderungen erhöhen const APP_VERSION = '1.6.0'; // ← semantische Version, wird bei make release gesetzt window.APP_VER = APP_VER; // global verfügbar für andere Module (z.B. offline-indicator) window.APP_VERSION = APP_VERSION; diff --git a/backend/static/js/map-offline.js b/backend/static/js/map-offline.js index bc99759..127c286 100644 --- a/backend/static/js/map-offline.js +++ b/backend/static/js/map-offline.js @@ -126,6 +126,22 @@ window.MapOffline = (function () { var POI_TYPES = ['waste_basket', 'dog_park', 'drinking_water', 'tierarzt', 'hundesalon', 'shop', 'restaurant', 'bank', 'giftkoeder', 'kotbeutel', 'gefahr', 'parkplatz', 'treffpunkt', 'sonstiges', 'hotel']; + + // Frische Liste mit Bestand mergen (per id) — eine zweite Region (Urlaubsort) darf die erste + // nicht löschen. Liefert die Anzahl frischer Einträge. + function _mergeStore(key, fresh) { + if (!fresh || !fresh.length) return Promise.resolve(0); + return _get(key).then(function (old) { + var merged = fresh; + if (old && old.length) { + var seen = {}; + fresh.forEach(function (p) { seen[p.id] = true; }); + merged = fresh.concat(old.filter(function (p) { return !seen[p.id]; })); + } + return _put(key, merged).then(function () { return fresh.length; }); + }); + } + function _cachePois(bbox) { var total = 0; var jobs = POI_TYPES.map(function (type) { @@ -133,25 +149,41 @@ window.MapOffline = (function () { south: bbox.south, west: bbox.west, north: bbox.north, east: bbox.east }); return fetch('/api/osm/pois?' + params) .then(function (r) { return r.ok ? r.json() : null; }) - .then(function (fresh) { - if (!fresh || !fresh.length) return; - total += fresh.length; - // Mit Bestand mergen (per id) — eine zweite Region (Urlaubsort) darf die erste nicht löschen. - return _get('p/' + type).then(function (old) { - var merged = fresh; - if (old && old.length) { - var seen = {}; - fresh.forEach(function (p) { seen[p.id] = true; }); - merged = fresh.concat(old.filter(function (p) { return !seen[p.id]; })); - } - return _put('p/' + type, merged); - }); - }) + .then(function (fresh) { return _mergeStore('p/' + type, fresh); }) + .then(function (n) { total += n; }) .catch(function () {}); }); + + // Sicherheitsdaten MÜSSEN offline da sein (René 2026-06-07): Giftköder-Alarme + // (/api/poison, Radius in m) + vermisste Hunde (/api/lost, Radius in km) — beide anonym. + var midLat = (bbox.south + bbox.north) / 2, midLon = (bbox.west + bbox.east) / 2; + var radiusKm = Math.max( + (bbox.north - bbox.south) * 111 / 2, + (bbox.east - bbox.west) * 111 * Math.cos(midLat * Math.PI / 180) / 2, 1); + jobs.push(fetch('/api/poison?lat=' + midLat + '&lon=' + midLon + '&radius=' + Math.round(radiusKm * 1000)) + .then(function (r) { return r.ok ? r.json() : null; }) + .then(function (fresh) { return _mergeStore('p/_poison', fresh); }) + .then(function (n) { total += n; }) + .catch(function () {})); + jobs.push(fetch('/api/lost?lat=' + midLat + '&lon=' + midLon + '&radius_km=' + Math.ceil(radiusKm)) + .then(function (r) { return r.ok ? r.json() : null; }) + .then(function (fresh) { return _mergeStore('p/_lost', fresh); }) + .then(function (n) { total += n; }) + .catch(function () {})); + return Promise.all(jobs).then(function () { return total; }); } + // Gespeicherte Sicherheits-Alarme ('poison' | 'lost') im Bbox-Ausschnitt — Offline-Fallback. + function alerts(kind, bbox) { + return _get('p/_' + kind).then(function (list) { + if (!list || !list.length) return []; + return list.filter(function (p) { + return p.lat >= bbox.south && p.lat <= bbox.north && p.lon >= bbox.west && p.lon <= bbox.east; + }); + }).catch(function () { return []; }); + } + // Gespeicherte POIs eines Typs im Bbox-Ausschnitt — Offline-Fallback für die Karten-Marker. function pois(type, bbox) { return _get('p/' + type).then(function (list) { @@ -414,7 +446,7 @@ window.MapOffline = (function () { return { registerProtocol: registerProtocol, downloadAround: downloadAround, downloadCorridor: downloadCorridor, - tile: tile, glyph: glyph, pois: pois, coverage: coverage, + tile: tile, glyph: glyph, pois: pois, alerts: alerts, coverage: coverage, setGps: setGps, markDeadZone: markDeadZone, autoFillDeadZones: autoFillDeadZones, stats: stats, hasRegion: hasRegion, clear: clear, MAXZOOM: MAXZOOM, }; diff --git a/backend/static/js/pages/lost.js b/backend/static/js/pages/lost.js index 30d18e5..e2c92e9 100644 --- a/backend/static/js/pages/lost.js +++ b/backend/static/js/pages/lost.js @@ -233,10 +233,25 @@ window.Page_lost = (() => { ...p, distanz_m: _haversine(_userPos.lat, _userPos.lon, p.lat, p.lon), })); + // Offline-Region-Snapshot (Offline-Karten speichern vermisste Hunde mit) dazu mergen — + // deckt vorab gespeicherte Gegenden ab, die der localStorage-Stand nicht kennt. + let regionLost = []; + try { + if (window.MapOffline?.alerts) { + regionLost = await MapOffline.alerts('lost', { + south: _userPos.lat - 0.3, north: _userPos.lat + 0.3, + west: _userPos.lon - 0.45, east: _userPos.lon + 0.45, + }); + } + } catch {} try { const raw = localStorage.getItem(_CACHE_KEY); if (raw) { const cached = JSON.parse(raw).data || []; + const seen = new Set(cached.map(r => r.id)); + regionLost.filter(r => !seen.has(r.id)).forEach(r => cached.push({ + ...r, distanz_m: _haversine(_userPos.lat, _userPos.lon, r.lat, r.lon), + })); _reports = [...offline_pending, ...cached]; _renderMarkers(); _renderHeld(); @@ -246,12 +261,16 @@ window.Page_lost = (() => { return; } } catch {} - _reports = offline_pending; - if (offline_pending.length) { + // Kein localStorage-Stand → wenigstens Pending + Region-Snapshot zeigen + _reports = [...offline_pending, ...regionLost.map(r => ({ + ...r, distanz_m: _haversine(_userPos.lat, _userPos.lon, r.lat, r.lon), + }))]; + if (_reports.length) { _renderMarkers(); _renderHeld(); _renderList(); _updateBadge(_reports.length); + if (infoEl) infoEl.textContent = 'Offline — zeige gespeicherte Meldungen.'; return; } UI.toast.error('Meldungen konnten nicht geladen werden.'); diff --git a/backend/static/js/pages/map.js b/backend/static/js/pages/map.js index 709aedf..e62f653 100644 --- a/backend/static/js/pages/map.js +++ b/backend/static/js/pages/map.js @@ -1918,29 +1918,40 @@ window.Page_map = (() => { API.breeder.mapMarkers(), ]); + // Offline-Fallback PRO QUELLE (nicht alles-oder-nichts): Der SW cached /api/places und + // /api/breeder/map-markers (feste URLs), aber /api/poison?lat=… ändert sich mit jeder + // Position → Cache-Miss → vorher verschwanden offline ausgerechnet die GIFTKÖDER, + // während places aus dem SW-Cache kam und den allFailed-Fallback verhinderte + // (Gerätetest 2026-06-07). Jede Quelle fällt einzeln auf den letzten guten Stand zurück. + let cached = null; + try { cached = JSON.parse(localStorage.getItem(_MAP_POI_KEY) || 'null'); } catch {} const allFailed = [places, poisonList, breederList].every(r => r.status === 'rejected'); - if (allFailed) { + + const placesVal = places.status === 'fulfilled' ? places.value : (cached?.places || []); + let poisonVal = poisonList.status === 'fulfilled' ? poisonList.value : (cached?.poison || []); + const breederVal = breederList.status === 'fulfilled' ? breederList.value : (cached?.breeders || []); + + // Giftköder zusätzlich aus dem Offline-Region-Snapshot (deckt vorab gespeicherte + // Gegenden ab, wo der localStorage-Stand der letzten Position nicht hinreicht). + if (poisonList.status === 'rejected' && window.MapOffline?.alerts) { try { - const raw = localStorage.getItem(_MAP_POI_KEY); - if (raw) { - const cached = JSON.parse(raw); - _addPlaces(cached.places || []); - _addPoison(cached.poison || []); - _addBreeders(cached.breeders || []); - UI.toast.info('Offline — Karte zeigt gecachte Kacheln. POI-Daten eventuell veraltet.'); - _scheduleOsmLoad(); - return; + const c = _map ? _map.getCenter() : (_userPos ? { lat: _userPos.lat, lng: _userPos.lon } : null); + if (c) { + const off = await MapOffline.alerts('poison', + { south: c.lat - 0.5, north: c.lat + 0.5, west: c.lng - 0.7, east: c.lng + 0.7 }); + const seen = new Set(poisonVal.map(p => p.id)); + poisonVal = poisonVal.concat(off.filter(p => !seen.has(p.id))); } } catch {} } - const placesVal = places.status === 'fulfilled' ? places.value : []; - const poisonVal = poisonList.status === 'fulfilled' ? poisonList.value : []; - const breederVal = breederList.status === 'fulfilled' ? breederList.value : []; + if (allFailed && (placesVal.length || poisonVal.length || breederVal.length)) { + UI.toast.info('Offline — Karte zeigt zuletzt geladene Daten.'); + } - if (places.status === 'fulfilled') _addPlaces(placesVal); - if (poisonList.status === 'fulfilled') _addPoison(poisonVal); - if (breederList.status === 'fulfilled') _addBreeders(breederVal); + _addPlaces(placesVal); + _addPoison(poisonVal); + _addBreeders(breederVal); if (places.status === 'fulfilled' || poisonList.status === 'fulfilled' || breederList.status === 'fulfilled') { try { diff --git a/backend/static/js/pages/routes.js b/backend/static/js/pages/routes.js index 1a59698..68729c7 100644 --- a/backend/static/js/pages/routes.js +++ b/backend/static/js/pages/routes.js @@ -2478,6 +2478,20 @@ window.Page_routes = (() => { UI.toast.success(`Route offline gespeichert — Korridor ±1 km, ${res.pois || 0} Marker, ` + `${(res.bytes / 1048576).toFixed(1)} MB.${res.capped ? ' (50-MB-Limit erreicht)' : ''}`); window.OfflineIndicator?.refresh(); + // Gespeicherte Bereiche sofort auf der Detailkarte zeigen (blau) — sonst ist der + // Korridor „unsichtbar", v.a. wenn er im schon gespeicherten Gebiet liegt. + try { + const gl = _detailMap?._gl; + if (gl) { + const gj = await MapOffline.coverage(); + if (gl.getSource('rd-off-cov')) gl.getSource('rd-off-cov').setData(gj); + else { + gl.addSource('rd-off-cov', { type: 'geojson', data: gj }); + gl.addLayer({ id: 'rd-off-cov', type: 'fill', source: 'rd-off-cov', + paint: { 'fill-color': '#3b82f6', 'fill-opacity': 0.15 } }); + } + } + } catch (e) {} } catch (e) { if (label) label.textContent = 'Offline'; UI.toast.error('Offline-Speichern fehlgeschlagen.'); diff --git a/backend/static/landing.html b/backend/static/landing.html index c6ceae8..570319a 100644 --- a/backend/static/landing.html +++ b/backend/static/landing.html @@ -4,7 +4,7 @@ - + Ban Yaro — Die Hunde-App für Deutschland, Österreich & Schweiz diff --git a/backend/static/sw.js b/backend/static/sw.js index 3a425c7..9b5f8dc 100644 --- a/backend/static/sw.js +++ b/backend/static/sw.js @@ -4,7 +4,7 @@ ============================================================ */ // ← EINZIGE Stelle für die Version — STATIC_ASSETS und CACHE_VERSION leiten sich ab -const VER = '1226'; +const VER = '1227'; const CACHE_VERSION = `by-v${VER}`; const CACHE_STATIC = `${CACHE_VERSION}-static`; const CACHE_TILES = 'ban-yaro-tiles-v1'; // bleibt über SW-Updates erhalten