feat: 支持健康数据上报
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { useAppDispatch, useAppSelector } from '@/hooks/redux';
|
||||
import { ChallengeType } from '@/services/challengesApi';
|
||||
import { WaterRecordSource } from '@/services/waterRecords';
|
||||
import { reportChallengeProgress, selectChallengeList } from '@/store/challengesSlice';
|
||||
import { createWaterRecordAction } from '@/store/waterSlice';
|
||||
import { deleteWaterIntakeFromHealthKit, getWaterIntakeFromHealthKit, saveWaterIntakeToHealthKit } from '@/utils/health';
|
||||
import { Toast } from '@/utils/toast.utils';
|
||||
import { getQuickWaterAmount, getWaterGoalFromStorage, setWaterGoalToStorage } from '@/utils/userPreferences';
|
||||
@@ -81,6 +83,9 @@ export const useWaterData = () => {
|
||||
const [waterRecords, setWaterRecords] = useState<{ [date: string]: WaterRecord[] }>({});
|
||||
const [selectedDate, setSelectedDate] = useState<string>(dayjs().format('YYYY-MM-DD'));
|
||||
|
||||
// Redux dispatch
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
// 获取指定日期的记录
|
||||
const getWaterRecordsByDate = useCallback(async (date: string, page = 1, limit = 20) => {
|
||||
setLoading(prev => ({ ...prev, records: true }));
|
||||
@@ -196,6 +201,15 @@ export const useWaterData = () => {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 同步到服务端(后台执行,不阻塞 UI)
|
||||
dispatch(createWaterRecordAction({
|
||||
amount,
|
||||
recordedAt: recordTime,
|
||||
source: WaterRecordSource.Manual,
|
||||
})).catch((err) => {
|
||||
console.warn('同步饮水记录到服务端失败:', err);
|
||||
});
|
||||
|
||||
// 重新获取当前日期的数据以刷新界面
|
||||
const updatedRecords = await getWaterRecordsByDate(date);
|
||||
const totalAmount = updatedRecords.reduce((sum, record) => sum + record.amount, 0);
|
||||
@@ -225,7 +239,7 @@ export const useWaterData = () => {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
[dailyWaterGoal, getWaterRecordsByDate, reportWaterChallengeProgress]
|
||||
[dailyWaterGoal, getWaterRecordsByDate, reportWaterChallengeProgress, dispatch]
|
||||
);
|
||||
|
||||
// 更新喝水记录(HealthKit不支持更新,只能删除后重新添加)
|
||||
@@ -554,6 +568,7 @@ export const useWaterDataByDate = (targetDate?: string) => {
|
||||
|
||||
// 创建喝水记录
|
||||
const reportWaterChallengeProgress = useWaterChallengeProgressReporter();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
const addWaterRecord = useCallback(
|
||||
async (amount: number, recordedAt?: string) => {
|
||||
@@ -567,6 +582,15 @@ export const useWaterDataByDate = (targetDate?: string) => {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 同步到服务端(后台执行,不阻塞 UI)
|
||||
dispatch(createWaterRecordAction({
|
||||
amount,
|
||||
recordedAt: recordTime,
|
||||
source: WaterRecordSource.Manual,
|
||||
})).catch((err) => {
|
||||
console.warn('同步饮水记录到服务端失败:', err);
|
||||
});
|
||||
|
||||
// 重新获取当前日期的数据以刷新界面
|
||||
const updatedRecords = await getWaterRecordsByDate(dateToUse);
|
||||
const totalAmount = updatedRecords.reduce((sum, record) => sum + record.amount, 0);
|
||||
@@ -596,7 +620,7 @@ export const useWaterDataByDate = (targetDate?: string) => {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
[dailyWaterGoal, dateToUse, getWaterRecordsByDate, reportWaterChallengeProgress]
|
||||
[dailyWaterGoal, dateToUse, getWaterRecordsByDate, reportWaterChallengeProgress, dispatch]
|
||||
);
|
||||
|
||||
// 更新喝水记录
|
||||
|
||||
Reference in New Issue
Block a user