feat: 支持原生模块健康数据

This commit is contained in:
richarjiang
2025-09-18 09:51:37 +08:00
parent 6b7776e51d
commit 6f0c872223
115 changed files with 1791 additions and 2851 deletions

View File

@@ -10,10 +10,10 @@ import {
updateWaterGoalAction,
updateWaterRecordAction,
} from '@/store/waterSlice';
import { deleteWaterIntakeFromHealthKit, saveWaterIntakeToHealthKit } from '@/utils/health';
import { Toast } from '@/utils/toast.utils';
import { saveWaterIntakeToHealthKit, deleteWaterIntakeFromHealthKit } from '@/utils/health';
import { syncWaterDataToWidget, refreshWidget } from '@/utils/widgetDataSync';
import { getQuickWaterAmount } from '@/utils/userPreferences';
import { refreshWidget, syncWaterDataToWidget } from '@/utils/widgetDataSync';
import dayjs from 'dayjs';
import { useCallback, useEffect, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
@@ -89,7 +89,7 @@ export const useWaterData = () => {
try {
await dispatch(createWaterRecordAction(dto)).unwrap();
// 同步到 HealthKit
try {
const healthKitSuccess = await saveWaterIntakeToHealthKit(amount, recordedAt);
@@ -100,27 +100,27 @@ export const useWaterData = () => {
console.error('HealthKit 同步错误:', healthError);
// HealthKit 同步失败不影响主要功能
}
// 重新获取今日统计并等待完成
const updatedStats = await dispatch(fetchTodayWaterStats()).unwrap();
// 同步数据到Widget
try {
const quickAddAmount = await getQuickWaterAmount();
await syncWaterDataToWidget({
currentIntake: updatedStats.totalAmount,
dailyGoal: updatedStats.dailyGoal,
quickAddAmount,
});
// 刷新Widget
await refreshWidget();
} catch (widgetError) {
console.error('Widget 同步错误:', widgetError);
// Widget 同步失败不影响主要功能
}
return true;
} catch (error: any) {
console.error('添加喝水记录失败:', error);
@@ -160,17 +160,17 @@ export const useWaterData = () => {
await dispatch(updateWaterRecordAction(dto)).unwrap();
// 重新获取今日统计并等待完成
const updatedStats = await dispatch(fetchTodayWaterStats()).unwrap();
// 同步数据到Widget
try {
const quickAddAmount = await getQuickWaterAmount();
await syncWaterDataToWidget({
currentIntake: updatedStats.totalAmount,
dailyGoal: updatedStats.dailyGoal,
quickAddAmount,
});
// 刷新Widget
await refreshWidget();
} catch (widgetError) {
@@ -197,9 +197,9 @@ export const useWaterData = () => {
try {
// 在删除前,尝试获取记录信息用于 HealthKit 同步
const recordToDelete = waterRecords.find(record => record.id === id);
await dispatch(deleteWaterRecordAction(id)).unwrap();
// 同步删除到 HealthKit
if (recordToDelete) {
try {
@@ -215,20 +215,20 @@ export const useWaterData = () => {
// HealthKit 同步失败不影响主要功能
}
}
// 重新获取今日统计并等待完成
const updatedStats = await dispatch(fetchTodayWaterStats()).unwrap();
// 同步数据到Widget
try {
const quickAddAmount = await getQuickWaterAmount();
await syncWaterDataToWidget({
currentIntake: updatedStats.totalAmount,
dailyGoal: updatedStats.dailyGoal,
quickAddAmount,
});
// 刷新Widget
await refreshWidget();
} catch (widgetError) {
@@ -254,10 +254,10 @@ export const useWaterData = () => {
const updateWaterGoal = useCallback(async (goal: number) => {
try {
await dispatch(updateWaterGoalAction(goal)).unwrap();
// 重新获取今日统计以确保数据一致性
const updatedStats = await dispatch(fetchTodayWaterStats()).unwrap();
// 同步目标到Widget
try {
const quickAddAmount = await getQuickWaterAmount();
@@ -270,7 +270,7 @@ export const useWaterData = () => {
} catch (widgetError) {
console.error('Widget 目标同步错误:', widgetError);
}
return true;
} catch (error: any) {
console.error('更新喝水目标失败:', error);
@@ -566,7 +566,7 @@ export const useWaterDataByDate = (targetDate?: string) => {
try {
// 在删除前,尝试获取记录信息用于 HealthKit 同步
const recordToDelete = waterRecords.find(record => record.id === id);
await dispatch(deleteWaterRecordAction(id)).unwrap();
// 同步删除到 HealthKit