@kingstinct/react-native-healthkit 4.4.6 → 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/ios/ReactNativeHealthkit.xcodeproj/project.xcworkspace/xcuserdata/robertherber.xcuserdatad/UserInterfaceState.xcuserstate +0 -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 +2 -15
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/native-types.js +170 -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 +3 -5
- package/lib/module/index.js.map +1 -1
- package/lib/module/native-types.js +154 -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 +169 -65
- 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 +5 -7
- package/src/native-types.ts +213 -74
- 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,36 @@
|
|
|
1
|
+
import Native from '../native-types'
|
|
2
|
+
import deserializeSample from './deserializeSample'
|
|
3
|
+
import ensureUnit from './ensureUnit'
|
|
4
|
+
import prepareOptions from './prepareOptions'
|
|
5
|
+
|
|
6
|
+
import type { HKQuantityTypeIdentifier, UnitForIdentifier } from '../native-types'
|
|
7
|
+
import type { GenericQueryOptions, HKQuantitySample } from '../types'
|
|
8
|
+
|
|
9
|
+
export type QueryQuantitySamplesFn = <
|
|
10
|
+
TIdentifier extends HKQuantityTypeIdentifier,
|
|
11
|
+
TUnit extends UnitForIdentifier<TIdentifier>
|
|
12
|
+
>(
|
|
13
|
+
identifier: TIdentifier,
|
|
14
|
+
options: GenericQueryOptions & { readonly unit?: TUnit }
|
|
15
|
+
) => Promise<readonly HKQuantitySample<TIdentifier>[]>;
|
|
16
|
+
|
|
17
|
+
const queryQuantitySamples: QueryQuantitySamplesFn = async (
|
|
18
|
+
identifier,
|
|
19
|
+
options,
|
|
20
|
+
) => {
|
|
21
|
+
const unit = await ensureUnit(identifier, options.unit)
|
|
22
|
+
const opts = prepareOptions(options)
|
|
23
|
+
|
|
24
|
+
const quantitySamples = await Native.queryQuantitySamples(
|
|
25
|
+
identifier,
|
|
26
|
+
unit,
|
|
27
|
+
opts.from,
|
|
28
|
+
opts.to,
|
|
29
|
+
opts.limit,
|
|
30
|
+
opts.ascending,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
return quantitySamples.map(deserializeSample)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default queryQuantitySamples
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import Native from '../native-types'
|
|
2
|
+
import ensureUnit from './ensureUnit'
|
|
3
|
+
|
|
4
|
+
import type { HKQuantityTypeIdentifier, HKStatisticsOptions, UnitForIdentifier } from '../native-types'
|
|
5
|
+
|
|
6
|
+
async function queryStatisticsForQuantity<TIdentifier extends HKQuantityTypeIdentifier, TUnit extends UnitForIdentifier<TIdentifier> = UnitForIdentifier<TIdentifier>>(
|
|
7
|
+
identifier: TIdentifier,
|
|
8
|
+
options: readonly HKStatisticsOptions[],
|
|
9
|
+
from: Date,
|
|
10
|
+
to?: Date,
|
|
11
|
+
unit?: TUnit,
|
|
12
|
+
) {
|
|
13
|
+
const actualUnit = await ensureUnit(identifier, unit)
|
|
14
|
+
const toDate = to || new Date()
|
|
15
|
+
const { mostRecentQuantityDateInterval, ...rawResponse } = await Native.queryStatisticsForQuantity(
|
|
16
|
+
identifier,
|
|
17
|
+
actualUnit,
|
|
18
|
+
from.toISOString(),
|
|
19
|
+
toDate.toISOString(),
|
|
20
|
+
options,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
const response = {
|
|
24
|
+
...rawResponse,
|
|
25
|
+
...(mostRecentQuantityDateInterval
|
|
26
|
+
? {
|
|
27
|
+
mostRecentQuantityDateInterval: {
|
|
28
|
+
from: new Date(mostRecentQuantityDateInterval.from),
|
|
29
|
+
to: new Date(mostRecentQuantityDateInterval.to),
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
: {}),
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return response
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default queryStatisticsForQuantity
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Native from '../native-types'
|
|
2
|
+
import deserializeWorkout from './deserializeWorkout'
|
|
3
|
+
import getPreferredUnitsTyped from './getPreferredUnitsTyped'
|
|
4
|
+
import prepareOptions from './prepareOptions'
|
|
5
|
+
|
|
6
|
+
import type { EnergyUnit, LengthUnit } from '../native-types'
|
|
7
|
+
import type { QueryWorkoutsOptions } from '../types'
|
|
8
|
+
|
|
9
|
+
async function queryWorkouts<
|
|
10
|
+
TEnergy extends EnergyUnit,
|
|
11
|
+
TDistance extends LengthUnit
|
|
12
|
+
>(options: QueryWorkoutsOptions<TEnergy, TDistance>) {
|
|
13
|
+
const { energyUnit, distanceUnit } = await getPreferredUnitsTyped(options)
|
|
14
|
+
const opts = prepareOptions(options)
|
|
15
|
+
|
|
16
|
+
const workouts = await Native.queryWorkoutSamples(
|
|
17
|
+
energyUnit,
|
|
18
|
+
distanceUnit,
|
|
19
|
+
opts.from,
|
|
20
|
+
opts.to,
|
|
21
|
+
opts.limit,
|
|
22
|
+
opts.ascending,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
return workouts.map(deserializeWorkout)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default queryWorkouts
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import Native from '../native-types'
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
HealthkitReadAuthorization, HealthkitWriteAuthorization, ReadPermissions, WritePermissions,
|
|
5
|
+
} from '../native-types'
|
|
6
|
+
|
|
7
|
+
const requestAuthorization = async (
|
|
8
|
+
read: readonly HealthkitReadAuthorization[],
|
|
9
|
+
write: readonly HealthkitWriteAuthorization[] = [],
|
|
10
|
+
): Promise<boolean> => {
|
|
11
|
+
const readPermissions = read.reduce((obj, cur) => ({ ...obj, [cur]: true }), {} as ReadPermissions)
|
|
12
|
+
|
|
13
|
+
const writePermissions = write.reduce((obj, cur) => ({ ...obj, [cur]: true }), {} as WritePermissions)
|
|
14
|
+
|
|
15
|
+
return Native.requestAuthorization(writePermissions, readPermissions)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default requestAuthorization
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import Native from '../native-types'
|
|
2
|
+
|
|
3
|
+
import type { HKCategoryTypeIdentifier, HKCategoryValueForIdentifier, MetadataMapperForCategoryIdentifier } from '../native-types'
|
|
4
|
+
|
|
5
|
+
async function saveCategorySample<T extends HKCategoryTypeIdentifier>(
|
|
6
|
+
identifier: T,
|
|
7
|
+
value: HKCategoryValueForIdentifier<T>,
|
|
8
|
+
options?: {
|
|
9
|
+
readonly start?: Date;
|
|
10
|
+
readonly end?: Date;
|
|
11
|
+
readonly metadata?: MetadataMapperForCategoryIdentifier<T>;
|
|
12
|
+
},
|
|
13
|
+
) {
|
|
14
|
+
const start = options?.start || options?.end || new Date()
|
|
15
|
+
const end = options?.end || options?.start || new Date()
|
|
16
|
+
const metadata = options?.metadata || {}
|
|
17
|
+
|
|
18
|
+
return Native.saveCategorySample(
|
|
19
|
+
identifier,
|
|
20
|
+
value,
|
|
21
|
+
start.toISOString(),
|
|
22
|
+
end.toISOString(),
|
|
23
|
+
metadata || {},
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default saveCategorySample
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import Native from '../native-types'
|
|
2
|
+
import ensureMetadata from './ensureMetadata'
|
|
3
|
+
|
|
4
|
+
import type { MetadataMapperForCorrelationIdentifier, HKCorrelationTypeIdentifier } from '../native-types'
|
|
5
|
+
import type { HKCategorySampleForSaving, HKQuantitySampleForSaving } from '../types'
|
|
6
|
+
|
|
7
|
+
async function saveCorrelationSample<
|
|
8
|
+
TIdentifier extends HKCorrelationTypeIdentifier,
|
|
9
|
+
TSamples extends readonly(
|
|
10
|
+
| HKCategorySampleForSaving
|
|
11
|
+
| HKQuantitySampleForSaving
|
|
12
|
+
)[]
|
|
13
|
+
>(
|
|
14
|
+
typeIdentifier: TIdentifier,
|
|
15
|
+
samples: TSamples,
|
|
16
|
+
options?: {
|
|
17
|
+
readonly start?: Date;
|
|
18
|
+
readonly end?: Date;
|
|
19
|
+
readonly metadata?: MetadataMapperForCorrelationIdentifier<TIdentifier>;
|
|
20
|
+
},
|
|
21
|
+
) {
|
|
22
|
+
const start = (options?.start || new Date()).toISOString()
|
|
23
|
+
const end = (options?.end || new Date()).toISOString()
|
|
24
|
+
|
|
25
|
+
return Native.saveCorrelationSample(
|
|
26
|
+
typeIdentifier,
|
|
27
|
+
samples.map((s) => ({ ...s, metadata: ensureMetadata(s.metadata) })),
|
|
28
|
+
start,
|
|
29
|
+
end,
|
|
30
|
+
ensureMetadata(options?.metadata),
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default saveCorrelationSample
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import Native from '../native-types'
|
|
2
|
+
|
|
3
|
+
import type { MetadataMapperForQuantityIdentifier, HKQuantityTypeIdentifier, UnitForIdentifier } from '../native-types'
|
|
4
|
+
|
|
5
|
+
async function saveQuantitySample<TType extends HKQuantityTypeIdentifier>(
|
|
6
|
+
identifier: TType,
|
|
7
|
+
unit: UnitForIdentifier<TType>,
|
|
8
|
+
value: number,
|
|
9
|
+
options?: {
|
|
10
|
+
readonly start?: Date;
|
|
11
|
+
readonly end?: Date;
|
|
12
|
+
readonly metadata?: MetadataMapperForQuantityIdentifier<TType>;
|
|
13
|
+
},
|
|
14
|
+
) {
|
|
15
|
+
const start = options?.start || options?.end || new Date()
|
|
16
|
+
const end = options?.end || options?.start || new Date()
|
|
17
|
+
const metadata = options?.metadata || {}
|
|
18
|
+
|
|
19
|
+
return Native.saveQuantitySample(
|
|
20
|
+
identifier,
|
|
21
|
+
unit,
|
|
22
|
+
value,
|
|
23
|
+
start.toISOString(),
|
|
24
|
+
end.toISOString(),
|
|
25
|
+
metadata,
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default saveQuantitySample
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Native from '../native-types'
|
|
2
|
+
import ensureMetadata from './ensureMetadata'
|
|
3
|
+
|
|
4
|
+
import type { HKWorkoutActivityType, HKWorkoutMetadata } from '../native-types'
|
|
5
|
+
import type { HKQuantitySampleForSaving } from '../types'
|
|
6
|
+
|
|
7
|
+
async function saveWorkoutSample<TIdentifier extends HKWorkoutActivityType>(
|
|
8
|
+
typeIdentifier: TIdentifier,
|
|
9
|
+
quantities: readonly HKQuantitySampleForSaving[],
|
|
10
|
+
_start: Date,
|
|
11
|
+
options?: {
|
|
12
|
+
readonly end?: Date;
|
|
13
|
+
readonly metadata?: HKWorkoutMetadata;
|
|
14
|
+
},
|
|
15
|
+
) {
|
|
16
|
+
const start = _start.toISOString()
|
|
17
|
+
const end = (options?.end || new Date()).toISOString()
|
|
18
|
+
|
|
19
|
+
return Native.saveWorkoutSample(
|
|
20
|
+
typeIdentifier,
|
|
21
|
+
quantities.map(((s) => ({ ...s, metadata: ensureMetadata(s.metadata) }))),
|
|
22
|
+
start,
|
|
23
|
+
end,
|
|
24
|
+
ensureMetadata(options?.metadata),
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default saveWorkoutSample
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { EventEmitter } from '..'
|
|
2
|
+
import Native from '../native-types'
|
|
3
|
+
|
|
4
|
+
import type { HKSampleTypeIdentifier } from '..'
|
|
5
|
+
|
|
6
|
+
const subscribeToChanges = async (
|
|
7
|
+
identifier: HKSampleTypeIdentifier,
|
|
8
|
+
callback: () => void,
|
|
9
|
+
) => {
|
|
10
|
+
const subscription = EventEmitter.addListener(
|
|
11
|
+
'onChange',
|
|
12
|
+
({ typeIdentifier }) => {
|
|
13
|
+
if (typeIdentifier === identifier) {
|
|
14
|
+
callback()
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
const queryId = await Native.subscribeToObserverQuery(identifier).catch(
|
|
20
|
+
async (error) => {
|
|
21
|
+
subscription.remove()
|
|
22
|
+
return Promise.reject(error)
|
|
23
|
+
},
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
return async () => {
|
|
27
|
+
subscription.remove()
|
|
28
|
+
return Native.unsubscribeQuery(queryId)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default subscribeToChanges
|