|
@@ -0,0 +1,73 @@
|
|
|
+import * as SQLite from 'expo-sqlite';
|
|
|
+import NetInfo, { NetInfoState } from '@react-native-community/netinfo';
|
|
|
+import { storage } from 'src/storage';
|
|
|
+import { fetchLimitedRanking, fetchLpi, fetchInHistory } from '@api/ranking';
|
|
|
+
|
|
|
+const db = SQLite.openDatabase('nomadManiaDb.db');
|
|
|
+
|
|
|
+export const initializeDatabase = (): Promise<void> => {
|
|
|
+ return new Promise<void>((resolve, reject) => {
|
|
|
+ db.transaction(tx => {
|
|
|
+ createSyncTriggers('tableName');
|
|
|
+
|
|
|
+ resolve();
|
|
|
+ }, error => {
|
|
|
+ console.error('DB initialization error: ', error);
|
|
|
+ reject(error);
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+const createSyncTriggers = (tableName: string): void => {
|
|
|
+};
|
|
|
+
|
|
|
+export const checkInternetConnection = async (): Promise<NetInfoState> => {
|
|
|
+ const state = await NetInfo.fetch();
|
|
|
+ return state;
|
|
|
+};
|
|
|
+
|
|
|
+export const syncDataWithServer = async (): Promise<void> => {
|
|
|
+ const { isConnected, isInternetReachable } = await checkInternetConnection();
|
|
|
+ if (isConnected && isInternetReachable) {
|
|
|
+ processSyncQueue();
|
|
|
+ await updateAvatars();
|
|
|
+ await updateMasterRanking();
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const processSyncQueue = (): void => {
|
|
|
+};
|
|
|
+
|
|
|
+export const updateAvatars = async (): Promise<void> => {
|
|
|
+};
|
|
|
+
|
|
|
+export const updateStaticGeoJSON = async (): Promise<void> => {
|
|
|
+};
|
|
|
+
|
|
|
+export const setupDatabaseAndSync = async (): Promise<void> => {
|
|
|
+ await initializeDatabase();
|
|
|
+ await syncDataWithServer();
|
|
|
+ await updateStaticGeoJSON();
|
|
|
+};
|
|
|
+
|
|
|
+const updateMasterRanking = async () => {
|
|
|
+ const dataLimitedRanking = await fetchLimitedRanking();
|
|
|
+
|
|
|
+ if (dataLimitedRanking && dataLimitedRanking.data) {
|
|
|
+ storage.set('masterRanking', JSON.stringify(dataLimitedRanking.data));
|
|
|
+ }
|
|
|
+
|
|
|
+ const dataLpi = await fetchLpi();
|
|
|
+
|
|
|
+ if (dataLpi && dataLpi.data) {
|
|
|
+ storage.set('lpiRanking', JSON.stringify(dataLpi.data));
|
|
|
+ }
|
|
|
+
|
|
|
+ const dataInHistory = await fetchInHistory();
|
|
|
+
|
|
|
+ if (dataInHistory && dataInHistory.data) {
|
|
|
+ storage.set('inHistoryRanking', JSON.stringify(dataInHistory.data));
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+export default setupDatabaseAndSync;
|