|
|
@@ -2,6 +2,8 @@ import * as FileSystem from 'expo-file-system/legacy';
|
|
|
import * as MapLibreRN from '@maplibre/maplibre-react-native';
|
|
|
|
|
|
import { VECTOR_MAP_HOST } from 'src/constants';
|
|
|
+import { fetchLastMapTilesUpdate } from '@api/app';
|
|
|
+import { storage, StoreType } from 'src/storage';
|
|
|
|
|
|
const baseTilesDir = `${FileSystem.cacheDirectory}tiles/`;
|
|
|
const STYLE_URL = `${VECTOR_MAP_HOST}/nomadmania-maps2025.json`;
|
|
|
@@ -18,6 +20,19 @@ async function deleteCachedTilesIfExist(): Promise<void> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function getPackName(p: any): string | undefined {
|
|
|
+ if (p?.name) return p.name;
|
|
|
+
|
|
|
+ const metaStr = p?.pack?.metadata;
|
|
|
+ if (!metaStr) return undefined;
|
|
|
+
|
|
|
+ try {
|
|
|
+ return JSON.parse(metaStr)?.name;
|
|
|
+ } catch {
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
async function setupOfflineRegion(): Promise<void> {
|
|
|
try {
|
|
|
const bounds: [GeoJSON.Position, GeoJSON.Position] = [
|
|
|
@@ -28,7 +43,7 @@ async function setupOfflineRegion(): Promise<void> {
|
|
|
const maxZoom = 6;
|
|
|
|
|
|
const existingPacks = await MapLibreRN.OfflineManager.getPacks();
|
|
|
- const pack = existingPacks.find((pack) => pack.name === PACK_NAME);
|
|
|
+ const pack = existingPacks.find((pack) => getPackName(pack) === PACK_NAME);
|
|
|
|
|
|
if (pack) {
|
|
|
const status = await pack.status();
|
|
|
@@ -44,7 +59,7 @@ async function setupOfflineRegion(): Promise<void> {
|
|
|
maxZoom,
|
|
|
styleURL: STYLE_URL
|
|
|
},
|
|
|
- (offlineRegion, status) => {},
|
|
|
+ () => {},
|
|
|
(error) => {
|
|
|
if (error) {
|
|
|
console.error('Error creating offline pack:', error);
|
|
|
@@ -59,5 +74,29 @@ async function setupOfflineRegion(): Promise<void> {
|
|
|
|
|
|
export async function initOfflineSetup(): Promise<void> {
|
|
|
await deleteCachedTilesIfExist();
|
|
|
+ await updateMapsCache();
|
|
|
await setupOfflineRegion();
|
|
|
}
|
|
|
+
|
|
|
+export async function updateMapsCache() {
|
|
|
+ const localLastDate =
|
|
|
+ (storage.get('lastMapTilesUpdate', StoreType.STRING) as string) || '1990-01-01';
|
|
|
+ const lastUpdate = await fetchLastMapTilesUpdate();
|
|
|
+
|
|
|
+ if (lastUpdate && lastUpdate.date !== localLastDate) {
|
|
|
+ const packs = await MapLibreRN.OfflineManager.getPacks();
|
|
|
+
|
|
|
+ for (const pack of packs) {
|
|
|
+ const name = getPackName(pack);
|
|
|
+ if (!name) continue;
|
|
|
+ try {
|
|
|
+ await MapLibreRN.OfflineManager.invalidatePack(name);
|
|
|
+ } catch {}
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ await MapLibreRN.OfflineManager.invalidateAmbientCache();
|
|
|
+ } catch {}
|
|
|
+
|
|
|
+ storage.set('lastMapTilesUpdate', lastUpdate.date);
|
|
|
+ }
|
|
|
+}
|