react-native-healthkit-bridge
Version:
A comprehensive React Native bridge for Apple HealthKit with TypeScript support, advanced authorization, and flexible data queries
61 lines (60 loc) • 3.07 kB
JavaScript
import { HealthKitMapper } from '../mappers/HealthKitMapper';
export class HealthKitRepositoryImpl {
constructor(dataSource, config = {}) {
this.dataSource = dataSource;
this.mapper = new HealthKitMapper();
this.config = Object.assign({ defaultDays: 7, enableLogging: false, retryAttempts: 3 }, config);
}
async requestAuthorization() {
return await this.dataSource.requestAuthorization();
}
async requestSelectiveAuthorization(readTypes, writeTypes = []) {
const identifierStrings = [...readTypes, ...writeTypes].map(id => id.toString());
return await this.dataSource.requestSelectiveAuthorization(readTypes.map(id => id.toString()), writeTypes.map(id => id.toString()));
}
async getAuthorizationStatus(identifiers) {
const identifierStrings = identifiers.map(id => id.toString());
return await this.dataSource.getAuthorizationStatus(identifierStrings);
}
async getQuantitySamplesWithRange(identifier, unit, startDate, endDate) {
return await this.dataSource.getQuantitySamplesWithRange(identifier.toString(), unit, startDate, endDate);
}
async getCategorySamplesWithRange(identifier, startDate, endDate) {
const rawData = await this.dataSource.getCategorySamplesWithRange(identifier.toString(), startDate, endDate);
return rawData.map(item => this.mapper.toCategorySample(item));
}
async getWorkoutsWithRange(startDate, endDate) {
const rawData = await this.dataSource.getWorkoutsWithRange(startDate, endDate);
return rawData.map(item => this.mapper.toWorkout(item));
}
async getQuantitySamples(identifier, unit) {
const rawData = await this.dataSource.getQuantitySamplesForDays(identifier.toString(), unit.toString(), 7);
return rawData.map(item => this.mapper.toQuantitySample(item));
}
async getCategorySamples(identifier) {
const rawData = await this.dataSource.getCategorySamplesForDays(identifier.toString(), 7);
return rawData.map(item => this.mapper.toCategorySample(item));
}
async getWorkouts() {
const rawData = await this.dataSource.getWorkoutsForDays(7);
return rawData.map(item => this.mapper.toWorkout(item));
}
async getQuantitySamplesForDays(identifier, unit, days) {
const rawData = await this.dataSource.getQuantitySamplesForDays(identifier.toString(), unit.toString(), days);
return rawData.map(item => this.mapper.toQuantitySample(item));
}
async getCategorySamplesForDays(identifier, days) {
const rawData = await this.dataSource.getCategorySamplesForDays(identifier.toString(), days);
return rawData.map(item => this.mapper.toCategorySample(item));
}
async getWorkoutsForDays(days) {
const rawData = await this.dataSource.getWorkoutsForDays(days);
return rawData.map(item => this.mapper.toWorkout(item));
}
isAvailable() {
return this.dataSource.isAvailable();
}
formatDate(date) {
return date.toISOString();
}
}