import { processPendingFoodLogs } from '@/add-log/food-detection/foodLogProcessor'; import { uploadPendingPhotos } from '@/add-log/photo/uploadService'; import * as BackgroundTask from 'expo-background-task'; import * as TaskManager from 'expo-task-manager'; const TASK_NAME = 'palace-foodlog-processor'; TaskManager.defineTask(TASK_NAME, async () => { try { await processPendingFoodLogs(); await uploadPendingPhotos(); return BackgroundTask.BackgroundTaskResult.Success; } catch { return BackgroundTask.BackgroundTaskResult.Failed; } }); export async function initBackgroundProcessing(): Promise { const status = await BackgroundTask.getStatusAsync(); if (status !== BackgroundTask.BackgroundTaskStatus.Available) { return; } const tasks = await TaskManager.getRegisteredTasksAsync(); const alreadyRegistered = tasks.some(task => task.taskName === TASK_NAME); if (!alreadyRegistered) { await BackgroundTask.registerTaskAsync(TASK_NAME, { minimumInterval: 15, }); } } export async function runPendingTasks(): Promise { await processPendingFoodLogs(); await uploadPendingPhotos(); }