fix(ios): 修复HealthKit类型安全性并优化HRV通知频率
将HealthKit数据类型从强制解包改为可选类型,避免潜在的运行时崩溃。所有数据类型访问现在都通过guard语句进行安全检查,当类型不可用时返回明确的错误信息。同时修复了活动摘要日期计算错误,确保每个摘要使用正确的日期。 HRV压力通知的最小间隔从4小时缩短至2小时,并移除了每日一次的限制,允许更及时的压力状态提醒。 BREAKING CHANGE: HealthKit数据类型API现在可能返回"TYPE_NOT_AVAILABLE"错误,调用方需要处理此新错误类型
This commit is contained in:
@@ -1,16 +1,15 @@
|
||||
import { logger } from '@/utils/logger';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
import dayjs from 'dayjs';
|
||||
import { NativeEventEmitter, NativeModules } from 'react-native';
|
||||
import { analyzeHRVData, fetchHRVWithStatus } from '@/utils/health';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
import { logger } from '@/utils/logger';
|
||||
import { convertHrvToStressIndex, getStressLevelInfo, StressLevel } from '@/utils/stress';
|
||||
import { NativeEventEmitter, NativeModules } from 'react-native';
|
||||
import { sendHRVStressNotification } from './hrvNotificationService';
|
||||
|
||||
const { HealthKitManager } = NativeModules;
|
||||
|
||||
const HRV_EVENT_NAME = 'hrvUpdate';
|
||||
const HRV_NOTIFICATION_STATE_KEY = '@hrv_stress_notification_state';
|
||||
const MIN_NOTIFICATION_INTERVAL_HOURS = 4;
|
||||
const MIN_NOTIFICATION_INTERVAL_HOURS = 2;
|
||||
|
||||
interface HrvEventData {
|
||||
timestamp: number;
|
||||
@@ -137,19 +136,13 @@ class HRVMonitorService {
|
||||
const elapsed = now - state.lastSentAt;
|
||||
const minIntervalMs = MIN_NOTIFICATION_INTERVAL_HOURS * 60 * 60 * 1000;
|
||||
|
||||
// 只检查最小间隔时间(2小时),不再限制每天只能发送一次
|
||||
if (elapsed < minIntervalMs) {
|
||||
const hoursLeft = ((minIntervalMs - elapsed) / (1000 * 60 * 60)).toFixed(1);
|
||||
logger.info(`[HRVMonitor] Cooldown active, ${hoursLeft}h remaining`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const lastSentDay = dayjs(state.lastSentAt).format('YYYY-MM-DD');
|
||||
const today = dayjs().format('YYYY-MM-DD');
|
||||
if (lastSentDay === today && state.lastStressLevel !== 'high') {
|
||||
logger.info('[HRVMonitor] Already sent HRV notification today');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.warn('[HRVMonitor] Failed to read notification state:', error);
|
||||
|
||||
Reference in New Issue
Block a user