feat: 更新统计标签和标题,优化健康数据卡片样式,调整步数和健康相关组件的样式

This commit is contained in:
2025-08-30 22:37:27 +08:00
parent f4dd40ed46
commit 6bdfda9fd3
11 changed files with 68 additions and 118 deletions

View File

@@ -162,39 +162,11 @@ async function fetchStepCount(date: Date): Promise<number> {
});
}
// 获取指定日期每小时步数数据
// 获取指定日期每小时步数数据 (已弃用,使用 fetchHourlyStepSamples 替代)
// 保留此函数以防后向兼容需求
async function fetchHourlyStepCount(date: Date): Promise<HourlyStepData[]> {
return new Promise((resolve) => {
const startOfDay = dayjs(date).startOf('day');
const endOfDay = dayjs(date).endOf('day');
AppleHealthKit.getStepCount({
startDate: startOfDay.toDate().toISOString(),
endDate: endOfDay.toDate().toISOString(),
includeManuallyAdded: false,
}, (err, res) => {
if (err) {
logError('每小时步数', err);
return resolve(Array.from({ length: 24 }, (_, i) => ({ hour: i, steps: 0 })));
}
if (!res || !Array.isArray(res) || res.length === 0) {
logWarning('每小时步数', '为空');
return resolve(Array.from({ length: 24 }, (_, i) => ({ hour: i, steps: 0 })));
}
logSuccess('每小时步数', res);
// 初始化24小时数据
const hourlyData: HourlyStepData[] = Array.from({ length: 24 }, (_, i) => ({
hour: i,
steps: 0
}));
// 如果返回的是累计数据,我们需要获取样本数据
resolve(hourlyData);
});
});
// 直接调用更准确的样本数据获取函数
return fetchHourlyStepSamples(date);
}
// 使用样本数据获取每小时步数
@@ -202,42 +174,48 @@ async function fetchHourlyStepSamples(date: Date): Promise<HourlyStepData[]> {
return new Promise((resolve) => {
const startOfDay = dayjs(date).startOf('day');
const endOfDay = dayjs(date).endOf('day');
AppleHealthKit.getSamples(
{
startDate: startOfDay.toDate().toISOString(),
endDate: endOfDay.toDate().toISOString(),
type: 'StepCount',
},
// 使用正确的 getDailyStepCountSamples 方法,设置 period 为 60 分钟获取每小时数据
const options = {
startDate: startOfDay.toDate().toISOString(),
endDate: endOfDay.toDate().toISOString(),
ascending: false,
period: 60, // 60分钟为一个时间段获取每小时数据
includeManuallyAdded: false,
};
AppleHealthKit.getDailyStepCountSamples(
options,
(err: any, res: any[]) => {
if (err) {
logError('每小时步数样本', err);
return resolve(Array.from({ length: 24 }, (_, i) => ({ hour: i, steps: 0 })));
// 如果主方法失败,尝试使用备用方法
return null
}
if (!res || !Array.isArray(res) || res.length === 0) {
logWarning('每小时步数样本', '为空');
return resolve(Array.from({ length: 24 }, (_, i) => ({ hour: i, steps: 0 })));
}
logSuccess('每小时步数样本', res);
// 初始化24小时数据
const hourlyData: HourlyStepData[] = Array.from({ length: 24 }, (_, i) => ({
hour: i,
steps: 0
const hourlyData: HourlyStepData[] = Array.from({ length: 24 }, (_, i) => ({
hour: i,
steps: 0
}));
// 将样本数据按小时分组并累加
// 将每小时的步数样本数据映射到对应的小时
res.forEach((sample: any) => {
if (sample && sample.startDate && sample.value) {
if (sample && sample.startDate && sample.value !== undefined) {
const hour = dayjs(sample.startDate).hour();
if (hour >= 0 && hour < 24) {
hourlyData[hour].steps += Math.round(sample.value);
// 使用样本中的步数值,如果有 metadata优先使用 metadata 中的数据
const stepValue = sample.metadata && sample.metadata.length > 0
? sample.metadata.reduce((total: number, meta: any) => total + (meta.quantity || 0), 0)
: sample.value;
hourlyData[hour].steps = Math.round(stepValue);
}
}
});
resolve(hourlyData);
}
);