@kingstinct/react-native-healthkit 4.2.0 → 4.3.0
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.
- package/README.md +1 -1
- package/ios/ReactNativeHealthkit.m +6 -1
- package/ios/ReactNativeHealthkit.swift +363 -174
- package/lib/commonjs/index.ios.js +9 -4
- package/lib/commonjs/index.ios.js.map +1 -1
- package/lib/commonjs/index.js +9 -8
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/native-types.js +10 -1
- package/lib/commonjs/native-types.js.map +1 -1
- package/lib/commonjs/types.js +4 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/example/App.js +197 -0
- package/lib/index.ios.js +310 -0
- package/lib/index.js +44 -0
- package/lib/module/index.ios.js +9 -4
- package/lib/module/index.ios.js.map +1 -1
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/native-types.js +9 -1
- package/lib/module/native-types.js.map +1 -1
- package/lib/module/types.js +1 -1
- package/lib/module/types.js.map +1 -1
- package/lib/native-types.js +447 -0
- package/lib/src/index.ios.js +314 -0
- package/lib/src/index.js +45 -0
- package/lib/src/native-types.js +453 -0
- package/lib/src/types.js +1 -0
- package/lib/types.js +1 -0
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/index.ios.d.ts +1 -1
- package/lib/typescript/src/native-types.d.ts +22 -1
- package/lib/typescript/src/types.d.ts +3 -1
- package/package.json +1 -1
- package/src/index.ios.tsx +9 -2
- package/src/index.tsx +2 -1
- package/src/native-types.ts +26 -2
- package/src/types.ts +8 -2
- package/.DS_Store +0 -0
- package/.circleci/config.yml +0 -98
- package/.editorconfig +0 -15
- package/.gitattributes +0 -3
- package/.github/workflows/main.yml +0 -32
- package/.gitignore +0 -60
- package/CONTRIBUTING.md +0 -184
- package/babel.config.js +0 -3
- package/example-expo/.expo/README.md +0 -15
- package/example-expo/.expo/devices.json +0 -8
- package/example-expo/.expo/packager-info.json +0 -5
- package/example-expo/.expo/settings.json +0 -9
- package/example-expo/.expo-shared/assets.json +0 -4
- package/example-expo/.gitignore +0 -14
- package/example-expo/App.tsx +0 -376
- package/example-expo/app.json +0 -43
- package/example-expo/assets/adaptive-icon.png +0 -0
- package/example-expo/assets/favicon.png +0 -0
- package/example-expo/assets/icon.png +0 -0
- package/example-expo/assets/splash.png +0 -0
- package/example-expo/babel.config.js +0 -8
- package/example-expo/build-1653040579600.ipa +0 -0
- package/example-expo/build-1653041063216.ipa +0 -0
- package/example-expo/eas.json +0 -18
- package/example-expo/package.json +0 -32
- package/example-expo/tsconfig.json +0 -6
- package/example-expo/yarn.lock +0 -6857
- package/lib/typescript/example-expo/App.d.ts +0 -3
- package/lib/typescript/src/__tests__/index.test.d.ts +0 -0
- package/src/__tests__/index.test.tsx +0 -1
- package/tsconfig.json +0 -27
- package/yarn.lock +0 -10241
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } 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 getWorkoutRoutes = (workoutUUID) => {
|
|
270
|
+
return Native.getWorkoutRoutes(workoutUUID);
|
|
271
|
+
};
|
|
272
|
+
const Healthkit = {
|
|
273
|
+
authorizationStatusFor: Native.authorizationStatusFor,
|
|
274
|
+
isHealthDataAvailable: Native.isHealthDataAvailable,
|
|
275
|
+
buildUnitWithPrefix,
|
|
276
|
+
disableAllBackgroundDelivery: Native.disableAllBackgroundDelivery,
|
|
277
|
+
disableBackgroundDelivery: Native.disableBackgroundDelivery,
|
|
278
|
+
enableBackgroundDelivery: Native.enableBackgroundDelivery,
|
|
279
|
+
// simple convenience getters
|
|
280
|
+
getBiologicalSex: Native.getBiologicalSex,
|
|
281
|
+
getFitzpatrickSkinType: Native.getFitzpatrickSkinType,
|
|
282
|
+
getWheelchairUse: Native.getWheelchairUse,
|
|
283
|
+
getBloodType: Native.getBloodType,
|
|
284
|
+
getDateOfBirth,
|
|
285
|
+
getMostRecentQuantitySample,
|
|
286
|
+
getMostRecentCategorySample,
|
|
287
|
+
getMostRecentWorkout,
|
|
288
|
+
getPreferredUnit,
|
|
289
|
+
getPreferredUnits,
|
|
290
|
+
getRequestStatusForAuthorization,
|
|
291
|
+
getWorkoutRoutes,
|
|
292
|
+
// query methods
|
|
293
|
+
queryCategorySamples,
|
|
294
|
+
queryCorrelationSamples,
|
|
295
|
+
queryQuantitySamples,
|
|
296
|
+
queryStatisticsForQuantity,
|
|
297
|
+
queryWorkouts,
|
|
298
|
+
requestAuthorization,
|
|
299
|
+
// save methods
|
|
300
|
+
saveCategorySample,
|
|
301
|
+
saveCorrelationSample,
|
|
302
|
+
saveQuantitySample,
|
|
303
|
+
saveWorkoutSample,
|
|
304
|
+
// subscriptions
|
|
305
|
+
subscribeToChanges,
|
|
306
|
+
// hooks
|
|
307
|
+
useMostRecentCategorySample,
|
|
308
|
+
useMostRecentQuantitySample,
|
|
309
|
+
useMostRecentWorkout,
|
|
310
|
+
useSubscribeToChanges,
|
|
311
|
+
};
|
|
312
|
+
export * from './native-types';
|
|
313
|
+
export * from './types';
|
|
314
|
+
export default Healthkit;
|
package/lib/src/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
getWorkoutRoutes: UnavailableFn,
|
|
26
|
+
isHealthDataAvailable: () => Promise.resolve(false),
|
|
27
|
+
queryCategorySamples: UnavailableFn,
|
|
28
|
+
queryCorrelationSamples: UnavailableFn,
|
|
29
|
+
queryQuantitySamples: UnavailableFn,
|
|
30
|
+
queryStatisticsForQuantity: UnavailableFn,
|
|
31
|
+
queryWorkouts: UnavailableFn,
|
|
32
|
+
requestAuthorization: UnavailableFn,
|
|
33
|
+
saveCategorySample: UnavailableFn,
|
|
34
|
+
saveCorrelationSample: UnavailableFn,
|
|
35
|
+
saveQuantitySample: UnavailableFn,
|
|
36
|
+
saveWorkoutSample: UnavailableFn,
|
|
37
|
+
subscribeToChanges: UnavailableFn,
|
|
38
|
+
useMostRecentCategorySample: UnavailableFn,
|
|
39
|
+
useMostRecentQuantitySample: UnavailableFn,
|
|
40
|
+
useMostRecentWorkout: UnavailableFn,
|
|
41
|
+
useSubscribeToChanges: UnavailableFn,
|
|
42
|
+
};
|
|
43
|
+
export * from './native-types';
|
|
44
|
+
export * from './types';
|
|
45
|
+
export default Healthkit;
|