feat: 优化数据加载逻辑,添加应用状态监听以刷新统计数据;为步数卡片添加动画效果

This commit is contained in:
2025-08-30 23:07:14 +08:00
parent 6bdfda9fd3
commit 4bb0576d92
2 changed files with 77 additions and 18 deletions

View File

@@ -26,6 +26,7 @@ import dayjs from 'dayjs';
import { LinearGradient } from 'expo-linear-gradient';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import {
AppState,
SafeAreaView,
ScrollView,
StyleSheet,
@@ -240,20 +241,42 @@ export default function ExploreScreen() {
}
};
// 加载所有数据的统一方法
const loadAllData = React.useCallback((targetDate?: Date) => {
const dateToUse = targetDate || getCurrentSelectedDate();
if (dateToUse) {
loadHealthData(dateToUse);
if (isLoggedIn) {
loadNutritionData(dateToUse);
loadMoodData(dateToUse);
}
}
}, [isLoggedIn]);
useFocusEffect(
React.useCallback(() => {
// 聚焦时当前选中日期加载,避免与用户手动选择的日期不一致
const currentDate = currentSelectedDate;
if (currentDate) {
loadHealthData(currentDate);
if (isLoggedIn) {
loadNutritionData(currentDate);
loadMoodData(currentDate);
}
}
}, [selectedIndex])
// 每次聚焦时都拉取当前选中日期的最新数据
loadAllData();
}, [loadAllData])
);
// AppState 监听:应用从后台返回前台时刷新数据
useEffect(() => {
const handleAppStateChange = (nextAppState: string) => {
if (nextAppState === 'active') {
// 应用从后台返回前台,刷新当前选中日期的数据
console.log('应用从后台返回前台,刷新统计数据...');
loadAllData();
}
};
const subscription = AppState.addEventListener('change', handleAppStateChange);
return () => {
subscription?.remove();
};
}, [loadAllData]);
useEffect(() => {
// 注册任务
registerTask({
@@ -272,11 +295,7 @@ export default function ExploreScreen() {
// 日期点击时,加载对应日期数据
const onSelectDate = (index: number, date: Date) => {
setSelectedIndex(index);
loadHealthData(date);
if (isLoggedIn) {
loadNutritionData(date);
loadMoodData(date);
}
loadAllData(date);
};