refactor(sleep): 重构睡眠数据获取逻辑,移除冗余代码并优化组件结构
- 从 healthSlice 和 health.ts 中移除 sleepDuration 字段及相关获取逻辑 - 将 SleepCard 改为按需异步获取睡眠数据,支持传入指定日期 - 睡眠详情页改为通过路由参数接收日期,支持查看历史记录 - 移除 statistics 页面对 sleepDuration 的直接依赖,统一由 SleepCard 管理 - 删除未使用的 SleepStageChart 组件,简化页面结构
This commit is contained in:
@@ -1,22 +1,40 @@
|
||||
import React from 'react';
|
||||
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
|
||||
import { fetchCompleteSleepData, formatSleepTime } from '@/utils/sleepHealthKit';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
|
||||
|
||||
interface SleepCardProps {
|
||||
sleepDuration?: number | null;
|
||||
selectedDate?: Date;
|
||||
style?: object;
|
||||
onPress?: () => void;
|
||||
}
|
||||
|
||||
const SleepCard: React.FC<SleepCardProps> = ({
|
||||
sleepDuration,
|
||||
selectedDate,
|
||||
style,
|
||||
onPress
|
||||
}) => {
|
||||
const formatSleepDuration = (duration: number): string => {
|
||||
const hours = Math.floor(duration / 60);
|
||||
const minutes = Math.floor(duration % 60);
|
||||
return `${hours}小时${minutes}分钟`;
|
||||
};
|
||||
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]}>
|
||||
@@ -24,7 +42,7 @@ const SleepCard: React.FC<SleepCardProps> = ({
|
||||
<Text style={styles.cardTitle}>睡眠</Text>
|
||||
</View>
|
||||
<Text style={styles.sleepValue}>
|
||||
{sleepDuration != null ? formatSleepDuration(sleepDuration) : '——'}
|
||||
{loading ? '加载中...' : (sleepDuration != null ? formatSleepTime(sleepDuration) : '--')}
|
||||
</Text>
|
||||
</View>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user