- 将目标页面改为任务列表,支持任务的创建、完成和跳过功能 - 新增任务卡片和任务进度卡片组件,展示任务状态和进度 - 实现任务数据的获取和管理,集成Redux状态管理 - 更新API服务,支持任务相关的CRUD操作 - 编写任务管理功能实现文档,详细描述功能和组件架构
114 lines
2.5 KiB
TypeScript
114 lines
2.5 KiB
TypeScript
import { Platform } from 'react-native';
|
|
import {
|
|
HKQuantityTypeIdentifier,
|
|
HKQuantitySample,
|
|
getMostRecentQuantitySample,
|
|
isAvailable,
|
|
authorize,
|
|
} from 'react-native-health';
|
|
|
|
interface HealthData {
|
|
oxygenSaturation: number | null;
|
|
heartRate: number | null;
|
|
lastUpdated: Date | null;
|
|
}
|
|
|
|
class HealthDataService {
|
|
private static instance: HealthDataService;
|
|
private isAuthorized = false;
|
|
|
|
private constructor() {}
|
|
|
|
public static getInstance(): HealthDataService {
|
|
if (!HealthDataService.instance) {
|
|
HealthDataService.instance = new HealthDataService();
|
|
}
|
|
return HealthDataService.instance;
|
|
}
|
|
|
|
async requestAuthorization(): Promise<boolean> {
|
|
if (Platform.OS !== 'ios') {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const available = await isAvailable();
|
|
if (!available) {
|
|
return false;
|
|
}
|
|
|
|
const permissions = [
|
|
{
|
|
type: HKQuantityTypeIdentifier.OxygenSaturation,
|
|
access: 'read' as const
|
|
},
|
|
{
|
|
type: HKQuantityTypeIdentifier.HeartRate,
|
|
access: 'read' as const
|
|
}
|
|
];
|
|
|
|
const authorized = await authorize(permissions);
|
|
this.isAuthorized = authorized;
|
|
return authorized;
|
|
} catch (error) {
|
|
console.error('Health data authorization error:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async getOxygenSaturation(): Promise<number | null> {
|
|
if (!this.isAuthorized) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const sample: HKQuantitySample | null = await getMostRecentQuantitySample(
|
|
HKQuantityTypeIdentifier.OxygenSaturation
|
|
);
|
|
|
|
if (sample) {
|
|
return Number(sample.value.toFixed(1));
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
console.error('Error reading oxygen saturation:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async getHeartRate(): Promise<number | null> {
|
|
if (!this.isAuthorized) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const sample: HKQuantitySample | null = await getMostRecentQuantitySample(
|
|
HKQuantityTypeIdentifier.HeartRate
|
|
);
|
|
|
|
if (sample) {
|
|
return Math.round(sample.value);
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
console.error('Error reading heart rate:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async getHealthData(): Promise<HealthData> {
|
|
const [oxygenSaturation, heartRate] = await Promise.all([
|
|
this.getOxygenSaturation(),
|
|
this.getHeartRate()
|
|
]);
|
|
|
|
return {
|
|
oxygenSaturation,
|
|
heartRate,
|
|
lastUpdated: new Date()
|
|
};
|
|
}
|
|
}
|
|
|
|
export default HealthDataService.getInstance(); |