@kingstinct/react-native-healthkit 4.4.4 → 5.0.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.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- package/ios/ReactNativeHealthkit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/ios/ReactNativeHealthkit.xcodeproj/project.xcworkspace/xcuserdata/robertherber.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/ReactNativeHealthkit.xcodeproj/xcuserdata/robertherber.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
- package/lib/commonjs/hooks/useHealthkitAuthorization.js +41 -0
- package/lib/commonjs/hooks/useHealthkitAuthorization.js.map +1 -0
- package/lib/commonjs/hooks/useIsHealthDataAvailable.js +29 -0
- package/lib/commonjs/hooks/useIsHealthDataAvailable.js.map +1 -0
- package/lib/commonjs/hooks/useMostRecentCategorySample.js +27 -0
- package/lib/commonjs/hooks/useMostRecentCategorySample.js.map +1 -0
- package/lib/commonjs/hooks/useMostRecentQuantitySample.js +43 -0
- package/lib/commonjs/hooks/useMostRecentQuantitySample.js.map +1 -0
- package/lib/commonjs/hooks/useMostRecentWorkout.js +60 -0
- package/lib/commonjs/hooks/useMostRecentWorkout.js.map +1 -0
- package/lib/commonjs/hooks/useStatisticsForQuantity.js +35 -0
- package/lib/commonjs/hooks/useStatisticsForQuantity.js.map +1 -0
- package/lib/commonjs/hooks/useSubscribeToChanges.js +37 -0
- package/lib/commonjs/hooks/useSubscribeToChanges.js.map +1 -0
- package/lib/commonjs/index.ios.js +66 -394
- package/lib/commonjs/index.ios.js.map +1 -1
- package/lib/commonjs/index.js +9 -17
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/native-types.js +249 -78
- package/lib/commonjs/native-types.js.map +1 -1
- package/lib/commonjs/types.js +13 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/utils/deserializeCategorySample.js +15 -0
- package/lib/commonjs/utils/deserializeCategorySample.js.map +1 -0
- package/lib/commonjs/utils/deserializeCorrelation.js +32 -0
- package/lib/commonjs/utils/deserializeCorrelation.js.map +1 -0
- package/lib/commonjs/utils/deserializeSample.js +17 -0
- package/lib/commonjs/utils/deserializeSample.js.map +1 -0
- package/lib/commonjs/utils/deserializeWorkout.js +17 -0
- package/lib/commonjs/utils/deserializeWorkout.js.map +1 -0
- package/lib/commonjs/utils/ensureMetadata.js +14 -0
- package/lib/commonjs/utils/ensureMetadata.js.map +1 -0
- package/lib/commonjs/utils/ensureUnit.js +23 -0
- package/lib/commonjs/utils/ensureUnit.js.map +1 -0
- package/lib/commonjs/utils/getDateOfBirth.js +19 -0
- package/lib/commonjs/utils/getDateOfBirth.js.map +1 -0
- package/lib/commonjs/utils/getMostRecentCategorySample.js +22 -0
- package/lib/commonjs/utils/getMostRecentCategorySample.js.map +1 -0
- package/lib/commonjs/utils/getMostRecentQuantitySample.js +22 -0
- package/lib/commonjs/utils/getMostRecentQuantitySample.js.map +1 -0
- package/lib/commonjs/utils/getMostRecentWorkout.js +24 -0
- package/lib/commonjs/utils/getMostRecentWorkout.js.map +1 -0
- package/lib/commonjs/utils/getPreferredUnit.js +19 -0
- package/lib/commonjs/utils/getPreferredUnit.js.map +1 -0
- package/lib/commonjs/utils/getPreferredUnits.js +19 -0
- package/lib/commonjs/utils/getPreferredUnits.js.map +1 -0
- package/lib/commonjs/utils/getPreferredUnitsTyped.js +46 -0
- package/lib/commonjs/utils/getPreferredUnitsTyped.js.map +1 -0
- package/lib/commonjs/utils/getRequestStatusForAuthorization.js +25 -0
- package/lib/commonjs/utils/getRequestStatusForAuthorization.js.map +1 -0
- package/lib/commonjs/utils/prepareOptions.js +27 -0
- package/lib/commonjs/utils/prepareOptions.js.map +1 -0
- package/lib/commonjs/utils/queryCategorySamples.js +24 -0
- package/lib/commonjs/utils/queryCategorySamples.js.map +1 -0
- package/lib/commonjs/utils/queryCorrelationSamples.js +24 -0
- package/lib/commonjs/utils/queryCorrelationSamples.js.map +1 -0
- package/lib/commonjs/utils/queryQuantitySamples.js +27 -0
- package/lib/commonjs/utils/queryQuantitySamples.js.map +1 -0
- package/lib/commonjs/utils/queryStatisticsForQuantity.js +34 -0
- package/lib/commonjs/utils/queryStatisticsForQuantity.js.map +1 -0
- package/lib/commonjs/utils/queryWorkouts.js +30 -0
- package/lib/commonjs/utils/queryWorkouts.js.map +1 -0
- package/lib/commonjs/utils/requestAuthorization.js +25 -0
- package/lib/commonjs/utils/requestAuthorization.js.map +1 -0
- package/lib/commonjs/utils/saveCategorySample.js +21 -0
- package/lib/commonjs/utils/saveCategorySample.js.map +1 -0
- package/lib/commonjs/utils/saveCorrelationSample.js +24 -0
- package/lib/commonjs/utils/saveCorrelationSample.js.map +1 -0
- package/lib/commonjs/utils/saveQuantitySample.js +21 -0
- package/lib/commonjs/utils/saveQuantitySample.js.map +1 -0
- package/lib/commonjs/utils/saveWorkoutSample.js +25 -0
- package/lib/commonjs/utils/saveWorkoutSample.js.map +1 -0
- package/lib/commonjs/utils/serializeDate.js +12 -0
- package/lib/commonjs/utils/serializeDate.js.map +1 -0
- package/lib/commonjs/utils/subscribeToChanges.js +37 -0
- package/lib/commonjs/utils/subscribeToChanges.js.map +1 -0
- package/lib/module/hooks/useHealthkitAuthorization.js +29 -0
- package/lib/module/hooks/useHealthkitAuthorization.js.map +1 -0
- package/lib/module/hooks/useIsHealthDataAvailable.js +18 -0
- package/lib/module/hooks/useIsHealthDataAvailable.js.map +1 -0
- package/lib/module/hooks/useMostRecentCategorySample.js +15 -0
- package/lib/module/hooks/useMostRecentCategorySample.js.map +1 -0
- package/lib/module/hooks/useMostRecentQuantitySample.js +30 -0
- package/lib/module/hooks/useMostRecentQuantitySample.js.map +1 -0
- package/lib/module/hooks/useMostRecentWorkout.js +47 -0
- package/lib/module/hooks/useMostRecentWorkout.js.map +1 -0
- package/lib/module/hooks/useStatisticsForQuantity.js +23 -0
- package/lib/module/hooks/useStatisticsForQuantity.js.map +1 -0
- package/lib/module/hooks/useSubscribeToChanges.js +26 -0
- package/lib/module/hooks/useSubscribeToChanges.js.map +1 -0
- package/lib/module/index.ios.js +32 -370
- package/lib/module/index.ios.js.map +1 -1
- package/lib/module/index.js +10 -7
- package/lib/module/index.js.map +1 -1
- package/lib/module/native-types.js +232 -74
- package/lib/module/native-types.js.map +1 -1
- package/lib/module/types.js +15 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/utils/deserializeCategorySample.js +7 -0
- package/lib/module/utils/deserializeCategorySample.js.map +1 -0
- package/lib/module/utils/deserializeCorrelation.js +21 -0
- package/lib/module/utils/deserializeCorrelation.js.map +1 -0
- package/lib/module/utils/deserializeSample.js +9 -0
- package/lib/module/utils/deserializeSample.js.map +1 -0
- package/lib/module/utils/deserializeWorkout.js +9 -0
- package/lib/module/utils/deserializeWorkout.js.map +1 -0
- package/lib/module/utils/ensureMetadata.js +6 -0
- package/lib/module/utils/ensureMetadata.js.map +1 -0
- package/lib/module/utils/ensureUnit.js +13 -0
- package/lib/module/utils/ensureUnit.js.map +1 -0
- package/lib/module/utils/getDateOfBirth.js +9 -0
- package/lib/module/utils/getDateOfBirth.js.map +1 -0
- package/lib/module/utils/getMostRecentCategorySample.js +12 -0
- package/lib/module/utils/getMostRecentCategorySample.js.map +1 -0
- package/lib/module/utils/getMostRecentQuantitySample.js +12 -0
- package/lib/module/utils/getMostRecentQuantitySample.js.map +1 -0
- package/lib/module/utils/getMostRecentWorkout.js +14 -0
- package/lib/module/utils/getMostRecentWorkout.js.map +1 -0
- package/lib/module/utils/getPreferredUnit.js +9 -0
- package/lib/module/utils/getPreferredUnit.js.map +1 -0
- package/lib/module/utils/getPreferredUnits.js +9 -0
- package/lib/module/utils/getPreferredUnits.js.map +1 -0
- package/lib/module/utils/getPreferredUnitsTyped.js +34 -0
- package/lib/module/utils/getPreferredUnitsTyped.js.map +1 -0
- package/lib/module/utils/getRequestStatusForAuthorization.js +15 -0
- package/lib/module/utils/getRequestStatusForAuthorization.js.map +1 -0
- package/lib/module/utils/prepareOptions.js +17 -0
- package/lib/module/utils/prepareOptions.js.map +1 -0
- package/lib/module/utils/queryCategorySamples.js +12 -0
- package/lib/module/utils/queryCategorySamples.js.map +1 -0
- package/lib/module/utils/queryCorrelationSamples.js +12 -0
- package/lib/module/utils/queryCorrelationSamples.js.map +1 -0
- package/lib/module/utils/queryQuantitySamples.js +14 -0
- package/lib/module/utils/queryQuantitySamples.js.map +1 -0
- package/lib/module/utils/queryStatisticsForQuantity.js +23 -0
- package/lib/module/utils/queryStatisticsForQuantity.js.map +1 -0
- package/lib/module/utils/queryWorkouts.js +17 -0
- package/lib/module/utils/queryWorkouts.js.map +1 -0
- package/lib/module/utils/requestAuthorization.js +15 -0
- package/lib/module/utils/requestAuthorization.js.map +1 -0
- package/lib/module/utils/saveCategorySample.js +11 -0
- package/lib/module/utils/saveCategorySample.js.map +1 -0
- package/lib/module/utils/saveCorrelationSample.js +13 -0
- package/lib/module/utils/saveCorrelationSample.js.map +1 -0
- package/lib/module/utils/saveQuantitySample.js +11 -0
- package/lib/module/utils/saveQuantitySample.js.map +1 -0
- package/lib/module/utils/saveWorkoutSample.js +14 -0
- package/lib/module/utils/saveWorkoutSample.js.map +1 -0
- package/lib/module/utils/serializeDate.js +4 -0
- package/lib/module/utils/serializeDate.js.map +1 -0
- package/lib/module/utils/subscribeToChanges.js +25 -0
- package/lib/module/utils/subscribeToChanges.js.map +1 -0
- package/lib/typescript/example-expo/App.d.ts +2 -0
- package/lib/typescript/src/hooks/useHealthkitAuthorization.d.ts +3 -0
- package/lib/typescript/src/hooks/useIsHealthDataAvailable.d.ts +2 -0
- package/lib/typescript/src/hooks/useMostRecentCategorySample.d.ts +4 -0
- package/lib/typescript/src/hooks/useMostRecentQuantitySample.d.ts +4 -0
- package/lib/typescript/src/hooks/useMostRecentWorkout.d.ts +7 -0
- package/lib/typescript/src/hooks/useStatisticsForQuantity.d.ts +4 -0
- package/lib/typescript/src/hooks/useSubscribeToChanges.d.ts +3 -0
- package/lib/typescript/src/index.d.ts +2 -3
- package/lib/typescript/src/index.ios.d.ts +48 -3
- package/lib/typescript/src/native-types.d.ts +235 -70
- package/lib/typescript/src/types.d.ts +14 -90
- package/lib/typescript/src/utils/deserializeCategorySample.d.ts +4 -0
- package/lib/typescript/src/utils/deserializeCorrelation.d.ts +4 -0
- package/lib/typescript/src/utils/deserializeSample.d.ts +4 -0
- package/lib/typescript/src/utils/deserializeWorkout.d.ts +4 -0
- package/lib/typescript/src/utils/ensureMetadata.d.ts +2 -0
- package/lib/typescript/src/utils/ensureUnit.d.ts +3 -0
- package/lib/typescript/src/utils/getDateOfBirth.d.ts +2 -0
- package/lib/typescript/src/utils/getMostRecentCategorySample.d.ts +3 -0
- package/lib/typescript/src/utils/getMostRecentQuantitySample.d.ts +3 -0
- package/lib/typescript/src/utils/getMostRecentWorkout.d.ts +5 -0
- package/lib/typescript/src/utils/getPreferredUnit.d.ts +4 -0
- package/lib/typescript/src/utils/getPreferredUnits.d.ts +4 -0
- package/lib/typescript/src/utils/getPreferredUnitsTyped.d.ts +9 -0
- package/lib/typescript/src/utils/getRequestStatusForAuthorization.d.ts +3 -0
- package/lib/typescript/src/utils/prepareOptions.d.ts +8 -0
- package/lib/typescript/src/utils/queryCategorySamples.d.ts +5 -0
- package/lib/typescript/src/utils/queryCorrelationSamples.d.ts +5 -0
- package/lib/typescript/src/utils/queryQuantitySamples.d.ts +7 -0
- package/lib/typescript/src/utils/queryStatisticsForQuantity.d.ts +14 -0
- package/lib/typescript/src/utils/queryWorkouts.d.ts +4 -0
- package/lib/typescript/src/utils/requestAuthorization.d.ts +3 -0
- package/lib/typescript/src/utils/saveCategorySample.d.ts +7 -0
- package/lib/typescript/src/utils/saveCorrelationSample.d.ts +8 -0
- package/lib/typescript/src/utils/saveQuantitySample.d.ts +7 -0
- package/lib/typescript/src/utils/saveWorkoutSample.d.ts +7 -0
- package/lib/typescript/src/utils/serializeDate.d.ts +2 -0
- package/lib/typescript/src/utils/subscribeToChanges.d.ts +3 -0
- package/package.json +17 -6
- package/src/hooks/useHealthkitAuthorization.ts +39 -0
- package/src/hooks/useIsHealthDataAvailable.ts +17 -0
- package/src/hooks/useMostRecentCategorySample.ts +24 -0
- package/src/hooks/useMostRecentQuantitySample.ts +39 -0
- package/src/hooks/useMostRecentWorkout.ts +60 -0
- package/src/hooks/useStatisticsForQuantity.ts +42 -0
- package/src/hooks/useSubscribeToChanges.ts +31 -0
- package/src/index.ios.tsx +36 -569
- package/src/index.tsx +12 -9
- package/src/native-types.ts +323 -93
- package/src/types.ts +41 -152
- package/src/utils/deserializeCategorySample.ts +12 -0
- package/src/utils/deserializeCorrelation.ts +28 -0
- package/src/utils/deserializeSample.ts +17 -0
- package/src/utils/deserializeWorkout.ts +14 -0
- package/src/utils/ensureMetadata.ts +5 -0
- package/src/utils/ensureUnit.ts +19 -0
- package/src/utils/getDateOfBirth.ts +8 -0
- package/src/utils/getMostRecentCategorySample.ts +18 -0
- package/src/utils/getMostRecentQuantitySample.ts +19 -0
- package/src/utils/getMostRecentWorkout.ts +27 -0
- package/src/utils/getPreferredUnit.ts +14 -0
- package/src/utils/getPreferredUnits.ts +14 -0
- package/src/utils/getPreferredUnitsTyped.ts +38 -0
- package/src/utils/getRequestStatusForAuthorization.ts +21 -0
- package/src/utils/prepareOptions.ts +15 -0
- package/src/utils/queryCategorySamples.ts +29 -0
- package/src/utils/queryCorrelationSamples.ts +29 -0
- package/src/utils/queryQuantitySamples.ts +36 -0
- package/src/utils/queryStatisticsForQuantity.ts +38 -0
- package/src/utils/queryWorkouts.ts +28 -0
- package/src/utils/requestAuthorization.ts +18 -0
- package/src/utils/saveCategorySample.ts +27 -0
- package/src/utils/saveCorrelationSample.ts +34 -0
- package/src/utils/saveQuantitySample.ts +29 -0
- package/src/utils/saveWorkoutSample.ts +28 -0
- package/src/utils/serializeDate.ts +3 -0
- package/src/utils/subscribeToChanges.ts +32 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import ensureUnit from '../utils/ensureUnit';
|
|
3
|
+
import getMostRecentQuantitySample from '../utils/getMostRecentQuantitySample';
|
|
4
|
+
import subscribeToChanges from '../utils/subscribeToChanges';
|
|
5
|
+
|
|
6
|
+
function useMostRecentQuantitySample(identifier, unit) {
|
|
7
|
+
const [lastSample, setLastSample] = useState(null);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
let cancelSubscription;
|
|
10
|
+
|
|
11
|
+
const init = async () => {
|
|
12
|
+
const actualUnit = await ensureUnit(identifier, unit);
|
|
13
|
+
cancelSubscription = await subscribeToChanges(identifier, async () => {
|
|
14
|
+
const value = await getMostRecentQuantitySample(identifier, actualUnit);
|
|
15
|
+
setLastSample(value);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
void init();
|
|
20
|
+
return () => {
|
|
21
|
+
var _cancelSubscription;
|
|
22
|
+
|
|
23
|
+
void ((_cancelSubscription = cancelSubscription) === null || _cancelSubscription === void 0 ? void 0 : _cancelSubscription());
|
|
24
|
+
};
|
|
25
|
+
}, [identifier, unit]);
|
|
26
|
+
return lastSample;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default useMostRecentQuantitySample;
|
|
30
|
+
//# sourceMappingURL=useMostRecentQuantitySample.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useEffect","useState","ensureUnit","getMostRecentQuantitySample","subscribeToChanges","useMostRecentQuantitySample","identifier","unit","lastSample","setLastSample","cancelSubscription","init","actualUnit","value"],"sources":["useMostRecentQuantitySample.ts"],"sourcesContent":["import { useEffect, useState } from 'react'\n\nimport ensureUnit from '../utils/ensureUnit'\nimport getMostRecentQuantitySample from '../utils/getMostRecentQuantitySample'\nimport subscribeToChanges from '../utils/subscribeToChanges'\n\nimport type { HKQuantityTypeIdentifier, UnitForIdentifier } from '../native-types'\nimport type { HKQuantitySample } from '../types'\n\nfunction useMostRecentQuantitySample<\n TIdentifier extends HKQuantityTypeIdentifier,\n TUnit extends UnitForIdentifier<TIdentifier>\n>(identifier: TIdentifier, unit?: TUnit) {\n const [lastSample, setLastSample] = useState<HKQuantitySample<\n TIdentifier\n > | null>(null)\n\n useEffect(() => {\n let cancelSubscription: (() => Promise<boolean>) | undefined\n\n const init = async () => {\n const actualUnit = await ensureUnit(identifier, unit)\n\n cancelSubscription = await subscribeToChanges(identifier, async () => {\n const value = await getMostRecentQuantitySample(identifier, actualUnit)\n setLastSample(value)\n })\n }\n void init()\n\n return () => {\n void cancelSubscription?.()\n }\n }, [identifier, unit])\n\n return lastSample\n}\n\nexport default useMostRecentQuantitySample\n"],"mappings":"AAAA,SAASA,SAAT,EAAoBC,QAApB,QAAoC,OAApC;AAEA,OAAOC,UAAP,MAAuB,qBAAvB;AACA,OAAOC,2BAAP,MAAwC,sCAAxC;AACA,OAAOC,kBAAP,MAA+B,6BAA/B;;AAKA,SAASC,2BAAT,CAGEC,UAHF,EAG2BC,IAH3B,EAGyC;EACvC,MAAM,CAACC,UAAD,EAAaC,aAAb,IAA8BR,QAAQ,CAElC,IAFkC,CAA5C;EAIAD,SAAS,CAAC,MAAM;IACd,IAAIU,kBAAJ;;IAEA,MAAMC,IAAI,GAAG,YAAY;MACvB,MAAMC,UAAU,GAAG,MAAMV,UAAU,CAACI,UAAD,EAAaC,IAAb,CAAnC;MAEAG,kBAAkB,GAAG,MAAMN,kBAAkB,CAACE,UAAD,EAAa,YAAY;QACpE,MAAMO,KAAK,GAAG,MAAMV,2BAA2B,CAACG,UAAD,EAAaM,UAAb,CAA/C;QACAH,aAAa,CAACI,KAAD,CAAb;MACD,CAH4C,CAA7C;IAID,CAPD;;IAQA,KAAKF,IAAI,EAAT;IAEA,OAAO,MAAM;MAAA;;MACX,6BAAKD,kBAAL,wDAAK,qBAAL;IACD,CAFD;EAGD,CAhBQ,EAgBN,CAACJ,UAAD,EAAaC,IAAb,CAhBM,CAAT;EAkBA,OAAOC,UAAP;AACD;;AAED,eAAeH,2BAAf"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useEffect, useState, useRef, useCallback } from 'react';
|
|
2
|
+
import getMostRecentWorkout from '../utils/getMostRecentWorkout';
|
|
3
|
+
import getPreferredUnitsTyped from '../utils/getPreferredUnitsTyped';
|
|
4
|
+
import subscribeToChanges from '../utils/subscribeToChanges';
|
|
5
|
+
|
|
6
|
+
function useMostRecentWorkout(options) {
|
|
7
|
+
const [workout, setWorkout] = useState(null);
|
|
8
|
+
const optionsRef = useRef(options);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
optionsRef.current = options;
|
|
11
|
+
}, [options]);
|
|
12
|
+
const update = useCallback(async () => {
|
|
13
|
+
const {
|
|
14
|
+
energyUnit,
|
|
15
|
+
distanceUnit
|
|
16
|
+
} = await getPreferredUnitsTyped(optionsRef.current);
|
|
17
|
+
console.log({
|
|
18
|
+
energyUnit,
|
|
19
|
+
distanceUnit
|
|
20
|
+
});
|
|
21
|
+
setWorkout(await getMostRecentWorkout({
|
|
22
|
+
energyUnit,
|
|
23
|
+
distanceUnit
|
|
24
|
+
}));
|
|
25
|
+
}, []);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
void update();
|
|
28
|
+
}, [update]);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
let cancelSubscription;
|
|
31
|
+
|
|
32
|
+
const init = async () => {
|
|
33
|
+
cancelSubscription = await subscribeToChanges('HKWorkoutTypeIdentifier', update);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
void init();
|
|
37
|
+
return () => {
|
|
38
|
+
var _cancelSubscription;
|
|
39
|
+
|
|
40
|
+
void ((_cancelSubscription = cancelSubscription) === null || _cancelSubscription === void 0 ? void 0 : _cancelSubscription());
|
|
41
|
+
};
|
|
42
|
+
}, [update]);
|
|
43
|
+
return workout;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default useMostRecentWorkout;
|
|
47
|
+
//# sourceMappingURL=useMostRecentWorkout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useEffect","useState","useRef","useCallback","getMostRecentWorkout","getPreferredUnitsTyped","subscribeToChanges","useMostRecentWorkout","options","workout","setWorkout","optionsRef","current","update","energyUnit","distanceUnit","console","log","cancelSubscription","init"],"sources":["useMostRecentWorkout.ts"],"sourcesContent":["import {\n useEffect, useState, useRef, useCallback,\n} from 'react'\n\nimport getMostRecentWorkout from '../utils/getMostRecentWorkout'\nimport getPreferredUnitsTyped from '../utils/getPreferredUnitsTyped'\nimport subscribeToChanges from '../utils/subscribeToChanges'\n\nimport type { EnergyUnit, LengthUnit } from '../native-types'\nimport type { HKWorkout } from '../types'\n\nfunction useMostRecentWorkout<\n TEnergy extends EnergyUnit,\n TDistance extends LengthUnit\n>(options?: { readonly energyUnit?: TEnergy; readonly distanceUnit?: TDistance }) {\n const [workout, setWorkout] = useState<HKWorkout<TEnergy, TDistance> | null>(null)\n\n const optionsRef = useRef(options)\n\n useEffect(() => {\n optionsRef.current = options\n }, [options])\n\n const update = useCallback(async () => {\n const { energyUnit, distanceUnit } = await getPreferredUnitsTyped(\n optionsRef.current,\n )\n\n console.log({ energyUnit, distanceUnit })\n\n setWorkout(await getMostRecentWorkout({\n energyUnit,\n distanceUnit,\n }))\n }, [])\n\n useEffect(() => {\n void update()\n }, [update])\n\n useEffect(() => {\n let cancelSubscription: (() => Promise<boolean>) | undefined\n\n const init = async () => {\n cancelSubscription = await subscribeToChanges(\n 'HKWorkoutTypeIdentifier',\n update,\n )\n }\n void init()\n\n return () => {\n void cancelSubscription?.()\n }\n }, [update])\n\n return workout\n}\n\nexport default useMostRecentWorkout\n"],"mappings":"AAAA,SACEA,SADF,EACaC,QADb,EACuBC,MADvB,EAC+BC,WAD/B,QAEO,OAFP;AAIA,OAAOC,oBAAP,MAAiC,+BAAjC;AACA,OAAOC,sBAAP,MAAmC,iCAAnC;AACA,OAAOC,kBAAP,MAA+B,6BAA/B;;AAKA,SAASC,oBAAT,CAGEC,OAHF,EAGkF;EAChF,MAAM,CAACC,OAAD,EAAUC,UAAV,IAAwBT,QAAQ,CAAuC,IAAvC,CAAtC;EAEA,MAAMU,UAAU,GAAGT,MAAM,CAACM,OAAD,CAAzB;EAEAR,SAAS,CAAC,MAAM;IACdW,UAAU,CAACC,OAAX,GAAqBJ,OAArB;EACD,CAFQ,EAEN,CAACA,OAAD,CAFM,CAAT;EAIA,MAAMK,MAAM,GAAGV,WAAW,CAAC,YAAY;IACrC,MAAM;MAAEW,UAAF;MAAcC;IAAd,IAA+B,MAAMV,sBAAsB,CAC/DM,UAAU,CAACC,OADoD,CAAjE;IAIAI,OAAO,CAACC,GAAR,CAAY;MAAEH,UAAF;MAAcC;IAAd,CAAZ;IAEAL,UAAU,CAAC,MAAMN,oBAAoB,CAAC;MACpCU,UADoC;MAEpCC;IAFoC,CAAD,CAA3B,CAAV;EAID,CAXyB,EAWvB,EAXuB,CAA1B;EAaAf,SAAS,CAAC,MAAM;IACd,KAAKa,MAAM,EAAX;EACD,CAFQ,EAEN,CAACA,MAAD,CAFM,CAAT;EAIAb,SAAS,CAAC,MAAM;IACd,IAAIkB,kBAAJ;;IAEA,MAAMC,IAAI,GAAG,YAAY;MACvBD,kBAAkB,GAAG,MAAMZ,kBAAkB,CAC3C,yBAD2C,EAE3CO,MAF2C,CAA7C;IAID,CALD;;IAMA,KAAKM,IAAI,EAAT;IAEA,OAAO,MAAM;MAAA;;MACX,6BAAKD,kBAAL,wDAAK,qBAAL;IACD,CAFD;EAGD,CAdQ,EAcN,CAACL,MAAD,CAdM,CAAT;EAgBA,OAAOJ,OAAP;AACD;;AAED,eAAeF,oBAAf"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback, useRef } from 'react';
|
|
2
|
+
import queryStatisticsForQuantity from '../utils/queryStatisticsForQuantity';
|
|
3
|
+
import useSubscribeToChanges from './useSubscribeToChanges';
|
|
4
|
+
|
|
5
|
+
function useStatisticsForQuantity(identifier, options, from, to, unit) {
|
|
6
|
+
const [result, setResult] = useState(null);
|
|
7
|
+
const optionsRef = useRef(options);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
optionsRef.current = options;
|
|
10
|
+
}, [options]);
|
|
11
|
+
const update = useCallback(async () => {
|
|
12
|
+
const res = await queryStatisticsForQuantity(identifier, optionsRef.current, from, to, unit);
|
|
13
|
+
setResult(res);
|
|
14
|
+
}, [identifier, from, to, unit]);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
void update();
|
|
17
|
+
}, [update]);
|
|
18
|
+
useSubscribeToChanges(identifier, update);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default useStatisticsForQuantity;
|
|
23
|
+
//# sourceMappingURL=useStatisticsForQuantity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useState","useEffect","useCallback","useRef","queryStatisticsForQuantity","useSubscribeToChanges","useStatisticsForQuantity","identifier","options","from","to","unit","result","setResult","optionsRef","current","update","res"],"sources":["useStatisticsForQuantity.ts"],"sourcesContent":["import {\n useState, useEffect, useCallback, useRef,\n} from 'react'\n\nimport queryStatisticsForQuantity from '../utils/queryStatisticsForQuantity'\nimport useSubscribeToChanges from './useSubscribeToChanges'\n\nimport type { HKQuantityTypeIdentifier, HKStatisticsOptions, UnitForIdentifier } from '../native-types'\nimport type { QueryStatisticsResponse } from '../types'\n\nfunction useStatisticsForQuantity<TIdentifier extends HKQuantityTypeIdentifier, TUnit extends UnitForIdentifier<TIdentifier> = UnitForIdentifier<TIdentifier>>(\n identifier: TIdentifier,\n options: readonly HKStatisticsOptions[],\n from: Date,\n to?: Date,\n unit?: TUnit,\n) {\n const [result, setResult] = useState<QueryStatisticsResponse<TIdentifier, TUnit> | null>(null)\n\n const optionsRef = useRef(options)\n\n useEffect(() => {\n optionsRef.current = options\n }, [options])\n\n const update = useCallback(async () => {\n const res = await queryStatisticsForQuantity(identifier, optionsRef.current, from, to, unit)\n setResult(res)\n }, [\n identifier, from, to, unit,\n ])\n\n useEffect(() => {\n void update()\n }, [update])\n\n useSubscribeToChanges(identifier, update)\n\n return result\n}\n\nexport default useStatisticsForQuantity\n"],"mappings":"AAAA,SACEA,QADF,EACYC,SADZ,EACuBC,WADvB,EACoCC,MADpC,QAEO,OAFP;AAIA,OAAOC,0BAAP,MAAuC,qCAAvC;AACA,OAAOC,qBAAP,MAAkC,yBAAlC;;AAKA,SAASC,wBAAT,CACEC,UADF,EAEEC,OAFF,EAGEC,IAHF,EAIEC,EAJF,EAKEC,IALF,EAME;EACA,MAAM,CAACC,MAAD,EAASC,SAAT,IAAsBb,QAAQ,CAAqD,IAArD,CAApC;EAEA,MAAMc,UAAU,GAAGX,MAAM,CAACK,OAAD,CAAzB;EAEAP,SAAS,CAAC,MAAM;IACda,UAAU,CAACC,OAAX,GAAqBP,OAArB;EACD,CAFQ,EAEN,CAACA,OAAD,CAFM,CAAT;EAIA,MAAMQ,MAAM,GAAGd,WAAW,CAAC,YAAY;IACrC,MAAMe,GAAG,GAAG,MAAMb,0BAA0B,CAACG,UAAD,EAAaO,UAAU,CAACC,OAAxB,EAAiCN,IAAjC,EAAuCC,EAAvC,EAA2CC,IAA3C,CAA5C;IACAE,SAAS,CAACI,GAAD,CAAT;EACD,CAHyB,EAGvB,CACDV,UADC,EACWE,IADX,EACiBC,EADjB,EACqBC,IADrB,CAHuB,CAA1B;EAOAV,SAAS,CAAC,MAAM;IACd,KAAKe,MAAM,EAAX;EACD,CAFQ,EAEN,CAACA,MAAD,CAFM,CAAT;EAIAX,qBAAqB,CAACE,UAAD,EAAaS,MAAb,CAArB;EAEA,OAAOJ,MAAP;AACD;;AAED,eAAeN,wBAAf"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
import subscribeToChanges from '../utils/subscribeToChanges';
|
|
3
|
+
|
|
4
|
+
function useSubscribeToChanges(identifier, onChange) {
|
|
5
|
+
const onChangeRef = useRef(onChange);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
onChangeRef.current = onChange;
|
|
8
|
+
}, [onChange]);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
let cancelSubscription;
|
|
11
|
+
|
|
12
|
+
const init = async () => {
|
|
13
|
+
cancelSubscription = await subscribeToChanges(identifier, onChangeRef.current);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
void init();
|
|
17
|
+
return () => {
|
|
18
|
+
var _cancelSubscription;
|
|
19
|
+
|
|
20
|
+
void ((_cancelSubscription = cancelSubscription) === null || _cancelSubscription === void 0 ? void 0 : _cancelSubscription());
|
|
21
|
+
};
|
|
22
|
+
}, [identifier]);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default useSubscribeToChanges;
|
|
26
|
+
//# sourceMappingURL=useSubscribeToChanges.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useEffect","useRef","subscribeToChanges","useSubscribeToChanges","identifier","onChange","onChangeRef","current","cancelSubscription","init"],"sources":["useSubscribeToChanges.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\n\nimport subscribeToChanges from '../utils/subscribeToChanges'\n\nimport type { HKSampleTypeIdentifier } from '..'\n\nfunction useSubscribeToChanges<TIdentifier extends HKSampleTypeIdentifier>(\n identifier: TIdentifier,\n onChange: () => void,\n): void {\n const onChangeRef = useRef(onChange)\n\n useEffect(() => {\n onChangeRef.current = onChange\n }, [onChange])\n\n useEffect(() => {\n let cancelSubscription: (() => Promise<boolean>) | undefined\n\n const init = async () => {\n cancelSubscription = await subscribeToChanges(identifier, onChangeRef.current)\n }\n void init()\n\n return () => {\n void cancelSubscription?.()\n }\n }, [identifier])\n}\n\nexport default useSubscribeToChanges\n"],"mappings":"AAAA,SAASA,SAAT,EAAoBC,MAApB,QAAkC,OAAlC;AAEA,OAAOC,kBAAP,MAA+B,6BAA/B;;AAIA,SAASC,qBAAT,CACEC,UADF,EAEEC,QAFF,EAGQ;EACN,MAAMC,WAAW,GAAGL,MAAM,CAACI,QAAD,CAA1B;EAEAL,SAAS,CAAC,MAAM;IACdM,WAAW,CAACC,OAAZ,GAAsBF,QAAtB;EACD,CAFQ,EAEN,CAACA,QAAD,CAFM,CAAT;EAIAL,SAAS,CAAC,MAAM;IACd,IAAIQ,kBAAJ;;IAEA,MAAMC,IAAI,GAAG,YAAY;MACvBD,kBAAkB,GAAG,MAAMN,kBAAkB,CAACE,UAAD,EAAaE,WAAW,CAACC,OAAzB,CAA7C;IACD,CAFD;;IAGA,KAAKE,IAAI,EAAT;IAEA,OAAO,MAAM;MAAA;;MACX,6BAAKD,kBAAL,wDAAK,qBAAL;IACD,CAFD;EAGD,CAXQ,EAWN,CAACJ,UAAD,CAXM,CAAT;AAYD;;AAED,eAAeD,qBAAf"}
|
package/lib/module/index.ios.js
CHANGED
|
@@ -1,350 +1,40 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return { ...sample,
|
|
27
|
-
startDate: new Date(sample.startDate),
|
|
28
|
-
endDate: new Date(sample.endDate)
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const deserializCategorySample = sample => ({ ...sample,
|
|
33
|
-
startDate: new Date(sample.startDate),
|
|
34
|
-
endDate: new Date(sample.endDate)
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
const serializeDate = date => date ? date.toISOString() : new Date(0).toISOString();
|
|
38
|
-
|
|
39
|
-
const prepareOptions = options => {
|
|
40
|
-
const limit = !options.limit || options.limit === Infinity ? 0 : options.limit;
|
|
41
|
-
const ascending = options.ascending ?? limit === 0;
|
|
42
|
-
const from = serializeDate(options.from);
|
|
43
|
-
const to = serializeDate(options.to);
|
|
44
|
-
return {
|
|
45
|
-
limit,
|
|
46
|
-
ascending,
|
|
47
|
-
from,
|
|
48
|
-
to
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const queryQuantitySamples = async (identifier, options) => {
|
|
53
|
-
const unit = await ensureUnit(identifier, options.unit);
|
|
54
|
-
const opts = prepareOptions(options);
|
|
55
|
-
const quantitySamples = await Native.queryQuantitySamples(identifier, unit, opts.from, opts.to, opts.limit, opts.ascending);
|
|
56
|
-
return quantitySamples.map(deserializeSample);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
async function getPreferredUnitsTyped(options) {
|
|
60
|
-
let energyUnit = options === null || options === void 0 ? void 0 : options.energyUnit;
|
|
61
|
-
let distanceUnit = options === null || options === void 0 ? void 0 : options.distanceUnit;
|
|
62
|
-
|
|
63
|
-
if (!energyUnit || !distanceUnit) {
|
|
64
|
-
const units = await Native.getPreferredUnits([HKQuantityTypeIdentifier.distanceWalkingRunning, HKQuantityTypeIdentifier.activeEnergyBurned]);
|
|
65
|
-
|
|
66
|
-
if (!energyUnit) {
|
|
67
|
-
energyUnit = units[HKQuantityTypeIdentifier.distanceWalkingRunning];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (!distanceUnit) {
|
|
71
|
-
distanceUnit = units[HKQuantityTypeIdentifier.activeEnergyBurned];
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (!energyUnit) {
|
|
76
|
-
energyUnit = HKUnit.Kilocalories;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (!distanceUnit) {
|
|
80
|
-
distanceUnit = HKUnit.Meters;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
energyUnit,
|
|
85
|
-
distanceUnit
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const subscribeToChanges = async (identifier, callback) => {
|
|
90
|
-
const subscription = EventEmitter.addListener('onChange', _ref => {
|
|
91
|
-
let {
|
|
92
|
-
typeIdentifier
|
|
93
|
-
} = _ref;
|
|
94
|
-
|
|
95
|
-
if (typeIdentifier === identifier) {
|
|
96
|
-
callback();
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
const queryId = await Native.subscribeToObserverQuery(identifier).catch(async error => {
|
|
100
|
-
subscription.remove();
|
|
101
|
-
return Promise.reject(error);
|
|
102
|
-
});
|
|
103
|
-
return async () => {
|
|
104
|
-
subscription.remove();
|
|
105
|
-
return Native.unsubscribeQuery(queryId);
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const getMostRecentQuantitySample = async (identifier, unit) => {
|
|
110
|
-
const samples = await queryQuantitySamples(identifier, {
|
|
111
|
-
limit: 1,
|
|
112
|
-
unit
|
|
113
|
-
});
|
|
114
|
-
return samples[0];
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
function useMostRecentWorkout(options) {
|
|
118
|
-
const [workout, setWorkout] = useState(null);
|
|
119
|
-
useEffect(() => {
|
|
120
|
-
let cancelSubscription;
|
|
121
|
-
|
|
122
|
-
const init = async () => {
|
|
123
|
-
const {
|
|
124
|
-
energyUnit,
|
|
125
|
-
distanceUnit
|
|
126
|
-
} = await getPreferredUnitsTyped(options);
|
|
127
|
-
cancelSubscription = await subscribeToChanges('HKWorkoutTypeIdentifier', async () => {
|
|
128
|
-
const w = await getMostRecentWorkout({
|
|
129
|
-
energyUnit,
|
|
130
|
-
distanceUnit
|
|
131
|
-
});
|
|
132
|
-
setWorkout(w);
|
|
133
|
-
});
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
void init();
|
|
137
|
-
return () => {
|
|
138
|
-
var _cancelSubscription;
|
|
139
|
-
|
|
140
|
-
void ((_cancelSubscription = cancelSubscription) === null || _cancelSubscription === void 0 ? void 0 : _cancelSubscription());
|
|
141
|
-
};
|
|
142
|
-
}, [options]);
|
|
143
|
-
return workout;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const getMostRecentCategorySample = async identifier => {
|
|
147
|
-
const samples = await queryCategorySamples(identifier, {
|
|
148
|
-
limit: 1,
|
|
149
|
-
ascending: false
|
|
150
|
-
});
|
|
151
|
-
return samples[0];
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
function useSubscribeToChanges(identifier, onChange) {
|
|
155
|
-
useEffect(() => {
|
|
156
|
-
let cancelSubscription;
|
|
157
|
-
|
|
158
|
-
const init = async () => {
|
|
159
|
-
cancelSubscription = await subscribeToChanges(identifier, onChange);
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
void init();
|
|
163
|
-
return () => {
|
|
164
|
-
var _cancelSubscription2;
|
|
165
|
-
|
|
166
|
-
void ((_cancelSubscription2 = cancelSubscription) === null || _cancelSubscription2 === void 0 ? void 0 : _cancelSubscription2());
|
|
167
|
-
};
|
|
168
|
-
}, [identifier, onChange]);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function useMostRecentCategorySample(identifier) {
|
|
172
|
-
const [category, setCategory] = useState(null);
|
|
173
|
-
const updater = useCallback(() => {
|
|
174
|
-
void getMostRecentCategorySample(identifier).then(setCategory);
|
|
175
|
-
}, [identifier]);
|
|
176
|
-
useSubscribeToChanges(identifier, updater);
|
|
177
|
-
return category;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function useMostRecentQuantitySample(identifier, unit) {
|
|
181
|
-
const [lastSample, setLastSample] = useState(null);
|
|
182
|
-
useEffect(() => {
|
|
183
|
-
let cancelSubscription;
|
|
184
|
-
|
|
185
|
-
const init = async () => {
|
|
186
|
-
const actualUnit = await ensureUnit(identifier, unit);
|
|
187
|
-
cancelSubscription = await subscribeToChanges(identifier, async () => {
|
|
188
|
-
const value = await getMostRecentQuantitySample(identifier, actualUnit);
|
|
189
|
-
setLastSample(value);
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
void init();
|
|
194
|
-
return () => {
|
|
195
|
-
var _cancelSubscription3;
|
|
196
|
-
|
|
197
|
-
void ((_cancelSubscription3 = cancelSubscription) === null || _cancelSubscription3 === void 0 ? void 0 : _cancelSubscription3());
|
|
198
|
-
};
|
|
199
|
-
}, [identifier, unit]);
|
|
200
|
-
return lastSample;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const saveQuantitySample = async (identifier, unit, value, options) => {
|
|
204
|
-
const start = (options === null || options === void 0 ? void 0 : options.start) || (options === null || options === void 0 ? void 0 : options.end) || new Date();
|
|
205
|
-
const end = (options === null || options === void 0 ? void 0 : options.end) || (options === null || options === void 0 ? void 0 : options.start) || new Date();
|
|
206
|
-
const metadata = (options === null || options === void 0 ? void 0 : options.metadata) || {};
|
|
207
|
-
return Native.saveQuantitySample(identifier, unit, value, start.toISOString(), end.toISOString(), metadata);
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
const queryStatisticsForQuantity = async (identifier, options, from, to, unit) => {
|
|
211
|
-
const actualUnit = await ensureUnit(identifier, unit);
|
|
212
|
-
const toDate = to || new Date();
|
|
213
|
-
const {
|
|
214
|
-
mostRecentQuantityDateInterval,
|
|
215
|
-
...rawResponse
|
|
216
|
-
} = await Native.queryStatisticsForQuantity(identifier, actualUnit, from.toISOString(), toDate.toISOString(), options);
|
|
217
|
-
const response = { ...rawResponse,
|
|
218
|
-
...(mostRecentQuantityDateInterval ? {
|
|
219
|
-
mostRecentQuantityDateInterval: {
|
|
220
|
-
from: new Date(mostRecentQuantityDateInterval.from),
|
|
221
|
-
to: new Date(mostRecentQuantityDateInterval.to)
|
|
222
|
-
}
|
|
223
|
-
} : {})
|
|
224
|
-
};
|
|
225
|
-
return response;
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
const requestAuthorization = async function (read) {
|
|
229
|
-
let write = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
230
|
-
const readPermissions = read.reduce((obj, cur) => ({ ...obj,
|
|
231
|
-
[cur]: true
|
|
232
|
-
}), {});
|
|
233
|
-
const writePermissions = write.reduce((obj, cur) => ({ ...obj,
|
|
234
|
-
[cur]: true
|
|
235
|
-
}), {});
|
|
236
|
-
return Native.requestAuthorization(writePermissions, readPermissions);
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
const getDateOfBirth = async () => {
|
|
240
|
-
const dateOfBirth = await Native.getDateOfBirth();
|
|
241
|
-
return new Date(dateOfBirth);
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
const getRequestStatusForAuthorization = async function (read) {
|
|
245
|
-
let write = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
246
|
-
const readPermissions = read.reduce((obj, cur) => ({ ...obj,
|
|
247
|
-
[cur]: true
|
|
248
|
-
}), {});
|
|
249
|
-
const writePermissions = write.reduce((obj, cur) => ({ ...obj,
|
|
250
|
-
[cur]: true
|
|
251
|
-
}), {});
|
|
252
|
-
return Native.getRequestStatusForAuthorization(writePermissions, readPermissions);
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
const queryCategorySamples = async (identifier, options) => {
|
|
256
|
-
const opts = prepareOptions(options);
|
|
257
|
-
const results = await Native.queryCategorySamples(identifier, opts.from, opts.to, opts.limit, opts.ascending);
|
|
258
|
-
return results.map(deserializCategorySample);
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
const queryWorkouts = async options => {
|
|
262
|
-
const {
|
|
263
|
-
energyUnit,
|
|
264
|
-
distanceUnit
|
|
265
|
-
} = await getPreferredUnitsTyped(options);
|
|
266
|
-
const opts = prepareOptions(options);
|
|
267
|
-
const workouts = await Native.queryWorkoutSamples(energyUnit, distanceUnit, opts.from, opts.to, opts.limit, opts.ascending);
|
|
268
|
-
return workouts.map(deserializeWorkout);
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
const getMostRecentWorkout = async options => {
|
|
272
|
-
const workouts = await queryWorkouts({
|
|
273
|
-
limit: 1,
|
|
274
|
-
ascending: false,
|
|
275
|
-
energyUnit: options === null || options === void 0 ? void 0 : options.energyUnit,
|
|
276
|
-
distanceUnit: options === null || options === void 0 ? void 0 : options.distanceUnit
|
|
277
|
-
});
|
|
278
|
-
return workouts[0];
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
async function saveCategorySample(identifier, value, options) {
|
|
282
|
-
const start = (options === null || options === void 0 ? void 0 : options.start) || (options === null || options === void 0 ? void 0 : options.end) || new Date();
|
|
283
|
-
const end = (options === null || options === void 0 ? void 0 : options.end) || (options === null || options === void 0 ? void 0 : options.start) || new Date();
|
|
284
|
-
const metadata = (options === null || options === void 0 ? void 0 : options.metadata) || {};
|
|
285
|
-
return Native.saveCategorySample(identifier, value, start.toISOString(), end.toISOString(), metadata || {});
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const getPreferredUnits = async identifiers => {
|
|
289
|
-
const units = await Native.getPreferredUnits(identifiers);
|
|
290
|
-
return identifiers.map(i => units[i]);
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
const buildUnitWithPrefix = (prefix, unit) => `${prefix}${unit}`;
|
|
294
|
-
|
|
295
|
-
function deserializeCorrelation(s) {
|
|
296
|
-
return { ...s,
|
|
297
|
-
objects: s.objects.map(o => {
|
|
298
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
299
|
-
// @ts-ignore
|
|
300
|
-
if (o.quantity !== undefined) {
|
|
301
|
-
return deserializeSample(o);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
return deserializCategorySample(o);
|
|
305
|
-
}),
|
|
306
|
-
endDate: new Date(s.endDate),
|
|
307
|
-
startDate: new Date(s.startDate)
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
function ensureMetadata(metadata) {
|
|
312
|
-
return metadata || {};
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const queryCorrelationSamples = async (typeIdentifier, options) => {
|
|
316
|
-
const opts = prepareOptions(options);
|
|
317
|
-
const correlations = await Native.queryCorrelationSamples(typeIdentifier, opts.from, opts.to);
|
|
318
|
-
return correlations.map(deserializeCorrelation);
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
const saveCorrelationSample = async (typeIdentifier, samples, options) => {
|
|
322
|
-
const start = ((options === null || options === void 0 ? void 0 : options.start) || new Date()).toISOString();
|
|
323
|
-
const end = ((options === null || options === void 0 ? void 0 : options.end) || new Date()).toISOString();
|
|
324
|
-
return Native.saveCorrelationSample(typeIdentifier, samples, start, end, ensureMetadata(options === null || options === void 0 ? void 0 : options.metadata));
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
const saveWorkoutSample = async (typeIdentifier, quantities, _start, options) => {
|
|
328
|
-
const start = _start.toISOString();
|
|
329
|
-
|
|
330
|
-
const end = ((options === null || options === void 0 ? void 0 : options.end) || new Date()).toISOString();
|
|
331
|
-
return Native.saveWorkoutSample(typeIdentifier, quantities, start, end, ensureMetadata(options === null || options === void 0 ? void 0 : options.metadata));
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
const getWorkoutRoutes = async workoutUUID => Native.getWorkoutRoutes(workoutUUID);
|
|
335
|
-
|
|
1
|
+
import useHealthkitAuthorization from './hooks/useHealthkitAuthorization';
|
|
2
|
+
import useIsHealthDataAvailable from './hooks/useIsHealthDataAvailable';
|
|
3
|
+
import useMostRecentCategorySample from './hooks/useMostRecentCategorySample';
|
|
4
|
+
import useMostRecentQuantitySample from './hooks/useMostRecentQuantitySample';
|
|
5
|
+
import useMostRecentWorkout from './hooks/useMostRecentWorkout';
|
|
6
|
+
import useSubscribeToChanges from './hooks/useSubscribeToChanges';
|
|
7
|
+
import Native from './native-types';
|
|
8
|
+
import getDateOfBirth from './utils/getDateOfBirth';
|
|
9
|
+
import getMostRecentCategorySample from './utils/getMostRecentCategorySample';
|
|
10
|
+
import getMostRecentQuantitySample from './utils/getMostRecentQuantitySample';
|
|
11
|
+
import getMostRecentWorkout from './utils/getMostRecentWorkout';
|
|
12
|
+
import getPreferredUnit from './utils/getPreferredUnit';
|
|
13
|
+
import getPreferredUnits from './utils/getPreferredUnits';
|
|
14
|
+
import getRequestStatusForAuthorization from './utils/getRequestStatusForAuthorization';
|
|
15
|
+
import queryCategorySamples from './utils/queryCategorySamples';
|
|
16
|
+
import queryCorrelationSamples from './utils/queryCorrelationSamples';
|
|
17
|
+
import queryQuantitySamples from './utils/queryQuantitySamples';
|
|
18
|
+
import queryStatisticsForQuantity from './utils/queryStatisticsForQuantity';
|
|
19
|
+
import queryWorkouts from './utils/queryWorkouts';
|
|
20
|
+
import requestAuthorization from './utils/requestAuthorization';
|
|
21
|
+
import saveCategorySample from './utils/saveCategorySample';
|
|
22
|
+
import saveCorrelationSample from './utils/saveCorrelationSample';
|
|
23
|
+
import saveQuantitySample from './utils/saveQuantitySample';
|
|
24
|
+
import saveWorkoutSample from './utils/saveWorkoutSample';
|
|
25
|
+
import subscribeToChanges from './utils/subscribeToChanges';
|
|
336
26
|
const Healthkit = {
|
|
337
27
|
authorizationStatusFor: Native.authorizationStatusFor.bind(Native),
|
|
338
28
|
isHealthDataAvailable: Native.isHealthDataAvailable.bind(Native),
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
enableBackgroundDelivery: Native.enableBackgroundDelivery,
|
|
29
|
+
disableAllBackgroundDelivery: Native.disableAllBackgroundDelivery.bind(Native),
|
|
30
|
+
disableBackgroundDelivery: Native.disableBackgroundDelivery.bind(Native),
|
|
31
|
+
enableBackgroundDelivery: Native.enableBackgroundDelivery.bind(Native),
|
|
343
32
|
// simple convenience getters
|
|
344
33
|
getBiologicalSex: Native.getBiologicalSex.bind(Native),
|
|
345
34
|
getFitzpatrickSkinType: Native.getFitzpatrickSkinType.bind(Native),
|
|
346
|
-
getWheelchairUse: Native.getWheelchairUse,
|
|
35
|
+
getWheelchairUse: Native.getWheelchairUse.bind(Native),
|
|
347
36
|
getBloodType: Native.getBloodType.bind(Native),
|
|
37
|
+
getWorkoutRoutes: Native.getWorkoutRoutes.bind(Native),
|
|
348
38
|
getDateOfBirth,
|
|
349
39
|
getMostRecentQuantitySample,
|
|
350
40
|
getMostRecentCategorySample,
|
|
@@ -352,7 +42,6 @@ const Healthkit = {
|
|
|
352
42
|
getPreferredUnit,
|
|
353
43
|
getPreferredUnits,
|
|
354
44
|
getRequestStatusForAuthorization,
|
|
355
|
-
getWorkoutRoutes,
|
|
356
45
|
// query methods
|
|
357
46
|
queryCategorySamples,
|
|
358
47
|
queryCorrelationSamples,
|
|
@@ -372,36 +61,9 @@ const Healthkit = {
|
|
|
372
61
|
useMostRecentQuantitySample,
|
|
373
62
|
useMostRecentWorkout,
|
|
374
63
|
useSubscribeToChanges,
|
|
375
|
-
useIsHealthDataAvailable
|
|
376
|
-
|
|
377
|
-
useEffect(() => {
|
|
378
|
-
const init = async () => {
|
|
379
|
-
const res = await Native.isHealthDataAvailable();
|
|
380
|
-
setIsAvailable(res);
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
void init();
|
|
384
|
-
}, []);
|
|
385
|
-
return isAvailable;
|
|
386
|
-
},
|
|
387
|
-
useHealthkitAuthorization: (read, write) => {
|
|
388
|
-
const [status, setStatus] = useState(null);
|
|
389
|
-
const refreshAuthStatus = useCallback(async () => {
|
|
390
|
-
const auth = await getRequestStatusForAuthorization(read, write);
|
|
391
|
-
setStatus(auth);
|
|
392
|
-
return auth;
|
|
393
|
-
}, []);
|
|
394
|
-
const request = useCallback(async () => {
|
|
395
|
-
await requestAuthorization(read, write);
|
|
396
|
-
return refreshAuthStatus();
|
|
397
|
-
}, []);
|
|
398
|
-
useEffect(() => {
|
|
399
|
-
void refreshAuthStatus();
|
|
400
|
-
}, []);
|
|
401
|
-
return [status, request];
|
|
402
|
-
}
|
|
64
|
+
useIsHealthDataAvailable,
|
|
65
|
+
useHealthkitAuthorization
|
|
403
66
|
};
|
|
404
|
-
export * from './native-types';
|
|
405
67
|
export * from './types';
|
|
406
68
|
export default Healthkit;
|
|
407
69
|
//# sourceMappingURL=index.ios.js.map
|