Refactor: Remove background task management and related hooks

- Deleted `useBackgroundTasks.ts` hook and its associated logic for managing background tasks.
- Removed `backgroundTaskManager.ts` service and all related task definitions and registrations.
- Cleaned up `Podfile.lock` and `package.json` to remove unused dependencies related to background tasks.
- Updated iOS project files to eliminate references to removed background task components.
- Added new background fetch identifier in `Info.plist` for future use.
This commit is contained in:
richarjiang
2025-09-05 09:47:49 +08:00
parent cb89ee7bc2
commit acb3907344
60 changed files with 77 additions and 2230 deletions

View File

@@ -13,9 +13,7 @@ import { Colors } from '@/constants/Colors';
import { getTabBarBottomPadding } from '@/constants/TabBar';
import { useAppDispatch, useAppSelector } from '@/hooks/redux';
import { useAuthGuard } from '@/hooks/useAuthGuard';
import { useBackgroundTasks } from '@/hooks/useBackgroundTasks';
import { notificationService } from '@/services/notifications';
import { backgroundTaskManager } from '@/services/backgroundTaskManager';
import { selectHealthDataByDate, setHealthData } from '@/store/healthSlice';
import { fetchDailyMoodCheckins, selectLatestMoodRecordByDate } from '@/store/moodSlice';
import { fetchDailyNutritionData, selectNutritionSummaryByDate } from '@/store/nutritionSlice';
@@ -38,8 +36,8 @@ import {
ScrollView,
StyleSheet,
Text,
View,
TouchableOpacity
TouchableOpacity,
View
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
@@ -150,7 +148,6 @@ export default function ExploreScreen() {
});
}, [userProfile]);
const { registerTask, isInitialized } = useBackgroundTasks();
// 心情相关状态
const dispatch = useAppDispatch();
const [isMoodLoading, setIsMoodLoading] = useState(false);
@@ -416,49 +413,6 @@ export default function ExploreScreen() {
};
}, [loadAllData, currentSelectedDate]);
useEffect(() => {
// 只有在后台任务管理器初始化完成后才注册任务
if (isInitialized) {
console.log('后台任务管理器已初始化,开始注册健康数据任务...');
registerTask({
id: 'health-data-task',
name: 'health-data-task',
handler: async () => {
try {
console.log('后台任务:更新健康数据和检查压力水平...');
// 发送测试通知,验证后台任务是否执行
await notificationService.sendImmediateNotification({
title: '后台任务测试 🔔',
body: `任务执行时间: ${new Date().toLocaleTimeString('zh-CN')}`,
data: {
type: 'background_task_test',
timestamp: new Date().toISOString(),
},
sound: true,
priority: 'high'
});
// 后台任务只更新健康数据,强制刷新以获取最新数据
await loadHealthData(undefined, true);
// 执行压力检查
await checkStressLevelAndNotify();
// 执行喝水目标检查
await checkWaterGoalAndNotify();
} catch (error) {
console.error('健康数据任务执行失败:', error);
}
},
}).then(() => {
console.log('健康数据任务注册成功');
}).catch((error) => {
console.error('健康数据任务注册失败:', error);
});
}
}, [isInitialized]);
// 检查压力水平并发送通知
const checkStressLevelAndNotify = React.useCallback(async () => {
try {
@@ -627,7 +581,7 @@ export default function ExploreScreen() {
style={styles.debugButton}
onPress={async () => {
console.log('🔧 手动触发后台任务测试...');
await backgroundTaskManager.debugExecuteBackgroundTask();
// await backgroundTaskManager.triggerTaskForTesting();
}}
>
<Text style={styles.debugButtonText}>🔧</Text>