From fb85a5f30c57e2b6efc8ede6b6ff32429e582a68 Mon Sep 17 00:00:00 2001 From: richarjiang Date: Fri, 19 Sep 2025 17:01:45 +0800 Subject: [PATCH] refactor(health): remove basalEnergyBurned from global state and move to local component Remove basalEnergyBurned from global health data structure and refactor BasalMetabolismCard to fetch its own data locally. This decouples the component from global state and improves data locality. - Remove basalEnergyBurned from HealthData interface and health utilities - Update BasalMetabolismCard to use selectedDate prop and fetch data locally - Simplify statistics screen by removing unused basalMetabolism variable - Update nutrition radar card to use activeCalories only for burned calories calculation --- app/(tabs)/statistics.tsx | 9 ++---- components/BasalMetabolismCard.tsx | 52 +++++++++++++++++++----------- store/healthSlice.ts | 1 - utils/health.ts | 5 --- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/app/(tabs)/statistics.tsx b/app/(tabs)/statistics.tsx index 6be9e8e..2b6eb71 100644 --- a/app/(tabs)/statistics.tsx +++ b/app/(tabs)/statistics.tsx @@ -89,7 +89,6 @@ export default function ExploreScreen() { // 解构健康数据(支持mock数据) const mockData = useMockData ? getTestHealthData('mock') : null; const activeCalories = useMockData ? (mockData?.activeEnergyBurned ?? null) : (healthData?.activeEnergyBurned ?? null); - const basalMetabolism: number | null = useMockData ? (mockData?.basalEnergyBurned ?? null) : (healthData?.basalEnergyBurned ?? null); @@ -230,7 +229,6 @@ export default function ExploreScreen() { date: dateString, data: { activeCalories: data.activeEnergyBurned, - basalEnergyBurned: data.basalEnergyBurned, hrv: data.hrv, heartRate: data.heartRate, activeEnergyBurned: data.activeEnergyBurned, @@ -459,8 +457,8 @@ export default function ExploreScreen() { {/* 营养摄入雷达图卡片 */} @@ -532,8 +530,7 @@ export default function ExploreScreen() { {/* 基础代谢卡片 */} diff --git a/components/BasalMetabolismCard.tsx b/components/BasalMetabolismCard.tsx index e20cdf1..ba72a08 100644 --- a/components/BasalMetabolismCard.tsx +++ b/components/BasalMetabolismCard.tsx @@ -1,27 +1,48 @@ -import { AnimatedNumber } from '@/components/AnimatedNumber'; +import { fetchHealthDataForDate } from '@/utils/health'; import { Image } from 'expo-image'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { StyleSheet, Text, View } from 'react-native'; interface BasalMetabolismCardProps { - value: number | null; - resetToken?: number; + selectedDate?: Date; style?: any; } -export function BasalMetabolismCard({ value, resetToken, style }: BasalMetabolismCardProps) { +export function BasalMetabolismCard({ selectedDate, style }: BasalMetabolismCardProps) { + const [basalMetabolism, setBasalMetabolism] = useState(null); + const [loading, setLoading] = useState(false); + + // 获取基础代谢数据 + useEffect(() => { + const loadBasalMetabolismData = async () => { + if (!selectedDate) return; + + try { + setLoading(true); + const data = await fetchHealthDataForDate(selectedDate); + setBasalMetabolism(data?.basalEnergyBurned || null); + } catch (error) { + console.error('BasalMetabolismCard: 获取基础代谢数据失败:', error); + setBasalMetabolism(null); + } finally { + setLoading(false); + } + }; + + loadBasalMetabolismData(); + }, [selectedDate]); // 获取基础代谢状态描述 const getMetabolismStatus = () => { - if (value === null || value === 0) { + if (basalMetabolism === null || basalMetabolism === 0) { return { text: '未知', color: '#9AA3AE' }; } // 基于常见的基础代谢范围来判断状态 - if (value >= 1800) { + if (basalMetabolism >= 1800) { return { text: '高代谢', color: '#10B981' }; - } else if (value >= 1400) { + } else if (basalMetabolism >= 1400) { return { text: '正常', color: '#3B82F6' }; - } else if (value >= 1000) { + } else if (basalMetabolism >= 1000) { return { text: '偏低', color: '#F59E0B' }; } else { return { text: '较低', color: '#EF4444' }; @@ -49,16 +70,9 @@ export function BasalMetabolismCard({ value, resetToken, style }: BasalMetabolis {/* 数值显示区域 */} - {value != null && value > 0 ? ( - Math.round(v).toString()} - /> - ) : ( - -- - )} + + {loading ? '加载中...' : (basalMetabolism != null && basalMetabolism > 0 ? Math.round(basalMetabolism).toString() : '--')} + 千卡/日 diff --git a/store/healthSlice.ts b/store/healthSlice.ts index 6208095..fbc25a6 100644 --- a/store/healthSlice.ts +++ b/store/healthSlice.ts @@ -13,7 +13,6 @@ export interface FitnessRingsData { export interface HealthData { activeCalories: number | null; - basalEnergyBurned: number | null; hrv: number | null; heartRate: number | null; activeEnergyBurned: number; diff --git a/utils/health.ts b/utils/health.ts index 21ce83c..8ce53a3 100644 --- a/utils/health.ts +++ b/utils/health.ts @@ -207,7 +207,6 @@ export type HourlyStandData = { export type TodayHealthData = { activeEnergyBurned: number; // kilocalories - basalEnergyBurned: number; // kilocalories - 基础代谢率 hrv: number | null; // 心率变异性 (ms) // 健身圆环数据 activeCalories: number; @@ -608,7 +607,6 @@ export async function fetchMaximumHeartRate(_options: HealthDataOptions): Promis function getDefaultHealthData(): TodayHealthData { return { activeEnergyBurned: 0, - basalEnergyBurned: 0, hrv: null, activeCalories: 0, activeCaloriesGoal: 350, @@ -630,13 +628,11 @@ export async function fetchHealthDataForDate(date: Date): Promise