254 lines
7.4 KiB
TypeScript
254 lines
7.4 KiB
TypeScript
import { BackgroundTaskType as BackgroundTask, backgroundTaskManager } from './backgroundTaskManager';
|
||
|
||
// 示例任务:数据同步任务
|
||
export const createDataSyncTask = (): BackgroundTask => ({
|
||
id: 'data-sync-task',
|
||
name: '数据同步任务',
|
||
handler: async (data?: any) => {
|
||
console.log('开始执行数据同步任务');
|
||
|
||
try {
|
||
// 这里实现您的数据同步逻辑
|
||
// 例如:同步用户数据、运动记录、目标进度等
|
||
|
||
// 模拟数据同步过程
|
||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||
|
||
console.log('数据同步任务执行完成');
|
||
} catch (error) {
|
||
console.error('数据同步任务执行失败:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
options: {
|
||
minimumInterval: 5, // 5分钟最小间隔
|
||
stopOnTerminate: false,
|
||
startOnBoot: true,
|
||
},
|
||
});
|
||
|
||
// 示例任务:健康数据更新任务
|
||
export const createHealthDataUpdateTask = (): BackgroundTask => ({
|
||
id: 'health-data-update-task',
|
||
name: '健康数据更新任务',
|
||
handler: async (data?: any) => {
|
||
console.log('开始执行健康数据更新任务');
|
||
|
||
try {
|
||
// 这里实现您的健康数据更新逻辑
|
||
// 例如:更新步数、心率、体重等健康数据
|
||
|
||
// 模拟健康数据更新过程
|
||
await new Promise(resolve => setTimeout(resolve, 1500));
|
||
|
||
console.log('健康数据更新任务执行完成');
|
||
} catch (error) {
|
||
console.error('健康数据更新任务执行失败:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
options: {
|
||
minimumInterval: 10, // 10分钟最小间隔
|
||
stopOnTerminate: false,
|
||
startOnBoot: true,
|
||
},
|
||
});
|
||
|
||
// 示例任务:通知检查任务
|
||
export const createNotificationCheckTask = (): BackgroundTask => ({
|
||
id: 'notification-check-task',
|
||
name: '通知检查任务',
|
||
handler: async (data?: any) => {
|
||
console.log('开始执行通知检查任务');
|
||
|
||
try {
|
||
// 这里实现您的通知检查逻辑
|
||
// 例如:检查是否需要发送运动提醒、目标达成通知等
|
||
|
||
// 模拟通知检查过程
|
||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||
|
||
console.log('通知检查任务执行完成');
|
||
} catch (error) {
|
||
console.error('通知检查任务执行失败:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
options: {
|
||
minimumInterval: 30, // 30分钟最小间隔
|
||
stopOnTerminate: false,
|
||
startOnBoot: true,
|
||
},
|
||
});
|
||
|
||
// 喝水提醒检查任务
|
||
export const createWaterReminderTask = (): BackgroundTask => ({
|
||
id: 'water-reminder-task',
|
||
name: '喝水提醒检查任务',
|
||
handler: async (data?: any) => {
|
||
console.log('开始执行喝水提醒检查任务');
|
||
|
||
try {
|
||
// 导入必要的模块
|
||
const { WaterNotificationHelpers } = await import('@/utils/notificationHelpers');
|
||
const { getTodayWaterStats } = await import('@/services/waterRecords');
|
||
const AsyncStorage = (await import('@react-native-async-storage/async-storage')).default;
|
||
|
||
// 获取用户信息
|
||
const userProfileJson = await AsyncStorage.getItem('@user_profile');
|
||
const userProfile = userProfileJson ? JSON.parse(userProfileJson) : null;
|
||
const userName = userProfile?.name || '朋友';
|
||
|
||
// 检查时间限制:早上9点以前和晚上9点以后不通知
|
||
const currentHour = new Date().getHours();
|
||
if (currentHour < 9 || currentHour >= 21) {
|
||
console.log(`当前时间${currentHour}点,不在通知时间范围内(9:00-21:00),跳过喝水提醒检查`);
|
||
return;
|
||
}
|
||
|
||
// 获取今日喝水统计数据
|
||
let todayStats;
|
||
try {
|
||
todayStats = await getTodayWaterStats();
|
||
} catch (error) {
|
||
console.log('获取喝水统计数据失败,可能用户未登录或无网络连接:', error);
|
||
return;
|
||
}
|
||
|
||
if (!todayStats || !todayStats.dailyGoal || todayStats.dailyGoal <= 0) {
|
||
console.log('没有设置喝水目标或目标无效,跳过喝水检查');
|
||
return;
|
||
}
|
||
|
||
// 构造今日统计数据
|
||
const waterStatsForCheck = {
|
||
totalAmount: todayStats.totalAmount || 0,
|
||
dailyGoal: todayStats.dailyGoal,
|
||
completionRate: todayStats.completionRate || 0
|
||
};
|
||
|
||
// 调用喝水通知检查函数
|
||
const notificationSent = await WaterNotificationHelpers.checkWaterGoalAndNotify(
|
||
userName,
|
||
waterStatsForCheck,
|
||
currentHour
|
||
);
|
||
|
||
if (notificationSent) {
|
||
console.log('喝水提醒通知已发送');
|
||
} else {
|
||
console.log('无需发送喝水提醒通知');
|
||
}
|
||
|
||
console.log('喝水提醒检查任务执行完成');
|
||
} catch (error) {
|
||
console.error('喝水提醒检查任务执行失败:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
options: {
|
||
minimumInterval: 60, // 60分钟最小间隔
|
||
stopOnTerminate: false,
|
||
startOnBoot: true,
|
||
},
|
||
});
|
||
|
||
// 示例任务:缓存清理任务
|
||
export const createCacheCleanupTask = (): BackgroundTask => ({
|
||
id: 'cache-cleanup-task',
|
||
name: '缓存清理任务',
|
||
handler: async (data?: any) => {
|
||
console.log('开始执行缓存清理任务');
|
||
|
||
try {
|
||
// 这里实现您的缓存清理逻辑
|
||
// 例如:清理过期的图片缓存、临时文件等
|
||
|
||
// 模拟缓存清理过程
|
||
await new Promise(resolve => setTimeout(resolve, 3000));
|
||
|
||
console.log('缓存清理任务执行完成');
|
||
} catch (error) {
|
||
console.error('缓存清理任务执行失败:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
options: {
|
||
minimumInterval: 86400, // 24小时最小间隔
|
||
stopOnTerminate: false,
|
||
startOnBoot: true,
|
||
},
|
||
});
|
||
|
||
// 示例任务:用户行为分析任务
|
||
export const createUserAnalyticsTask = (): BackgroundTask => ({
|
||
id: 'user-analytics-task',
|
||
name: '用户行为分析任务',
|
||
handler: async (data?: any) => {
|
||
console.log('开始执行用户行为分析任务');
|
||
|
||
try {
|
||
// 这里实现您的用户行为分析逻辑
|
||
// 例如:分析用户运动习惯、使用模式等
|
||
|
||
// 模拟用户行为分析过程
|
||
await new Promise(resolve => setTimeout(resolve, 2500));
|
||
|
||
console.log('用户行为分析任务执行完成');
|
||
} catch (error) {
|
||
console.error('用户行为分析任务执行失败:', error);
|
||
throw error;
|
||
}
|
||
},
|
||
options: {
|
||
minimumInterval: 60, // 1小时最小间隔
|
||
stopOnTerminate: false,
|
||
startOnBoot: true,
|
||
},
|
||
});
|
||
|
||
// 注册所有默认任务
|
||
export const registerDefaultTasks = async (): Promise<void> => {
|
||
try {
|
||
const tasks = [
|
||
createDataSyncTask(),
|
||
createHealthDataUpdateTask(),
|
||
createNotificationCheckTask(),
|
||
createWaterReminderTask(),
|
||
createCacheCleanupTask(),
|
||
createUserAnalyticsTask(),
|
||
];
|
||
|
||
for (const task of tasks) {
|
||
await backgroundTaskManager.registerTask(task);
|
||
}
|
||
|
||
console.log('所有默认任务注册完成');
|
||
} catch (error) {
|
||
console.error('注册默认任务失败:', error);
|
||
throw error;
|
||
}
|
||
};
|
||
|
||
// 创建自定义任务的工厂函数
|
||
export const createCustomTask = (
|
||
id: string,
|
||
name: string,
|
||
handler: (data?: any) => Promise<void>,
|
||
options?: {
|
||
minimumInterval?: number;
|
||
stopOnTerminate?: boolean;
|
||
startOnBoot?: boolean;
|
||
}
|
||
): BackgroundTask => ({
|
||
id,
|
||
name,
|
||
handler,
|
||
options: {
|
||
minimumInterval: 300, // 默认5分钟
|
||
stopOnTerminate: false,
|
||
startOnBoot: true,
|
||
...options,
|
||
},
|
||
});
|