react-native-health-connect
Version:
React native library for health connect (Android only)
112 lines (106 loc) • 4.31 kB
JavaScript
import { NativeModules, Platform } from 'react-native';
import { HealthConnectError } from './errors';
const LINKING_ERROR = `The package 'react-native-health-connect' doesn't seem to be linked. Make sure: \n\n` + Platform.select({
ios: "- You have run 'pod install'\n",
default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
const PLATFORM_NOT_SUPPORTED_ERROR = `Platform not supported. This package only supports Android.`;
// @ts-expect-error
const isTurboModuleEnabled = global.__turboModuleProxy != null;
const moduleProxy = message => new Proxy({}, {
get() {
throw new Error(message);
}
});
const HealthConnectModule = Platform.select({
android: isTurboModuleEnabled ? require('./NativeHealthConnect').default : NativeModules.HealthConnect,
ios: moduleProxy(PLATFORM_NOT_SUPPORTED_ERROR),
default: moduleProxy(PLATFORM_NOT_SUPPORTED_ERROR)
});
const HealthConnect = HealthConnectModule ? HealthConnectModule : moduleProxy(LINKING_ERROR);
const DEFAULT_PROVIDER_PACKAGE_NAME = 'com.google.android.apps.healthdata';
/**
* Gets the status of the Health Connect SDK
* @param providerPackageName the package name of the Health Connect provider
* @returns the status of the SDK - check SdkAvailabilityStatus constants
*/
export function getSdkStatus() {
let providerPackageName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_PROVIDER_PACKAGE_NAME;
return HealthConnect.getSdkStatus(providerPackageName);
}
/**
* Initializes the Health Connect SDK
* @param providerPackageName the package name of the Health Connect provider
* @returns true if the SDK was initialized successfully
*/
export function initialize() {
let providerPackageName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_PROVIDER_PACKAGE_NAME;
return HealthConnect.initialize(providerPackageName);
}
/**
* Opens Health Connect settings app
*/
export function openHealthConnectSettings() {
return HealthConnect.openHealthConnectSettings();
}
/**
* Opens Health Connect data management screen
*/
export function openHealthConnectDataManagement(providerPackageName) {
return HealthConnect.openHealthConnectDataManagement(providerPackageName);
}
/**
* Request permissions to access Health Connect data
* @param permissions list of permissions to request
* @returns granted permissions
*/
export function requestPermission(permissions) {
return HealthConnect.requestPermission(permissions);
}
export function requestExerciseRoute(recordId) {
return HealthConnect.requestExerciseRoute(recordId);
}
export function getGrantedPermissions() {
return HealthConnect.getGrantedPermissions();
}
export function revokeAllPermissions() {
return HealthConnect.revokeAllPermissions();
}
export function readRecords(recordType, options) {
return HealthConnect.readRecords(recordType, options);
}
export function readRecord(recordType, recordId) {
return HealthConnect.readRecord(recordType, recordId);
}
export function insertRecords(records) {
if (records.length === 0) {
throw new HealthConnectError('You must provide at least one record', 'insertRecords');
}
const recordTypes = records.map(record => record.recordType);
const uniqueRecordTypes = new Set(recordTypes);
if (uniqueRecordTypes.size > 1) {
throw new HealthConnectError('All records must have the same type', 'insertRecords');
}
return HealthConnect.insertRecords(records);
}
export function aggregateRecord(request) {
return HealthConnect.aggregateRecord(request);
}
export function aggregateGroupByDuration(request) {
return HealthConnect.aggregateGroupByDuration(request);
}
export function aggregateGroupByPeriod(request) {
return HealthConnect.aggregateGroupByPeriod(request);
}
export function getChanges(request) {
return HealthConnect.getChanges(request);
}
export function deleteRecordsByUuids(recordType, recordIdsList, clientRecordIdsList) {
return HealthConnect.deleteRecordsByUuids(recordType, recordIdsList, clientRecordIdsList);
}
export function deleteRecordsByTimeRange(recordType, timeRangeFilter) {
return HealthConnect.deleteRecordsByTimeRange(recordType, timeRangeFilter);
}
export * from './constants';
export * from './types';
//# sourceMappingURL=index.js.map