- 从 healthSlice 和 health.ts 中移除 sleepDuration 字段及相关获取逻辑 - 将 SleepCard 改为按需异步获取睡眠数据,支持传入指定日期 - 睡眠详情页改为通过路由参数接收日期,支持查看历史记录 - 移除 statistics 页面对 sleepDuration 的直接依赖,统一由 SleepCard 管理 - 删除未使用的 SleepStageChart 组件,简化页面结构
82 lines
1.9 KiB
TypeScript
82 lines
1.9 KiB
TypeScript
import { fetchCompleteSleepData, formatSleepTime } from '@/utils/sleepHealthKit';
|
|
import React, { useEffect, useState } from 'react';
|
|
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
|
|
|
|
interface SleepCardProps {
|
|
selectedDate?: Date;
|
|
style?: object;
|
|
onPress?: () => void;
|
|
}
|
|
|
|
const SleepCard: React.FC<SleepCardProps> = ({
|
|
selectedDate,
|
|
style,
|
|
onPress
|
|
}) => {
|
|
const [sleepDuration, setSleepDuration] = useState<number | null>(null);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
// 获取睡眠数据
|
|
useEffect(() => {
|
|
const loadSleepData = async () => {
|
|
if (!selectedDate) return;
|
|
|
|
try {
|
|
setLoading(true);
|
|
const data = await fetchCompleteSleepData(selectedDate);
|
|
setSleepDuration(data?.totalSleepTime || null);
|
|
} catch (error) {
|
|
console.error('SleepCard: 获取睡眠数据失败:', error);
|
|
setSleepDuration(null);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
loadSleepData();
|
|
}, [selectedDate]);
|
|
|
|
const CardContent = (
|
|
<View style={[styles.container, style]}>
|
|
<View style={styles.cardHeaderRow}>
|
|
<Text style={styles.cardTitle}>睡眠</Text>
|
|
</View>
|
|
<Text style={styles.sleepValue}>
|
|
{loading ? '加载中...' : (sleepDuration != null ? formatSleepTime(sleepDuration) : '--')}
|
|
</Text>
|
|
</View>
|
|
);
|
|
|
|
if (onPress) {
|
|
return (
|
|
<TouchableOpacity onPress={onPress} activeOpacity={0.7}>
|
|
{CardContent}
|
|
</TouchableOpacity>
|
|
);
|
|
}
|
|
|
|
return CardContent;
|
|
};
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
// Container styles will be inherited from parent (FloatingCard)
|
|
},
|
|
cardHeaderRow: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
justifyContent: 'space-between',
|
|
},
|
|
cardTitle: {
|
|
fontSize: 14,
|
|
color: '#192126',
|
|
},
|
|
sleepValue: {
|
|
fontSize: 16,
|
|
color: '#1E40AF',
|
|
fontWeight: '700',
|
|
marginTop: 8,
|
|
},
|
|
});
|
|
|
|
export default SleepCard; |