Skip to content

Commit

Permalink
wip read records
Browse files Browse the repository at this point in the history
  • Loading branch information
carozo committed Dec 27, 2024
1 parent 1af1638 commit da8cbab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
8 changes: 3 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<number | null>(null);
Expand All @@ -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);
Expand All @@ -23,10 +24,7 @@ export default function App() {
});
return (
<View style={styles.container}>
<Text>
Your blood glucose is{' '}
{bloodGlucose?.records[0].level.inMillimolesPerLiter}
</Text>
<Text>Your blood glucose is {bloodGlucose}</Text>
</View>
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/units.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum BloodGlucoseUnit {
MgPerdL = 'mg/dL',
MmolPerL = 'mmol/L',
MgPerdL = 'mgPerdL',
MmolPerL = 'mmolPerL',
}
31 changes: 19 additions & 12 deletions src/useHealth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = () => {
Expand Down

0 comments on commit da8cbab

Please sign in to comment.