Skip to content

Commit

Permalink
feat: finished read
Browse files Browse the repository at this point in the history
  • Loading branch information
carozo committed Dec 30, 2024
1 parent 52edb62 commit b88a947
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 45 deletions.
14 changes: 14 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.health.READ_BLOOD_GLUCOSE" />
<uses-permission android:name="android.permission.health.WRITE_BLOOD_GLUCOSE" />
<uses-permission android:name="android.permission.health.READ_BLOOD_PRESSURE" />
<uses-permission android:name="android.permission.health.WRITE_BLOOD_PRESSURE" />
<uses-permission android:name="android.permission.health.READ_HEART_RATE" />
<uses-permission android:name="android.permission.health.WRITE_HEART_RATE" />
<uses-permission android:name="android.permission.health.READ_HEIGHT" />
<uses-permission android:name="android.permission.health.WRITE_HEIGHT" />
<uses-permission android:name="android.permission.health.READ_OXYGEN_SATURATION" />
<uses-permission android:name="android.permission.health.WRITE_OXYGEN_SATURATION" />
<uses-permission android:name="android.permission.health.READ_STEPS" />
<uses-permission android:name="android.permission.health.WRITE_STEPS" />
<uses-permission android:name="android.permission.health.READ_WEIGHT" />
<uses-permission android:name="android.permission.health.WRITE_WEIGHT" />
<uses-permission android:name="android.permission.health.READ_RESTING_HEART_RATE" />
<uses-permission android:name="android.permission.health.WRITE_RESTING_HEART_RATE" />

<application
android:name=".MainApplication"
Expand Down
82 changes: 77 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,38 @@ import { View, StyleSheet, Text } from 'react-native';
import { useEffect, useState } from 'react';
import { HealthLinkPermissions } from '../../src/types/permissions';
import { HealthLinkDataType } from '../../src/types/dataTypes';
import { BloodGlucoseUnit } from '../../src/types/units';
import { initializeHealth, read } from 'react-native-health-link';
import { BloodGlucoseUnit, HeighUnit, WeightUnit } from '../../src/types/units';

export default function App() {
const [bloodGlucose, setBloodGlucose] = useState<number | null>(null);
console.log(bloodGlucose);
const [bloodGlucose, setBloodGlucose] = useState<number | undefined>();
const [weight, setWeight] = useState<number | undefined>();
const [height, setHeight] = useState<number | undefined>();
const [heartRate, setHeartRate] = useState<number | undefined>();
const [restingHeartRate, setRestingHeartRate] = useState<
number | undefined
>();
const [bloodPressure, setBloodPressure] = useState<
{ systolic: number; diastolic: number } | undefined
>();
const [oxygenSaturation, setOxygenSaturation] = useState<
number | undefined
>();
const [steps, setSteps] = useState<number | undefined>();

console.log(restingHeartRate, 'haah');
useEffect(() => {
initializeHealth({
read: [HealthLinkPermissions.BloodGlucose],
read: [
HealthLinkPermissions.BloodGlucose,
HealthLinkPermissions.Height,
HealthLinkPermissions.Weight,
HealthLinkPermissions.HeartRate,
HealthLinkPermissions.RestingHeartRate,
HealthLinkPermissions.BloodPressure,
HealthLinkPermissions.OxygenSaturation,
HealthLinkPermissions.Steps,
],
write: [HealthLinkPermissions.BloodGlucose],
}).then(() => {
read(HealthLinkDataType.BloodGlucose, {
Expand All @@ -19,11 +42,60 @@ export default function App() {
}).then((data) => {
setBloodGlucose(data[0]?.value);
});
read(HealthLinkDataType.Height, {
startDate: new Date('2021-01-01').toISOString(),
unit: HeighUnit.Cm,
}).then((data) => {
setHeight(data[0]?.value);
});
read(HealthLinkDataType.Weight, {
startDate: new Date('2021-01-01').toISOString(),
unit: WeightUnit.Kg,
}).then((data) => {
setWeight(data[0]?.value);
});
read(HealthLinkDataType.HeartRate, {
startDate: new Date('2021-01-01').toISOString(),
}).then((data) => {
setHeartRate(data[0]?.value);
});
read(HealthLinkDataType.RestingHeartRate, {
startDate: new Date('2024-12-14').toISOString(),
}).then((data) => {
setRestingHeartRate(data[0]?.value);
});
read(HealthLinkDataType.BloodPressure, {
startDate: new Date('2021-01-01').toISOString(),
}).then((data) => {
setBloodPressure(data[0]?.value);
});
read(HealthLinkDataType.OxygenSaturation, {
startDate: new Date('2021-01-01').toISOString(),
}).then((data) => {
setOxygenSaturation(data[0]?.value);
});
read(HealthLinkDataType.Steps, {
startDate: new Date('2024-12-30').toISOString(),
endDate: new Date('2024-12-31').toISOString(),
}).then((data) => {
setSteps(data[0]?.value);
});
});
});
}, []);

return (
<View style={styles.container}>
<Text>Your blood glucose is {bloodGlucose}</Text>
<Text>Your weight is {weight}</Text>
<Text>Your height is {height}</Text>
<Text>Your heart rate is {heartRate}</Text>
<Text>Your resting heart rate is {restingHeartRate}</Text>
<Text>
Your blood pressure is {bloodPressure?.systolic}/
{bloodPressure?.diastolic}
</Text>
<Text>Your oxygen saturation is {oxygenSaturation}</Text>
<Text>Your steps today are {steps}</Text>
</View>
);
}
Expand Down
14 changes: 9 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
type ReadOptions,
} from './types/dataTypes';
import type { HealthValue } from 'react-native-health';
import { readDataResultDeserializer, readIosCallback } from './types/results';
import {
readDataResultDeserializer,
readIosCallback,
type HealthLinkDataValue,
} from './types/results';

const AppleHealthKit = require('react-native-health');

Expand All @@ -34,11 +38,11 @@ export const initializeHealth = async (permissions: HealthPermissions) => {
}
};

export const read = async (
dataType: HealthLinkDataType,
export const read = async <T extends HealthLinkDataType>(
dataType: T,
options: ReadOptions
) => {
let data: ReadRecordsResult<any> | HealthValue[] = [];
): Promise<Array<HealthLinkDataValue<T>>> => {
let data: ReadRecordsResult<T> | HealthValue[] = [];
if (Platform.OS === 'ios') {
data = (await readIosCallback(dataType, options)) as HealthValue[];
} else if (Platform.OS === 'android') {
Expand Down
4 changes: 3 additions & 1 deletion src/types/dataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ export enum HealthLinkDataType {
BloodGlucose = 'BloodGlucose',
Height = 'Height',
Weight = 'Weight',
LeanBodyMass = 'LeanBodyMass',
HeartRate = 'HeartRate',
RestingHeartRate = 'RestingHeartRate',
BloodPressure = 'BloodPressure',
OxygenSaturation = 'OxygenSaturation',
Steps = 'Steps',
}

export type TimeOperator = 'after' | 'before' | 'between';
Expand Down
22 changes: 17 additions & 5 deletions src/types/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ export enum HealthLinkPermissions {
BloodGlucose = 'BloodGlucose',
Height = 'Height',
Weight = 'Weight',
LeanBodyMass = 'LeanBodyMass',
HeartRate = 'HeartRate',
RestingHeartRate = 'RestingHeartRate',
BloodPressure = 'BloodPressure',
OxygenSaturation = 'OxygenSaturation',
Steps = 'Steps',
}

export const genericToIosPermissions = (
permissions: HealthPermissions
): HealthKitPermissions => {
const readPermissions = permissions.read.reduce<AppleHealthPermission[]>(
(acc, permission) => {
if (Array.isArray(genericToIosMap[permission])) {
acc.push(...(genericToIosMap[permission] as AppleHealthPermission[]));
}
if (genericToIosMap[permission]) {
acc.push(genericToIosMap[permission] as AppleHealthPermission);
}
Expand Down Expand Up @@ -71,17 +76,22 @@ export const genericToAndroidPermissions = (
return androidPermissions;
};

const genericToIosMap: { [key: string]: string } = {
const genericToIosMap: { [key: string]: string | string[] } = {
[HealthLinkPermissions.BloodGlucose]:
AppleHealthKit.Constants.Permissions.BloodGlucose,
[HealthLinkPermissions.Height]: AppleHealthKit.Constants.Permissions.Height,
[HealthLinkPermissions.Weight]: AppleHealthKit.Constants.Permissions.Weight,
[HealthLinkPermissions.LeanBodyMass]:
AppleHealthKit.Constants.Permissions.LeanBodyMass,
[HealthLinkPermissions.HeartRate]:
AppleHealthKit.Constants.Permissions.HeartRate,
[HealthLinkPermissions.RestingHeartRate]:
AppleHealthKit.Constants.Permissions.RestingHeartRate,
[HealthLinkPermissions.BloodPressure]: [
AppleHealthKit.Constants.Permissions.BloodPressureDiastolic,
AppleHealthKit.Constants.Permissions.BloodPressureSystolic,
],
[HealthLinkPermissions.OxygenSaturation]:
AppleHealthKit.Constants.Permissions.OxygenSaturation,
[HealthLinkPermissions.Steps]: AppleHealthKit.Constants.Permissions.Steps,
};

const genericToAndroidMap: {
Expand All @@ -90,7 +100,9 @@ const genericToAndroidMap: {
[HealthLinkPermissions.BloodGlucose]: 'BloodGlucose',
[HealthLinkPermissions.Height]: 'Height',
[HealthLinkPermissions.Weight]: 'Weight',
[HealthLinkPermissions.LeanBodyMass]: 'LeanBodyMass',
[HealthLinkPermissions.HeartRate]: 'HeartRate',
[HealthLinkPermissions.RestingHeartRate]: 'RestingHeartRate',
[HealthLinkPermissions.BloodPressure]: 'BloodPressure',
[HealthLinkPermissions.OxygenSaturation]: 'OxygenSaturation',
[HealthLinkPermissions.Steps]: 'Steps',
};
Loading

0 comments on commit b88a947

Please sign in to comment.