Refactor components and enhance background task management
- Updated font sizes and weights in BasalMetabolismCard, MoodCard, HealthDataCard, and NutritionRadarCard for improved readability. - Removed loading state from MoodCard to simplify the component. - Adjusted styles in WeightHistoryCard for better layout and spacing. - Integrated expo-background-fetch for improved background task handling. - Updated Info.plist to include background fetch capability. - Enhanced background task registration and execution logic in backgroundTaskManager. - Added debug function to manually trigger background task execution for testing purposes.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import * as ExpoBackgroundTask from 'expo-background-task';
|
||||
import * as BackgroundFetch from 'expo-background-fetch';
|
||||
import * as TaskManager from 'expo-task-manager';
|
||||
|
||||
// 任务类型定义
|
||||
@@ -63,13 +63,17 @@ class BackgroundTaskManager {
|
||||
|
||||
// 注册后台任务
|
||||
private async registerBackgroundTask(): Promise<void> {
|
||||
const BACKGROUND_TASK = 'background-task';
|
||||
const BACKGROUND_FETCH_TASK = 'background-fetch-task';
|
||||
|
||||
console.log('注册后台任务');
|
||||
// 定义后台任务
|
||||
TaskManager.defineTask(BACKGROUND_TASK, async () => {
|
||||
console.log('注册后台获取任务');
|
||||
|
||||
// 定义后台获取任务
|
||||
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
|
||||
console.log('后台获取任务被系统调用');
|
||||
const now = new Date();
|
||||
|
||||
try {
|
||||
console.log('开始执行后台任务');
|
||||
console.log(`开始执行后台任务 - ${now.toISOString()}`);
|
||||
|
||||
// 执行所有注册的任务
|
||||
const results = await this.executeAllTasks();
|
||||
@@ -77,19 +81,29 @@ class BackgroundTaskManager {
|
||||
console.log('后台任务执行完成:', results);
|
||||
|
||||
// 返回成功状态
|
||||
return ExpoBackgroundTask.BackgroundTaskResult.Success;
|
||||
return BackgroundFetch.BackgroundFetchResult.NewData;
|
||||
} catch (error) {
|
||||
console.error('后台任务执行失败:', error);
|
||||
return ExpoBackgroundTask.BackgroundTaskResult.Failed;
|
||||
return BackgroundFetch.BackgroundFetchResult.Failed;
|
||||
}
|
||||
});
|
||||
|
||||
// 注册后台任务
|
||||
await ExpoBackgroundTask.registerTaskAsync(BACKGROUND_TASK, {
|
||||
minimumInterval: 15, // 最小间隔60分钟
|
||||
});
|
||||
|
||||
console.log('后台任务注册成功');
|
||||
// 注册后台获取任务
|
||||
try {
|
||||
const status = await BackgroundFetch.getStatusAsync();
|
||||
if (status === BackgroundFetch.BackgroundFetchStatus.Available) {
|
||||
await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
|
||||
minimumInterval: 15 * 60, // 15分钟(以秒为单位)
|
||||
stopOnTerminate: false,
|
||||
startOnBoot: true,
|
||||
});
|
||||
console.log('后台获取任务注册成功');
|
||||
} else {
|
||||
console.warn('后台获取不可用,状态:', status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('注册后台获取任务失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 注册自定义任务
|
||||
@@ -212,8 +226,8 @@ class BackgroundTaskManager {
|
||||
}
|
||||
|
||||
// 检查后台任务状态
|
||||
public async getBackgroundTaskStatus(): Promise<ExpoBackgroundTask.BackgroundTaskStatus | null> {
|
||||
return await ExpoBackgroundTask.getStatusAsync();
|
||||
public async getBackgroundTaskStatus(): Promise<BackgroundFetch.BackgroundFetchStatus | null> {
|
||||
return await BackgroundFetch.getStatusAsync();
|
||||
}
|
||||
|
||||
// 保存任务状态到本地存储
|
||||
@@ -254,6 +268,42 @@ class BackgroundTaskManager {
|
||||
|
||||
await this.saveTaskStatuses();
|
||||
}
|
||||
|
||||
// 调试函数:强制触发后台任务执行
|
||||
public async debugExecuteBackgroundTask(): Promise<void> {
|
||||
console.log('=== 调试:手动触发后台任务执行 ===');
|
||||
|
||||
try {
|
||||
// 获取后台任务状态
|
||||
const status = await this.getBackgroundTaskStatus();
|
||||
console.log('后台获取状态:', status);
|
||||
|
||||
// 执行所有注册的任务
|
||||
console.log('当前注册的任务数量:', this.tasks.size);
|
||||
this.tasks.forEach((task, id) => {
|
||||
console.log(`- 任务ID: ${id}, 名称: ${task.name}`);
|
||||
});
|
||||
|
||||
const results = await this.executeAllTasks();
|
||||
console.log('任务执行结果:', results);
|
||||
|
||||
// 显示任务状态
|
||||
const taskStatuses = this.getAllTaskStatuses();
|
||||
taskStatuses.forEach(status => {
|
||||
console.log(`任务 ${status.id} 状态:`, {
|
||||
isRegistered: status.isRegistered,
|
||||
executionCount: status.executionCount,
|
||||
lastExecution: status.lastExecution?.toISOString(),
|
||||
lastError: status.lastError
|
||||
});
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('调试执行失败:', error);
|
||||
}
|
||||
|
||||
console.log('=== 调试执行完成 ===');
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例实例
|
||||
|
||||
Reference in New Issue
Block a user