@kingstinct/react-native-healthkit 5.1.2 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +28 -23
  2. package/android/.project +18 -1
  3. package/android/.settings/org.eclipse.buildship.core.prefs +4 -4
  4. package/ios/ReactNativeHealthkit.m +9 -0
  5. package/ios/ReactNativeHealthkit.swift +23 -7
  6. package/lib/commonjs/hooks/useHealthkitAuthorization.js.map +1 -1
  7. package/lib/commonjs/hooks/useHealthkitAuthorization.test.js +57 -0
  8. package/lib/commonjs/hooks/useHealthkitAuthorization.test.js.map +1 -0
  9. package/lib/commonjs/hooks/useIsHealthDataAvailable.test.js +12 -19
  10. package/lib/commonjs/hooks/useIsHealthDataAvailable.test.js.map +1 -1
  11. package/lib/commonjs/index.ios.js +1 -0
  12. package/lib/commonjs/index.ios.js.map +1 -1
  13. package/lib/commonjs/index.js +3 -1
  14. package/lib/commonjs/index.js.map +1 -1
  15. package/lib/commonjs/jest.setup.js +29 -2
  16. package/lib/commonjs/jest.setup.js.map +1 -1
  17. package/lib/commonjs/native-types.js +37 -30
  18. package/lib/commonjs/native-types.js.map +1 -1
  19. package/lib/commonjs/test-utils.js +21 -0
  20. package/lib/commonjs/test-utils.js.map +1 -0
  21. package/lib/commonjs/utils/getPreferredUnit.js.map +1 -1
  22. package/lib/commonjs/utils/subscribeToChanges.js +4 -4
  23. package/lib/commonjs/utils/subscribeToChanges.js.map +1 -1
  24. package/lib/module/hooks/useHealthkitAuthorization.js.map +1 -1
  25. package/lib/module/hooks/useHealthkitAuthorization.test.js +45 -0
  26. package/lib/module/hooks/useHealthkitAuthorization.test.js.map +1 -0
  27. package/lib/module/hooks/useIsHealthDataAvailable.test.js +8 -16
  28. package/lib/module/hooks/useIsHealthDataAvailable.test.js.map +1 -1
  29. package/lib/module/index.ios.js +1 -0
  30. package/lib/module/index.ios.js.map +1 -1
  31. package/lib/module/index.js +3 -1
  32. package/lib/module/index.js.map +1 -1
  33. package/lib/module/jest.setup.js +29 -2
  34. package/lib/module/jest.setup.js.map +1 -1
  35. package/lib/module/native-types.js +37 -30
  36. package/lib/module/native-types.js.map +1 -1
  37. package/lib/module/test-utils.js +13 -0
  38. package/lib/module/test-utils.js.map +1 -0
  39. package/lib/module/utils/getPreferredUnit.js.map +1 -1
  40. package/lib/module/utils/subscribeToChanges.js +1 -2
  41. package/lib/module/utils/subscribeToChanges.js.map +1 -1
  42. package/lib/{src/types.js → typescript/src/hooks/useHealthkitAuthorization.test.d.ts} +0 -0
  43. package/lib/typescript/src/index.ios.d.ts +1 -0
  44. package/lib/typescript/src/native-types.d.ts +60 -51
  45. package/lib/typescript/src/test-utils.d.ts +2 -0
  46. package/lib/typescript/src/utils/getPreferredUnit.d.ts +1 -1
  47. package/package.json +8 -4
  48. package/src/hooks/useHealthkitAuthorization.test.ts +54 -0
  49. package/src/hooks/useHealthkitAuthorization.ts +1 -0
  50. package/src/hooks/useIsHealthDataAvailable.test.ts +9 -14
  51. package/src/index.ios.tsx +3 -1
  52. package/src/index.tsx +2 -0
  53. package/src/jest.setup.ts +32 -2
  54. package/src/native-types.ts +364 -162
  55. package/src/test-utils.ts +10 -0
  56. package/src/utils/getPreferredUnit.ts +2 -2
  57. package/src/utils/subscribeToChanges.ts +1 -2
  58. package/lib/example/App.js +0 -197
  59. package/lib/index.ios.js +0 -310
  60. package/lib/index.js +0 -44
  61. package/lib/native-types.js +0 -447
  62. package/lib/src/index.ios.js +0 -314
  63. package/lib/src/index.js +0 -45
  64. package/lib/src/native-types.js +0 -453
  65. package/lib/types.js +0 -1
@@ -0,0 +1,10 @@
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
+ import { act } from '@testing-library/react-native'
3
+
4
+ const waitForNextUpdate = async () => {
5
+ await act(async () => {
6
+ await new Promise((resolve) => { setTimeout(resolve, 0) })
7
+ })
8
+ }
9
+
10
+ export default waitForNextUpdate
@@ -4,11 +4,11 @@ import type { HKQuantityTypeIdentifier, HKUnit } from '../native-types'
4
4
 
5
5
  export type GetPreferredUnitFn = (
6
6
  identifier: HKQuantityTypeIdentifier
7
- ) => Promise<HKUnit>;
7
+ ) => Promise<HKUnit | undefined>;
8
8
 
9
9
  const getPreferredUnit: GetPreferredUnitFn = async (type) => {
10
10
  const [unit] = await getPreferredUnits([type])
11
- return unit!
11
+ return unit
12
12
  }
13
13
 
14
14
  export default getPreferredUnit
@@ -1,5 +1,4 @@
1
- import { EventEmitter } from '..'
2
- import Native from '../native-types'
1
+ import Native, { EventEmitter } from '../native-types'
3
2
 
4
3
  import type { HKSampleTypeIdentifier } from '..'
5
4
 
@@ -1,197 +0,0 @@
1
- /* eslint-disable react-native/no-inline-styles */
2
- import 'expo-dev-client';
3
- import * as React from 'react';
4
- import { Button, ScrollView, Text } from 'react-native';
5
- import { DataTable } from 'react-native-paper';
6
- import dayjs from 'dayjs';
7
- import Healthkit, { HKCategoryTypeIdentifier, HKCharacteristicTypeIdentifier, HKCorrelationTypeIdentifier, HKInsulinDeliveryReason, HKQuantityTypeIdentifier, HKStatisticsOptions, HKUnit, HKWeatherCondition, HKWorkoutActivityType, } from '../src/index'; // this way we can work with the working copy - but keep in mind native changes requires a new build 🚀
8
- const DisplayWorkout = ({ workout }) => {
9
- return (React.createElement(DataTable.Row, null,
10
- React.createElement(DataTable.Cell, null, HKWorkoutActivityType[workout.workoutActivityType]),
11
- React.createElement(DataTable.Cell, { style: { paddingRight: 10 }, numeric: true }, workout ? workout.duration.toFixed(0) + 's' : '-'),
12
- React.createElement(DataTable.Cell, null, workout
13
- ? workout.totalDistance?.quantity.toFixed(1) +
14
- ' ' +
15
- workout.totalDistance?.unit
16
- : '-'),
17
- React.createElement(DataTable.Cell, null, workout
18
- ? workout.totalEnergyBurned?.quantity.toFixed(1) +
19
- ' ' +
20
- workout.totalEnergyBurned?.unit
21
- : '-')));
22
- };
23
- const DisplayQuantitySample = ({ title, sample }) => {
24
- return (React.createElement(DataTable.Row, null,
25
- React.createElement(DataTable.Cell, null, title),
26
- React.createElement(DataTable.Cell, { style: { paddingRight: 10 }, numeric: true }, sample ? sample.quantity.toFixed(1) : '-'),
27
- React.createElement(DataTable.Cell, null, sample ? sample.unit : '-'),
28
- React.createElement(DataTable.Cell, null, sample ? sample.startDate.toLocaleTimeString() : '-')));
29
- };
30
- const DisplayCategorySample = ({ title, sample }) => {
31
- return (React.createElement(DataTable.Row, null,
32
- React.createElement(DataTable.Cell, null, title),
33
- React.createElement(DataTable.Cell, { style: { paddingRight: 10 }, numeric: true }, sample ? sample.value : '-'),
34
- React.createElement(DataTable.Cell, null, sample ? sample.startDate.toLocaleTimeString() : '-'),
35
- React.createElement(DataTable.Cell, null, sample ? sample.endDate.toLocaleTimeString() : '-')));
36
- };
37
- const DisplayStat = ({ title, sample }) => {
38
- return (React.createElement(DataTable.Row, null,
39
- React.createElement(DataTable.Cell, null, title),
40
- React.createElement(DataTable.Cell, { style: { paddingRight: 10 }, numeric: true }, sample ? sample.quantity.toFixed(1) : '-'),
41
- React.createElement(DataTable.Cell, null, sample ? sample.unit : '-'),
42
- React.createElement(DataTable.Cell, null, "N/A")));
43
- };
44
- function DataView() {
45
- const [dateOfBirth, setDateOfBirth] = React.useState(null);
46
- const [bloodGlucoseSamples, setBloodGlucoseSamples] = React.useState(null);
47
- const bodyFat = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.bodyFatPercentage);
48
- const bloodGlucose = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.bloodGlucose);
49
- const bodyWeight = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.bodyMass);
50
- const heartRate = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.heartRate);
51
- const lastWorkout = Healthkit.useMostRecentWorkout();
52
- const lastMindfulSession = Healthkit.useMostRecentCategorySample(HKCategoryTypeIdentifier.mindfulSession);
53
- const walkingSpeed = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.walkingSpeed);
54
- const sixMinWalk = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.sixMinuteWalkTestDistance);
55
- const walkingStepLength = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.walkingStepLength);
56
- const walkingAsymmetryPercentage = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.walkingAsymmetryPercentage);
57
- const walkingDoubleSupportPercentage = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.walkingDoubleSupportPercentage);
58
- const stairAscentSpeed = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.stairAscentSpeed);
59
- const stairDescentSpeed = Healthkit.useMostRecentQuantitySample(HKQuantityTypeIdentifier.stairDescentSpeed);
60
- const [queryStatisticsResponse, setQueryStatisticsResponse] = React.useState(null);
61
- const writeSampleToHealthkit = () => {
62
- Healthkit.saveQuantitySample(HKQuantityTypeIdentifier.insulinDelivery, HKUnit.InternationalUnit, 4.2, {
63
- metadata: {
64
- HKInsulinDeliveryReason: HKInsulinDeliveryReason.basal,
65
- },
66
- });
67
- Healthkit.saveCorrelationSample(HKCorrelationTypeIdentifier.food, [
68
- {
69
- quantityType: HKQuantityTypeIdentifier.dietaryCaffeine,
70
- unit: HKUnit.Grams,
71
- quantity: 1,
72
- metadata: {},
73
- },
74
- {
75
- quantityType: HKQuantityTypeIdentifier.dietaryEnergyConsumed,
76
- unit: HKUnit.Kilocalories,
77
- quantity: 1,
78
- metadata: {},
79
- },
80
- ]);
81
- Healthkit.saveWorkoutSample(HKWorkoutActivityType.archery, [
82
- {
83
- quantityType: HKQuantityTypeIdentifier.activeEnergyBurned,
84
- unit: HKUnit.Kilocalories,
85
- quantity: 63,
86
- metadata: {},
87
- },
88
- {
89
- quantityType: HKQuantityTypeIdentifier.appleExerciseTime,
90
- unit: HKUnit.Minutes,
91
- quantity: 11,
92
- metadata: {},
93
- },
94
- ], new Date(), {
95
- metadata: {
96
- HKWeatherCondition: HKWeatherCondition.hurricane,
97
- },
98
- });
99
- Healthkit.getDateOfBirth().then(setDateOfBirth);
100
- Healthkit.queryStatisticsForQuantity(HKQuantityTypeIdentifier.heartRate, [
101
- HKStatisticsOptions.discreteAverage,
102
- HKStatisticsOptions.discreteMax,
103
- HKStatisticsOptions.discreteMin,
104
- ], dayjs().startOf('day').toDate()).then(setQueryStatisticsResponse);
105
- Healthkit.queryQuantitySamples(HKQuantityTypeIdentifier.bloodGlucose, {
106
- ascending: true,
107
- from: dayjs().startOf('day').toDate(),
108
- to: new Date(),
109
- }).then(setBloodGlucoseSamples);
110
- };
111
- console.log(walkingDoubleSupportPercentage);
112
- return (React.createElement(ScrollView, { style: { flex: 1, paddingTop: 40 } },
113
- React.createElement(Button, { title: "Write Sample to HealthKit", onPress: () => writeSampleToHealthkit() }),
114
- React.createElement(Text, null,
115
- "Date of birth: ",
116
- dateOfBirth?.toLocaleDateString()),
117
- React.createElement(DataTable, null,
118
- React.createElement(DataTable.Header, null,
119
- React.createElement(DataTable.Title, null, "Metric"),
120
- React.createElement(DataTable.Title, { style: { paddingRight: 10 }, numeric: true }, "Value"),
121
- React.createElement(DataTable.Title, null, "Unit"),
122
- React.createElement(DataTable.Title, null, "Time")),
123
- React.createElement(DisplayQuantitySample, { sample: bodyFat, title: "Body fat" }),
124
- React.createElement(DisplayQuantitySample, { sample: bodyWeight, title: "Weight" }),
125
- React.createElement(DisplayQuantitySample, { sample: heartRate, title: "Heart rate" }),
126
- React.createElement(DisplayQuantitySample, { sample: bloodGlucose, title: "Glucose" }),
127
- React.createElement(DisplayStat, { sample: queryStatisticsResponse?.averageQuantity, title: "Avg. HR" }),
128
- React.createElement(DisplayStat, { sample: queryStatisticsResponse?.maximumQuantity, title: "High HR" }),
129
- React.createElement(DisplayStat, { sample: queryStatisticsResponse?.minimumQuantity, title: "Low HR" }),
130
- React.createElement(DisplayCategorySample, { sample: lastMindfulSession, title: "Mindful" }),
131
- React.createElement(DataTable.Header, null,
132
- React.createElement(DataTable.Title, null, "Workout"),
133
- React.createElement(DataTable.Title, { style: { paddingRight: 10 }, numeric: true }, "Duration"),
134
- React.createElement(DataTable.Title, null, "Distance"),
135
- React.createElement(DataTable.Title, null, "Energy")),
136
- lastWorkout ? React.createElement(DisplayWorkout, { workout: lastWorkout }) : null,
137
- React.createElement(DataTable.Header, null,
138
- React.createElement(DataTable.Title, null, "Blood Glucose"),
139
- React.createElement(DataTable.Title, { style: { paddingRight: 10 }, numeric: true }, "Value"),
140
- React.createElement(DataTable.Title, null, "Units"),
141
- React.createElement(DataTable.Title, null, "Time")),
142
- bloodGlucoseSamples
143
- ? bloodGlucoseSamples.map((sample) => (React.createElement(DisplayQuantitySample, { sample: sample, title: "Glucose" })))
144
- : null,
145
- React.createElement(DataTable.Header, null,
146
- React.createElement(DataTable.Title, null, "Mobility"),
147
- React.createElement(DataTable.Title, { style: { paddingRight: 10 }, numeric: true }, "Value"),
148
- React.createElement(DataTable.Title, null, "Units"),
149
- React.createElement(DataTable.Title, null, "Time")),
150
- React.createElement(DisplayQuantitySample, { sample: walkingSpeed, title: "Walking speed" }),
151
- React.createElement(DisplayQuantitySample, { sample: sixMinWalk, title: "Six-minute walk test" }),
152
- React.createElement(DisplayQuantitySample, { sample: walkingStepLength, title: "Walking Step Length" }),
153
- React.createElement(DisplayQuantitySample, { sample: walkingAsymmetryPercentage, title: "Walking Asymmetry" }),
154
- React.createElement(DisplayQuantitySample, { sample: walkingDoubleSupportPercentage, title: "Walking Double Support" }),
155
- React.createElement(DisplayQuantitySample, { sample: stairAscentSpeed, title: "Stair Ascent" }),
156
- React.createElement(DisplayQuantitySample, { sample: stairDescentSpeed, title: "Stair Descent" }))));
157
- }
158
- const App = () => {
159
- const [hasPermissions, setHasPermissions] = React.useState(false);
160
- React.useEffect(() => {
161
- Healthkit.requestAuthorization([
162
- HKCharacteristicTypeIdentifier.biologicalSex,
163
- HKCharacteristicTypeIdentifier.bloodType,
164
- HKCharacteristicTypeIdentifier.dateOfBirth,
165
- HKCharacteristicTypeIdentifier.fitzpatrickSkinType,
166
- HKQuantityTypeIdentifier.waistCircumference,
167
- HKQuantityTypeIdentifier.bodyMassIndex,
168
- HKQuantityTypeIdentifier.bodyMass,
169
- HKQuantityTypeIdentifier.heartRate,
170
- HKQuantityTypeIdentifier.bloodGlucose,
171
- HKQuantityTypeIdentifier.insulinDelivery,
172
- HKQuantityTypeIdentifier.activeEnergyBurned,
173
- HKCategoryTypeIdentifier.mindfulSession,
174
- HKQuantityTypeIdentifier.dietaryCaffeine,
175
- HKQuantityTypeIdentifier.dietaryEnergyConsumed,
176
- HKQuantityTypeIdentifier.walkingSpeed,
177
- HKQuantityTypeIdentifier.walkingAsymmetryPercentage,
178
- HKQuantityTypeIdentifier.walkingDoubleSupportPercentage,
179
- HKQuantityTypeIdentifier.stairAscentSpeed,
180
- HKQuantityTypeIdentifier.stairDescentSpeed,
181
- HKQuantityTypeIdentifier.walkingStepLength,
182
- 'HKWorkoutTypeIdentifier',
183
- ], [
184
- HKQuantityTypeIdentifier.waistCircumference,
185
- HKQuantityTypeIdentifier.activeEnergyBurned,
186
- HKQuantityTypeIdentifier.bloodGlucose,
187
- HKQuantityTypeIdentifier.insulinDelivery,
188
- HKQuantityTypeIdentifier.bodyFatPercentage,
189
- HKCategoryTypeIdentifier.mindfulSession,
190
- HKQuantityTypeIdentifier.dietaryCaffeine,
191
- HKQuantityTypeIdentifier.dietaryEnergyConsumed,
192
- 'HKWorkoutTypeIdentifier',
193
- ]).then(setHasPermissions);
194
- }, []);
195
- return hasPermissions ? (React.createElement(DataView, null)) : (React.createElement(Text, { style: { paddingTop: 40, textAlign: 'center' } }, "Waiting for user to authorize.."));
196
- };
197
- export default App;
package/lib/index.ios.js DELETED
@@ -1,310 +0,0 @@
1
- import { useState, useEffect, useCallback } from 'react';
2
- import Native, { EventEmitter, HKQuantityTypeIdentifier, HKUnit, } from './native-types';
3
- const getPreferredUnit = async (type) => {
4
- const [unit] = await getPreferredUnits([type]);
5
- return unit;
6
- };
7
- const ensureUnit = async (type, providedUnit) => {
8
- if (providedUnit) {
9
- return providedUnit;
10
- }
11
- const unit = await Native.getPreferredUnits([type]);
12
- return unit[type];
13
- };
14
- function deserializeSample(sample) {
15
- return {
16
- ...sample,
17
- startDate: new Date(sample.startDate),
18
- endDate: new Date(sample.endDate),
19
- };
20
- }
21
- function deserializeWorkout(sample) {
22
- return {
23
- ...sample,
24
- startDate: new Date(sample.startDate),
25
- endDate: new Date(sample.endDate),
26
- };
27
- }
28
- const deserializCategorySample = (sample) => {
29
- return {
30
- ...sample,
31
- startDate: new Date(sample.startDate),
32
- endDate: new Date(sample.endDate),
33
- };
34
- };
35
- const serializeDate = (date) => {
36
- return date ? date.toISOString() : new Date(0).toISOString();
37
- };
38
- const prepareOptions = (options) => {
39
- const limit = !options.limit || options.limit === Infinity ? 0 : options.limit;
40
- const ascending = options.ascending ?? limit === 0;
41
- const from = serializeDate(options.from);
42
- const to = serializeDate(options.to);
43
- return { limit, ascending, from, to };
44
- };
45
- const queryQuantitySamples = async (identifier, options) => {
46
- const unit = await ensureUnit(identifier, options.unit);
47
- const opts = prepareOptions(options);
48
- const quantitySamples = await Native.queryQuantitySamples(identifier, unit, opts.from, opts.to, opts.limit, opts.ascending);
49
- return quantitySamples.map(deserializeSample);
50
- };
51
- const subscribeToChanges = async (identifier, callback) => {
52
- const subscription = EventEmitter.addListener('onChange', ({ typeIdentifier }) => {
53
- if (typeIdentifier === identifier) {
54
- callback();
55
- }
56
- });
57
- const queryId = await Native.subscribeToObserverQuery(identifier).catch((error) => {
58
- subscription.remove();
59
- return Promise.reject(error);
60
- });
61
- return () => {
62
- subscription.remove();
63
- return Native.unsubscribeQuery(queryId);
64
- };
65
- };
66
- const getMostRecentQuantitySample = async (identifier, unit) => {
67
- const samples = await queryQuantitySamples(identifier, {
68
- limit: 1,
69
- unit: unit,
70
- });
71
- return samples[0];
72
- };
73
- function useMostRecentWorkout(options) {
74
- const [workout, setWorkout] = useState(null);
75
- useEffect(() => {
76
- let cancelSubscription;
77
- const init = async () => {
78
- const { energyUnit, distanceUnit } = await getPreferredUnitsTyped(options);
79
- cancelSubscription = await subscribeToChanges('HKWorkoutTypeIdentifier', () => {
80
- getMostRecentWorkout({ energyUnit, distanceUnit }).then(setWorkout);
81
- });
82
- };
83
- init();
84
- return () => {
85
- cancelSubscription && cancelSubscription();
86
- };
87
- }, [options]);
88
- return workout;
89
- }
90
- const getMostRecentCategorySample = async (identifier) => {
91
- const samples = await queryCategorySamples(identifier, {
92
- limit: 1,
93
- ascending: false,
94
- });
95
- return samples[0];
96
- };
97
- function useMostRecentCategorySample(identifier) {
98
- const [category, setCategory] = useState(null);
99
- const updater = useCallback(() => {
100
- getMostRecentCategorySample(identifier).then(setCategory);
101
- }, [identifier]);
102
- useSubscribeToChanges(identifier, updater);
103
- return category;
104
- }
105
- function useSubscribeToChanges(identifier, onChange) {
106
- useEffect(() => {
107
- let cancelSubscription;
108
- const init = async () => {
109
- cancelSubscription = await subscribeToChanges(identifier, onChange);
110
- };
111
- init();
112
- return () => {
113
- cancelSubscription && cancelSubscription();
114
- };
115
- }, [identifier, onChange]);
116
- }
117
- function useMostRecentQuantitySample(identifier, unit) {
118
- const [lastSample, setLastSample] = useState(null);
119
- useEffect(() => {
120
- let cancelSubscription;
121
- const init = async () => {
122
- const actualUnit = await ensureUnit(identifier, unit);
123
- cancelSubscription = await subscribeToChanges(identifier, () => {
124
- getMostRecentQuantitySample(identifier, actualUnit).then((value) => {
125
- setLastSample(value);
126
- });
127
- });
128
- };
129
- init();
130
- return () => {
131
- cancelSubscription && cancelSubscription();
132
- };
133
- }, [identifier, unit]);
134
- return lastSample;
135
- }
136
- const saveQuantitySample = (identifier, unit, value, options) => {
137
- const start = options?.start || options?.end || new Date();
138
- const end = options?.end || options?.start || new Date();
139
- const metadata = options?.metadata || {};
140
- return Native.saveQuantitySample(identifier, unit, value, start.toISOString(), end.toISOString(), metadata);
141
- };
142
- const queryStatisticsForQuantity = async (identifier, options, from, to, unit) => {
143
- const actualUnit = await ensureUnit(identifier, unit);
144
- const toDate = to || new Date();
145
- const { mostRecentQuantityDateInterval, ...rawResponse } = await Native.queryStatisticsForQuantity(identifier, actualUnit, from.toISOString(), toDate.toISOString(), options);
146
- const response = {
147
- ...rawResponse,
148
- ...(mostRecentQuantityDateInterval
149
- ? {
150
- mostRecentQuantityDateInterval: {
151
- from: new Date(mostRecentQuantityDateInterval.from),
152
- to: new Date(mostRecentQuantityDateInterval.to),
153
- },
154
- }
155
- : {}),
156
- };
157
- return response;
158
- };
159
- const requestAuthorization = (read, write = []) => {
160
- const readPermissions = read.reduce((obj, cur) => {
161
- return { ...obj, [cur]: true };
162
- }, {});
163
- const writePermissions = write.reduce((obj, cur) => {
164
- return { ...obj, [cur]: true };
165
- }, {});
166
- return Native.requestAuthorization(writePermissions, readPermissions);
167
- };
168
- const getDateOfBirth = async () => {
169
- const dateOfBirth = await Native.getDateOfBirth();
170
- return new Date(dateOfBirth);
171
- };
172
- const getRequestStatusForAuthorization = (read, write = []) => {
173
- const readPermissions = read.reduce((obj, cur) => {
174
- return { ...obj, [cur]: true };
175
- }, {});
176
- const writePermissions = write.reduce((obj, cur) => {
177
- return { ...obj, [cur]: true };
178
- }, {});
179
- return Native.getRequestStatusForAuthorization(writePermissions, readPermissions);
180
- };
181
- const queryCategorySamples = async (identifier, options) => {
182
- const opts = prepareOptions(options);
183
- const results = await Native.queryCategorySamples(identifier, opts.from, opts.to, opts.limit, opts.ascending);
184
- return results.map(deserializCategorySample);
185
- };
186
- async function getPreferredUnitsTyped(options) {
187
- let energyUnit = options?.energyUnit;
188
- let distanceUnit = options?.distanceUnit;
189
- if (!energyUnit || !distanceUnit) {
190
- const units = await Native.getPreferredUnits([
191
- HKQuantityTypeIdentifier.distanceWalkingRunning,
192
- HKQuantityTypeIdentifier.activeEnergyBurned,
193
- ]);
194
- if (!energyUnit) {
195
- energyUnit = units[HKQuantityTypeIdentifier.distanceWalkingRunning];
196
- }
197
- if (!distanceUnit) {
198
- distanceUnit = units[HKQuantityTypeIdentifier.activeEnergyBurned];
199
- }
200
- }
201
- if (!energyUnit) {
202
- energyUnit = HKUnit.Kilocalories;
203
- }
204
- if (!distanceUnit) {
205
- distanceUnit = HKUnit.Meters;
206
- }
207
- return { energyUnit, distanceUnit };
208
- }
209
- const queryWorkouts = async (options) => {
210
- const { energyUnit, distanceUnit } = await getPreferredUnitsTyped(options);
211
- const opts = prepareOptions(options);
212
- const workouts = await Native.queryWorkoutSamples(energyUnit, distanceUnit, opts.from, opts.to, opts.limit, opts.ascending);
213
- return workouts.map(deserializeWorkout);
214
- };
215
- const getMostRecentWorkout = async (options) => {
216
- const workouts = await queryWorkouts({
217
- limit: 1,
218
- ascending: false,
219
- energyUnit: options?.energyUnit,
220
- distanceUnit: options?.distanceUnit,
221
- });
222
- return workouts[0];
223
- };
224
- function saveCategorySample(identifier, value, options) {
225
- const start = options?.start || options?.end || new Date();
226
- const end = options?.end || options?.start || new Date();
227
- const metadata = options?.metadata || {};
228
- return Native.saveCategorySample(identifier, value, start.toISOString(), end.toISOString(), metadata || {});
229
- }
230
- const getPreferredUnits = async (identifiers) => {
231
- const units = await Native.getPreferredUnits(identifiers);
232
- return identifiers.map((i) => units[i]);
233
- };
234
- const buildUnitWithPrefix = (prefix, unit) => {
235
- return `${prefix}${unit}`;
236
- };
237
- function deserializeCorrelation(s) {
238
- return {
239
- ...s,
240
- objects: s.objects.map((o) => {
241
- // @ts-ignore
242
- if (o.quantity !== undefined) {
243
- return deserializeSample(o);
244
- }
245
- return deserializCategorySample(o);
246
- }),
247
- endDate: new Date(s.endDate),
248
- startDate: new Date(s.startDate),
249
- };
250
- }
251
- function ensureMetadata(metadata) {
252
- return metadata || {};
253
- }
254
- const queryCorrelationSamples = async (typeIdentifier, options) => {
255
- const opts = prepareOptions(options);
256
- const correlations = await Native.queryCorrelationSamples(typeIdentifier, opts.from, opts.to);
257
- return correlations.map(deserializeCorrelation);
258
- };
259
- const saveCorrelationSample = async (typeIdentifier, samples, options) => {
260
- const start = (options?.start || new Date()).toISOString();
261
- const end = (options?.end || new Date()).toISOString();
262
- return Native.saveCorrelationSample(typeIdentifier, samples, start, end, ensureMetadata(options?.metadata));
263
- };
264
- const saveWorkoutSample = (typeIdentifier, quantities, _start, options) => {
265
- const start = _start.toISOString();
266
- const end = (options?.end || new Date()).toISOString();
267
- return Native.saveWorkoutSample(typeIdentifier, quantities, start, end, ensureMetadata(options?.metadata));
268
- };
269
- const Healthkit = {
270
- authorizationStatusFor: Native.authorizationStatusFor,
271
- isHealthDataAvailable: Native.isHealthDataAvailable,
272
- buildUnitWithPrefix,
273
- disableAllBackgroundDelivery: Native.disableAllBackgroundDelivery,
274
- disableBackgroundDelivery: Native.disableBackgroundDelivery,
275
- enableBackgroundDelivery: Native.enableBackgroundDelivery,
276
- // simple convenience getters
277
- getBiologicalSex: Native.getBiologicalSex,
278
- getFitzpatrickSkinType: Native.getFitzpatrickSkinType,
279
- getWheelchairUse: Native.getWheelchairUse,
280
- getBloodType: Native.getBloodType,
281
- getDateOfBirth,
282
- getMostRecentQuantitySample,
283
- getMostRecentCategorySample,
284
- getMostRecentWorkout,
285
- getPreferredUnit,
286
- getPreferredUnits,
287
- getRequestStatusForAuthorization,
288
- // query methods
289
- queryCategorySamples,
290
- queryCorrelationSamples,
291
- queryQuantitySamples,
292
- queryStatisticsForQuantity,
293
- queryWorkouts,
294
- requestAuthorization,
295
- // save methods
296
- saveCategorySample,
297
- saveCorrelationSample,
298
- saveQuantitySample,
299
- saveWorkoutSample,
300
- // subscriptions
301
- subscribeToChanges,
302
- // hooks
303
- useMostRecentCategorySample,
304
- useMostRecentQuantitySample,
305
- useMostRecentWorkout,
306
- useSubscribeToChanges,
307
- };
308
- export * from './types';
309
- export * from './native-types';
310
- export default Healthkit;
package/lib/index.js DELETED
@@ -1,44 +0,0 @@
1
- import { Platform } from 'react-native';
2
- const notAvailableError = 'Platform "' +
3
- Platform.OS +
4
- '" not supported, use isHealthDataAvailable to check for availability before using it';
5
- const UnavailableFn = () => {
6
- throw new Error(notAvailableError);
7
- };
8
- const Healthkit = {
9
- authorizationStatusFor: UnavailableFn,
10
- buildUnitWithPrefix: UnavailableFn,
11
- disableAllBackgroundDelivery: UnavailableFn,
12
- disableBackgroundDelivery: UnavailableFn,
13
- enableBackgroundDelivery: UnavailableFn,
14
- getBiologicalSex: UnavailableFn,
15
- getBloodType: UnavailableFn,
16
- getDateOfBirth: UnavailableFn,
17
- getFitzpatrickSkinType: UnavailableFn,
18
- getMostRecentCategorySample: UnavailableFn,
19
- getMostRecentQuantitySample: UnavailableFn,
20
- getMostRecentWorkout: UnavailableFn,
21
- getPreferredUnit: UnavailableFn,
22
- getPreferredUnits: UnavailableFn,
23
- getRequestStatusForAuthorization: UnavailableFn,
24
- getWheelchairUse: UnavailableFn,
25
- isHealthDataAvailable: () => Promise.resolve(false),
26
- queryCategorySamples: UnavailableFn,
27
- queryCorrelationSamples: UnavailableFn,
28
- queryQuantitySamples: UnavailableFn,
29
- queryStatisticsForQuantity: UnavailableFn,
30
- queryWorkouts: UnavailableFn,
31
- requestAuthorization: UnavailableFn,
32
- saveCategorySample: UnavailableFn,
33
- saveCorrelationSample: UnavailableFn,
34
- saveQuantitySample: UnavailableFn,
35
- saveWorkoutSample: UnavailableFn,
36
- subscribeToChanges: UnavailableFn,
37
- useMostRecentCategorySample: UnavailableFn,
38
- useMostRecentQuantitySample: UnavailableFn,
39
- useMostRecentWorkout: UnavailableFn,
40
- useSubscribeToChanges: UnavailableFn,
41
- };
42
- export * from './types';
43
- export * from './native-types';
44
- export default Healthkit;