瀏覽代碼

Merge branch 'db-update' of SashaGoncharov19/nomadmania-app into dev

Viktoriia 1 年之前
父節點
當前提交
878344948f

+ 5 - 0
src/database/index.ts

@@ -9,9 +9,12 @@ import { updateAvatars } from './avatarsService';
 import { fetchAndSaveStatistics } from './statisticsService';
 import { saveTriumphsData } from './triumphsService';
 import { saveSeriesRankingData } from './seriesRankingService';
+import { updateDarePlacesDb, updateNmRegionsDb } from 'src/db';
 
 const db = SQLite.openDatabase('nomadManiaDb.db');
 const lastUpdateDate = storage.get('lastUpdateDate', StoreType.STRING) as string || '1990-01-01';
+const lastUpdateNmRegions = storage.get('lastUpdateNmRegions', StoreType.STRING) as string || '1990-01-01';
+const lastUpdateDarePlaces = storage.get('lastUpdateDarePlaces', StoreType.STRING) as string || '1990-01-01';
 
 export const initializeDatabase = (): Promise<void> => {
   return new Promise<void>((resolve, reject) => {
@@ -44,6 +47,8 @@ export const syncDataWithServer = async (): Promise<void> => {
     await updateMasterRanking();
     await downloadFlags();
     await updateAvatars(lastUpdateDate);
+    await updateNmRegionsDb(lastUpdateNmRegions);
+    await updateDarePlacesDb(lastUpdateDarePlaces);
     await initTilesDownload(userId);
   }
 };

+ 105 - 27
src/db/index.ts

@@ -1,6 +1,9 @@
 import * as SQLite from 'expo-sqlite';
 import * as FileSystem from 'expo-file-system';
 import { Asset } from 'expo-asset';
+import { API_HOST } from 'src/constants';
+import { storage } from 'src/storage';
+import { fetchLastDareDbUpdate, fetchLastRegionsDbUpdate } from '@api/app';
 
 let db1: SQLite.SQLiteDatabase | null = null;
 let db2: SQLite.SQLiteDatabase | null = null;
@@ -11,18 +14,15 @@ const sqliteFullPath = FileSystem.documentDirectory + sqliteDirectory;
 const DS = '/';
 
 async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
-  console.log("DB copy start - " + dbName);
+  console.log('DB copy start - ' + dbName);
   await dbAsset.downloadAsync();
-  await FileSystem.downloadAsync(
-    dbAsset.uri,
-    sqliteFullPath + DS + dbName
-  );
+  await FileSystem.downloadAsync(dbAsset.uri, sqliteFullPath + DS + dbName);
 
   const dbUri = sqliteFullPath + DS + dbName;
 
   await FileSystem.copyAsync({
     from: dbAsset.localUri ?? '',
-    to: dbUri,
+    to: dbUri
   });
   return dbUri;
 }
@@ -34,24 +34,39 @@ export async function openDatabases() {
       await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
     }
 
-    const nmRegionsDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, { size: true });
+    const nmRegionsDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, {
+      size: true
+    });
     if (!nmRegionsDB.exists) {
-      await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
+      await copyDatabaseFile(
+        nmRegionsDBname,
+        Asset.fromModule(require('../../assets/db/' + nmRegionsDBname))
+      );
     }
-    if (nmRegionsDB.size == 0) {
+    if (nmRegionsDB.exists && nmRegionsDB.size == 0) {
       await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname);
-      await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
+      await copyDatabaseFile(
+        nmRegionsDBname,
+        Asset.fromModule(require('../../assets/db/' + nmRegionsDBname))
+      );
     }
 
-
-    const darePlacesDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, { size: true });
+    const darePlacesDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, {
+      size: true
+    });
     if (!darePlacesDB.exists) {
-      await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
+      await copyDatabaseFile(
+        darePlacesDBname,
+        Asset.fromModule(require('../../assets/db/' + darePlacesDBname))
+      );
     }
-    
-    if (darePlacesDB.size == 0) {
+
+    if (darePlacesDB.exists && darePlacesDB.size == 0) {
       await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname);
-      await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
+      await copyDatabaseFile(
+        darePlacesDBname,
+        Asset.fromModule(require('../../assets/db/' + darePlacesDBname))
+      );
     }
 
     const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
@@ -69,17 +84,8 @@ export async function refreshDatabases() {
     if (!fileInfo.exists) {
       await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
     }
-    await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname, {idempotent: true});
-    await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
-
-    await FileSystem.deleteAsync(sqliteFullPath + DS +  darePlacesDBname , {idempotent: true} );
-    await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
-
-    const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
-    db1 = null;
-    db2 = null;
-    db1 = openDatabase(nmRegionsDBname);
-    db2 = openDatabase(darePlacesDBname);
+    await refreshNmDatabase();
+    await refreshDarePlacesDatabase();
   } catch (error) {
     console.error('refreshDatabases - Error:');
     console.error(JSON.stringify(error, null, 2));
@@ -93,3 +99,75 @@ export function getFirstDatabase() {
 export function getSecondDatabase() {
   return db2;
 }
+
+const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
+
+async function refreshNmDatabase() {
+  try {
+    await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname, { idempotent: true });
+    const nmUrl = `${API_HOST}/static/app/${nmRegionsDBname}`;
+    let nmFileUri = sqliteFullPath + DS + nmRegionsDBname;
+
+    const nmResponse = await FileSystem.downloadAsync(nmUrl, nmFileUri);
+
+    if (nmResponse.status !== 200) {
+      throw new Error(`Failed to download the nmDb file: Status code ${nmResponse.status}`);
+    }
+
+    db1 = null;
+    db1 = openDatabase(nmRegionsDBname);
+  } catch (error) {
+    console.error('refreshDatabase nmRegions - Error:');
+    console.error(JSON.stringify(error, null, 2));
+  }
+}
+
+async function refreshDarePlacesDatabase() {
+  try {
+    await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname, { idempotent: true });
+    const dareUrl = `${API_HOST}/static/app/${darePlacesDBname}`;
+    let dareFileUri = sqliteFullPath + DS + darePlacesDBname;
+
+    const dareResponse = await FileSystem.downloadAsync(dareUrl, dareFileUri);
+
+    if (dareResponse.status !== 200) {
+      throw new Error(`Failed to download the dareDb file: Status code ${dareResponse.status}`);
+    }
+
+    db2 = null;
+    db2 = openDatabase(darePlacesDBname);
+  } catch (error) {
+    console.error('refreshDatabase darePlaces - Error:');
+    console.error(JSON.stringify(error, null, 2));
+  }
+}
+
+export async function updateNmRegionsDb(localLastDate: string) {
+  const lastUpdate = await fetchLastRegionsDbUpdate(localLastDate);
+
+  if (lastUpdate && lastUpdate.date !== localLastDate) {
+    const dirInfo = await FileSystem.getInfoAsync(sqliteFullPath);
+
+    if (!dirInfo.exists) {
+      await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
+    }
+
+    await refreshNmDatabase();
+    storage.set('lastUpdateNmRegions', lastUpdate.date);
+  }
+}
+
+export async function updateDarePlacesDb(localLastDate: string) {
+  const lastUpdate = await fetchLastDareDbUpdate(localLastDate);
+
+  if (lastUpdate && lastUpdate.date !== localLastDate) {
+    const dirInfo = await FileSystem.getInfoAsync(sqliteFullPath);
+
+    if (!dirInfo.exists) {
+      await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
+    }
+
+    await refreshDarePlacesDatabase();
+    storage.set('lastUpdateDarePlaces', lastUpdate.date);
+  }
+}

+ 7 - 3
src/screens/InAppScreens/TravellersScreen/index.tsx

@@ -1,5 +1,5 @@
 import React from 'react';
-import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native';
+import { View, Text, FlatList, TouchableOpacity, StyleSheet, Dimensions } from 'react-native';
 import { useNavigation } from '@react-navigation/native';
 
 import { Colors } from '../../../theme';
@@ -35,7 +35,11 @@ const TravellersScreen = () => {
       onPress={() => navigation.navigate(item.page as never)}
     >
       <View style={styles.button}>
-        <item.icon fill={Colors.ORANGE} width={110} height={32} />
+        <item.icon
+          fill={Colors.ORANGE}
+          width={Dimensions.get('window').width < 380 ? 105 : 110}
+          height={32}
+        />
         <Text style={styles.label}>{item.label}</Text>
       </View>
     </TouchableOpacity>
@@ -59,7 +63,7 @@ const TravellersScreen = () => {
 const styles = StyleSheet.create({
   container: {
     justifyContent: 'space-between',
-    paddingHorizontal: 24,
+    paddingHorizontal: Dimensions.get('window').width < 380 ? '5%' : 24,
     paddingVertical: 8,
     gap: 32,
     width: '100%'

+ 7 - 3
src/screens/InAppScreens/TravelsScreen/index.tsx

@@ -1,5 +1,5 @@
 import React, { useState } from 'react';
-import { View, Text, FlatList, TouchableOpacity, StyleSheet } from 'react-native';
+import { View, Text, FlatList, TouchableOpacity, StyleSheet, Dimensions } from 'react-native';
 import { useNavigation } from '@react-navigation/native';
 
 import { Colors } from '../../../theme';
@@ -41,7 +41,11 @@ const TravelsScreen = () => {
   const renderItem = ({ item }: { item: { label: string; icon: any; page: string } }) => (
     <TouchableOpacity style={{ alignItems: 'center' }} onPress={() => handlePress(item.page)}>
       <View style={styles.button}>
-        <item.icon fill={Colors.ORANGE} width={110} height={32} />
+        <item.icon
+          fill={Colors.ORANGE}
+          width={Dimensions.get('window').width < 380 ? 105 : 110}
+          height={32}
+        />
         <Text style={styles.label}>{item.label}</Text>
       </View>
     </TouchableOpacity>
@@ -70,7 +74,7 @@ const TravelsScreen = () => {
 const styles = StyleSheet.create({
   container: {
     justifyContent: 'space-between',
-    paddingHorizontal: 24,
+    paddingHorizontal: Dimensions.get('window').width < 380 ? '5%' : 24,
     paddingVertical: 8,
     gap: 32,
     width: '100%'