feat(nutrition): 添加营养数据保存功能到HealthKit,包括蛋白质、脂肪和碳水化合物
This commit is contained in:
@@ -10,6 +10,7 @@ import { addDietRecord, type CreateDietRecordDto } from '@/services/dietRecords'
|
||||
import { foodLibraryApi, type CreateCustomFoodDto } from '@/services/foodLibraryApi';
|
||||
import { fetchDailyNutritionData } from '@/store/nutritionSlice';
|
||||
import type { FoodItem, MealType, SelectedFoodItem } from '@/types/food';
|
||||
import { saveNutritionToHealthKit } from '@/utils/health';
|
||||
import { Ionicons } from '@expo/vector-icons';
|
||||
import { Image } from 'expo-image';
|
||||
import { useLocalSearchParams, useRouter } from 'expo-router';
|
||||
@@ -178,7 +179,27 @@ export default function FoodLibraryScreen() {
|
||||
imageUrl: item.food.imageUrl,
|
||||
};
|
||||
|
||||
// 先保存到后端
|
||||
await addDietRecord(dietRecordData);
|
||||
|
||||
// 然后尝试同步到 HealthKit(非阻塞)
|
||||
// 提取蛋白质、脂肪和碳水化合物数据
|
||||
const { proteinGrams, fatGrams, carbohydrateGrams, mealTime } = dietRecordData;
|
||||
|
||||
if (proteinGrams !== undefined || fatGrams !== undefined || carbohydrateGrams !== undefined) {
|
||||
// 使用 catch 确保 HealthKit 同步失败不影响后端记录
|
||||
saveNutritionToHealthKit(
|
||||
{
|
||||
proteinGrams: proteinGrams || undefined,
|
||||
fatGrams: fatGrams || undefined,
|
||||
carbohydrateGrams: carbohydrateGrams || undefined
|
||||
},
|
||||
mealTime
|
||||
).catch(error => {
|
||||
// HealthKit 同步失败只记录日志,不影响用户体验
|
||||
console.error('HealthKit 营养数据同步失败(不影响记录):', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 记录成功后,刷新当天的营养数据
|
||||
|
||||
Reference in New Issue
Block a user