فهرست منبع

refresh map cache

Viktoriia 4 هفته پیش
والد
کامیت
03cebee760

+ 2 - 2
src/database/index.ts

@@ -28,11 +28,11 @@ export const syncDataWithServer = async (): Promise<void> => {
   const { isConnected } = await checkInternetConnection();
 
   if (isConnected) {
+    await updateNmRegionsDb(lastUpdateNmRegions);
     await updateDarePlacesDb(lastUpdateDarePlaces);
+    await initOfflineSetup();
     await updateMasterRanking();
-    await updateNmRegionsDb(lastUpdateNmRegions);
     await downloadFlags();
-    await initOfflineSetup();
   }
 };
 

+ 41 - 2
src/database/tilesService/index.ts

@@ -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);
+  }
+}

+ 2 - 1
src/modules/api/app/app-api.ts

@@ -17,5 +17,6 @@ export const appApi = {
   getLastDareUpdate: (date: string) =>
     request.postForm<PostGetLastUpdate>(API.GET_LAST_DARE_DB_UPDATE, { date }),
   premiumStatus: (token: string) =>
-    request.postForm<PostGetPremiumStatus>(API.GET_PREMIUM_STATUS, { token })
+    request.postForm<PostGetPremiumStatus>(API.GET_PREMIUM_STATUS, { token }),
+  getLastMapTilesUpdate: () => request.postForm<PostGetLastUpdate>(API.GET_LAST_MAP_TILES_UPDATE)
 };

+ 2 - 1
src/modules/api/app/app-query-keys.tsx

@@ -2,5 +2,6 @@ export const appQueryKeys = {
   deleteUser: () => ['deleteUser'] as const,
   getLastRegionsUpdate: () => ['getLastRegionsUpdate'] as const,
   getLastDareUpdate: () => ['getLastDareUpdate'] as const,
-  premiumStatus: (token: string) => ['premiumStatus', token] as const
+  premiumStatus: (token: string) => ['premiumStatus', token] as const,
+  getLastMapTilesUpdate: () => ['getLastMapTilesUpdate'] as const,
 };

+ 1 - 0
src/modules/api/app/queries/index.ts

@@ -2,3 +2,4 @@ export * from './use-post-delete-user';
 export * from './use-post-last-regions-db-update';
 export * from './use-post-last-dare-db-update';
 export * from './use-post-premium-status';
+export * from './use-post-last-map-tiles-update';

+ 21 - 0
src/modules/api/app/queries/use-post-last-map-tiles-update.tsx

@@ -0,0 +1,21 @@
+import { appQueryKeys } from '../app-query-keys';
+import { type PostGetLastUpdate, appApi } from '../app-api';
+import { queryClient } from 'src/utils/queryClient';
+
+export const fetchLastMapTilesUpdate = async () => {
+  try {
+    const data: PostGetLastUpdate = await queryClient.fetchQuery({
+      queryKey: appQueryKeys.getLastMapTilesUpdate(),
+      queryFn: async () => {
+        const response = await appApi.getLastMapTilesUpdate();
+        return response.data;
+      },
+      gcTime: 0,
+      staleTime: 0
+    });
+
+    return data;
+  } catch (error) {
+    console.error('Failed to fetch last regions update:', error);
+  }
+};

+ 0 - 1
src/screens/InAppScreens/MessagesScreen/GroupChatScreen/index.tsx

@@ -2085,7 +2085,6 @@ const GroupChatScreen = ({ route }: { route: any }) => {
         ) : null}
         <View
           onLayout={(e) => {
-            console.log('e.nativeEvent.layout.height', e.nativeEvent.layout.height)
             setInputHeight(e.nativeEvent.layout.height);
           }}
         >

+ 2 - 0
src/types/api.ts

@@ -91,6 +91,7 @@ export enum API_ENDPOINT {
   DELETE_USER = 'delete-user',
   GET_LAST_REGIONS_UPDATE = 'last-regions-db-update',
   GET_LAST_DARE_UPDATE = 'last-dare-db-update',
+  GET_LAST_MAP_TILES_UPDATE = 'last-map-tiles-update',
   GET_PROFILE_REGIONS = 'get-profile',
   GET_UNIVERSAL = 'universal',
   GET_REGIONS_DATA = 'get-app-region-screen-data',
@@ -290,6 +291,7 @@ export enum API {
   DELETE_USER = `${API_ROUTE.APP}/${API_ENDPOINT.DELETE_USER}`,
   GET_LAST_REGIONS_DB_UPDATE = `${API_ROUTE.APP}/${API_ENDPOINT.GET_LAST_REGIONS_UPDATE}`,
   GET_LAST_DARE_DB_UPDATE = `${API_ROUTE.APP}/${API_ENDPOINT.GET_LAST_DARE_UPDATE}`,
+  GET_LAST_MAP_TILES_UPDATE = `${API_ROUTE.APP}/${API_ENDPOINT.GET_LAST_MAP_TILES_UPDATE}`,
   GET_PROFILE_REGIONS = `${API_ROUTE.REGIONS}/${API_ENDPOINT.GET_PROFILE_REGIONS}`,
   GET_UNIVERSAL = `${API_ROUTE.SEARCH}/${API_ENDPOINT.GET_UNIVERSAL}`,
   GET_REGIONS_DATA = `${API_ROUTE.REGIONS}/${API_ENDPOINT.GET_REGIONS_DATA}`,