From a98b2b56ec6ce0036b4f8398dc85ef125afbaa1f Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Tue, 10 Dec 2024 00:52:15 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20t=C3=A2che=20de=20mise=20=C3=A0=20jour?= =?UTF-8?q?=20en=20arri=C3=A8re-plan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/app.json | 1 + client/app/_layout.tsx | 3 ++- client/package-lock.json | 13 ++++++++++++ client/package.json | 1 + client/utils/background.ts | 42 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 client/utils/background.ts diff --git a/client/app.json b/client/app.json index 010ada8..d6c4b29 100644 --- a/client/app.json +++ b/client/app.json @@ -43,6 +43,7 @@ "locationAlwaysAndWhenInUsePermission": "Allow $(PRODUCT_NAME) to use your location." } ], + "expo-background-fetch", "expo-task-manager", "expo-secure-store" ], diff --git a/client/app/_layout.tsx b/client/app/_layout.tsx index d0c2601..61be9f8 100644 --- a/client/app/_layout.tsx +++ b/client/app/_layout.tsx @@ -6,13 +6,14 @@ import { Provider as StoreProvider } from 'react-redux' import { MD3DarkTheme, MD3LightTheme, PaperProvider } from 'react-native-paper' import store from '@/utils/store' import * as SecureStore from '@/utils/SecureStore' +import { useStartBackgroundFetchServiceEffect } from '@/utils/background' import { useStartGeolocationServiceEffect } from '@/utils/geolocation' import { useEffect } from 'react' import { useRouteInfo } from 'expo-router/build/hooks' -import TabsHeader from '@/components/ui/TabsHeader' export default function RootLayout() { useStartGeolocationServiceEffect() + useStartBackgroundFetchServiceEffect() const colorScheme = useColorScheme() const router = useRouter() const route = useRouteInfo() diff --git a/client/package-lock.json b/client/package-lock.json index 0c56231..c63e9c0 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -16,6 +16,7 @@ "@reduxjs/toolkit": "^2.4.0", "@turf/circle": "^7.1.0", "expo": "~52.0.11", + "expo-background-fetch": "~13.0.3", "expo-blur": "~14.0.1", "expo-constants": "~17.0.3", "expo-dev-client": "~5.0.4", @@ -7565,6 +7566,18 @@ "react-native": "*" } }, + "node_modules/expo-background-fetch": { + "version": "13.0.3", + "resolved": "https://registry.npmjs.org/expo-background-fetch/-/expo-background-fetch-13.0.3.tgz", + "integrity": "sha512-wayjvMima858mvEqsXo6camcoeBLusVJnvMPdG0GKi2d9hKuQXCNP90sShDpgXOEIVzjN0UzZ8PqULgQWbqdAg==", + "license": "MIT", + "dependencies": { + "expo-task-manager": "~12.0.0" + }, + "peerDependencies": { + "expo": "*" + } + }, "node_modules/expo-blur": { "version": "14.0.1", "resolved": "https://registry.npmjs.org/expo-blur/-/expo-blur-14.0.1.tgz", diff --git a/client/package.json b/client/package.json index a4dc116..198feeb 100644 --- a/client/package.json +++ b/client/package.json @@ -22,6 +22,7 @@ "@reduxjs/toolkit": "^2.4.0", "@turf/circle": "^7.1.0", "expo": "~52.0.11", + "expo-background-fetch": "~13.0.3", "expo-blur": "~14.0.1", "expo-constants": "~17.0.3", "expo-dev-client": "~5.0.4", diff --git a/client/utils/background.ts b/client/utils/background.ts new file mode 100644 index 0000000..c91fdf2 --- /dev/null +++ b/client/utils/background.ts @@ -0,0 +1,42 @@ +import * as BackgroundFetch from 'expo-background-fetch' +import * as TaskManager from 'expo-task-manager' +import { Platform } from 'react-native' +import { useEffect } from 'react' + +const BACKGROUND_FETCH_TASK = "background-fetch" +const BACKGROUND_FETCH_INTERVAL = 20 + +async function backgroundUpdate() { + async () => { + const now = Date.now() + console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`) + // Be sure to return the successful result type! + return BackgroundFetch.BackgroundFetchResult.NewData + } +} + +TaskManager.defineTask(BACKGROUND_FETCH_TASK, backgroundUpdate) + +export async function startBackgroundFetchService(): Promise void)> { + if (Platform.OS === "web") { + const interval = setInterval(backgroundUpdate, BACKGROUND_FETCH_INTERVAL) + return () => clearInterval(interval) + } + + if (await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK)) + return async () => await BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK) + + await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, { + minimumInterval: BACKGROUND_FETCH_INTERVAL, + stopOnTerminate: false, + startOnBoot: true, + }) + + return async () => await BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK) +} + +export const useStartBackgroundFetchServiceEffect = () => useEffect(() => { + let cleanup: void | (() => void) = () => {} + startBackgroundFetchService().then(result => cleanup = result) + return cleanup +}, [])