feat: Update Podfile.lock to include NitroModules and ReactNativeHealthkit dependencies
fix: Adjust objectVersion in project.pbxproj and improve WaterWidget folder exception handling refactor: Remove sleepService.ts as part of code cleanup chore: Comment out HealthKit initialization in health.ts and clean up fetchSleepDuration function
This commit is contained in:
@@ -77,21 +77,22 @@ export async function ensureHealthPermissions(): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
console.log('开始初始化HealthKit...');
|
||||
|
||||
AppleHealthKit.initHealthKit(PERMISSIONS, (error) => {
|
||||
if (error) {
|
||||
console.error('HealthKit初始化失败:', error);
|
||||
// 常见错误处理
|
||||
if (typeof error === 'string') {
|
||||
if (error.includes('not available')) {
|
||||
console.warn('HealthKit不可用 - 可能在模拟器上运行或非iOS设备');
|
||||
}
|
||||
}
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
console.log('HealthKit初始化成功');
|
||||
resolve(true);
|
||||
});
|
||||
resolve(true)
|
||||
// AppleHealthKit.initHealthKit(PERMISSIONS, (error) => {
|
||||
// if (error) {
|
||||
// console.error('HealthKit初始化失败:', error);
|
||||
// // 常见错误处理
|
||||
// if (typeof error === 'string') {
|
||||
// if (error.includes('not available')) {
|
||||
// console.warn('HealthKit不可用 - 可能在模拟器上运行或非iOS设备');
|
||||
// }
|
||||
// }
|
||||
// resolve(false);
|
||||
// return;
|
||||
// }
|
||||
// console.log('HealthKit初始化成功');
|
||||
// resolve(true);
|
||||
// });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -445,7 +446,7 @@ async function fetchSleepDuration(date: Date): Promise<number> {
|
||||
return new Promise((resolve) => {
|
||||
// 使用睡眠专用的日期范围,包含前一天晚上的睡眠数据
|
||||
const sleepOptions = createSleepDateRange(date);
|
||||
|
||||
|
||||
AppleHealthKit.getSleepSamples(sleepOptions, (err, res) => {
|
||||
if (err) {
|
||||
logError('睡眠数据', err);
|
||||
@@ -456,23 +457,23 @@ async function fetchSleepDuration(date: Date): Promise<number> {
|
||||
return resolve(0);
|
||||
}
|
||||
logSuccess('睡眠', res);
|
||||
|
||||
|
||||
// 过滤睡眠数据,只计算主睡眠时间段
|
||||
const filteredSamples = res.filter(sample => {
|
||||
if (!sample || !sample.startDate || !sample.endDate) return false;
|
||||
|
||||
|
||||
const startDate = dayjs(sample.startDate);
|
||||
const endDate = dayjs(sample.endDate);
|
||||
const targetDate = dayjs(date);
|
||||
|
||||
|
||||
// 判断这个睡眠段是否属于当天的主睡眠
|
||||
// 睡眠段的结束时间应该在当天,或者睡眠段跨越了前一天晚上到当天早上
|
||||
const isMainSleepPeriod = endDate.isSame(targetDate, 'day') ||
|
||||
const isMainSleepPeriod = endDate.isSame(targetDate, 'day') ||
|
||||
(startDate.isBefore(targetDate, 'day') && endDate.isAfter(targetDate.startOf('day')));
|
||||
|
||||
|
||||
return isMainSleepPeriod;
|
||||
});
|
||||
|
||||
|
||||
resolve(calculateSleepDuration(filteredSamples));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user