Ajout tâche de mise à jour en arrière-plan

This commit is contained in:
Emmy D'Anello 2024-12-10 00:52:15 +01:00
parent 7becd396d3
commit a98b2b56ec
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
5 changed files with 59 additions and 1 deletions

View File

@ -43,6 +43,7 @@
"locationAlwaysAndWhenInUsePermission": "Allow $(PRODUCT_NAME) to use your location."
}
],
"expo-background-fetch",
"expo-task-manager",
"expo-secure-store"
],

View File

@ -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()

View File

@ -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",

View File

@ -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",

View File

@ -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 | (() => 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
}, [])