diff --git a/example/src/App.tsx b/example/src/App.tsx index 3c0a160..1722a07 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -3,6 +3,7 @@ import { read, useHealth } from '../../src/useHealth'; import { useEffect, useState } from 'react'; import { HealthLinkPermissions } from '../../src/types/permissions'; import { HealthLinkDataType } from '../../src/types/dataTypes'; +import { BloodGlucoseUnit } from '../../src/types/units'; export default function App() { const [bloodGlucose, setBloodGlucose] = useState(null); @@ -14,7 +15,7 @@ export default function App() { write: [HealthLinkPermissions.BloodGlucose], }).then(() => { read(HealthLinkDataType.BloodGlucose, { - unit: 'mg/dL', + unit: BloodGlucoseUnit.MmolPerL, startDate: new Date('2021-01-01').toISOString(), }).then((data) => { setBloodGlucose(data); @@ -23,10 +24,7 @@ export default function App() { }); return ( - - Your blood glucose is{' '} - {bloodGlucose?.records[0].level.inMillimolesPerLiter} - + Your blood glucose is {bloodGlucose} ); } diff --git a/src/types/units.ts b/src/types/units.ts index 25db00b..5911413 100644 --- a/src/types/units.ts +++ b/src/types/units.ts @@ -1,4 +1,4 @@ export enum BloodGlucoseUnit { - MgPerdL = 'mg/dL', - MmolPerL = 'mmol/L', + MgPerdL = 'mgPerdL', + MmolPerL = 'mmolPerL', } diff --git a/src/useHealth.ts b/src/useHealth.ts index d21de17..4866873 100644 --- a/src/useHealth.ts +++ b/src/useHealth.ts @@ -98,10 +98,10 @@ const readDataResultDeserializer = ( return (data: any) => { switch (dataType) { case 'BloodGlucose': - if (options.unit === BloodGlucoseUnit.MmolPerL) { - return data[0]?.level?.inMillimolesPerLiter; - } else { + if (options.unit === BloodGlucoseUnit.MgPerdL) { return data[0]?.level?.inMilligramsPerDeciliter; + } else { + return data[0]?.level?.inMillimolesPerLiter; } case 'Height': return data[0]?.height?.inMeters; @@ -112,21 +112,28 @@ const readDataResultDeserializer = ( } }; +const iosCallback = (dataType: HealthLinkDataType, options: ReadOptions) => { + return new Promise((resolve, reject) => { + dataValueToIosReadFunction(dataType)(options, (err: any, results: any) => { + if (err) { + reject(err); + } + resolve(results); + }); + }); +}; + export const read = async ( dataType: HealthLinkDataType, - options: { - unit?: string; - startDate?: string; - endDate?: string; - ascending?: boolean; - limit?: number; - } + options: ReadOptions ) => { + let data; if (Platform.OS === 'ios') { - return dataValueToIosReadFunction(dataType)(options); + data = await iosCallback(dataType, options); } else if (Platform.OS === 'android') { - return readRecords(dataType, optionsToAndroidOptions(options)); + data = readRecords(dataType, optionsToAndroidOptions(options)); } + return readDataResultDeserializer(dataType, options)?.(data); }; export const useHealth = () => {