feat(widget): 增强Widget数据同步机制并优化UI设计

- 在useWaterData中统一处理数据变更后的Widget同步逻辑
- 新增数组类型数据存取方法支持更复杂数据结构
- 重构Widget UI为圆形进度条设计,提升视觉体验
- 修复数据同步时可能存在的竞态条件问题
- 优化错误处理,确保Widget同步失败不影响主功能
This commit is contained in:
richarjiang
2025-09-11 10:38:54 +08:00
parent 62690ee3fc
commit 35d6b74451
5 changed files with 148 additions and 55 deletions

View File

@@ -101,17 +101,16 @@ export const useWaterData = () => {
// HealthKit 同步失败不影响主要功能
}
// 重新获取今日统计
dispatch(fetchTodayWaterStats());
// 重新获取今日统计并等待完成
const updatedStats = await dispatch(fetchTodayWaterStats()).unwrap();
// 同步数据到Widget
try {
const newCurrentIntake = (todayStats?.totalAmount || 0) + amount;
const quickAddAmount = await getQuickWaterAmount();
await syncWaterDataToWidget({
currentIntake: newCurrentIntake,
dailyGoal: dailyWaterGoal || 2000,
currentIntake: updatedStats.totalAmount,
dailyGoal: updatedStats.dailyGoal,
quickAddAmount,
});
@@ -159,8 +158,25 @@ export const useWaterData = () => {
try {
await dispatch(updateWaterRecordAction(dto)).unwrap();
// 重新获取今日统计
dispatch(fetchTodayWaterStats());
// 重新获取今日统计并等待完成
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) {
@@ -200,8 +216,25 @@ export const useWaterData = () => {
}
}
// 重新获取今日统计
dispatch(fetchTodayWaterStats());
// 重新获取今日统计并等待完成
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) {
@@ -222,12 +255,15 @@ export const useWaterData = () => {
try {
await dispatch(updateWaterGoalAction(goal)).unwrap();
// 重新获取今日统计以确保数据一致性
const updatedStats = await dispatch(fetchTodayWaterStats()).unwrap();
// 同步目标到Widget
try {
const quickAddAmount = await getQuickWaterAmount();
await syncWaterDataToWidget({
dailyGoal: goal,
currentIntake: todayStats?.totalAmount || 0,
currentIntake: updatedStats.totalAmount,
quickAddAmount,
});
await refreshWidget();