diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2f9e48b..5d37d8f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,7 +5,7 @@ name: Test # Controls when the action will run. on: push: - branches: [ "**" ] + pull_request: workflow_dispatch: # A workflow run is made up of one or more jobs that can run sequentially or in parallel diff --git a/README.md b/README.md index 086ee92..8aa538a 100644 --- a/README.md +++ b/README.md @@ -91,13 +91,26 @@ Some imperative examples: console.log(quantity) // 17.5 console.log(unit) // % - /* Listen to data */ await HealthKit.requestAuthorization([HKQuantityTypeIdentifier.heartRate]); // request read permission for heart rate - /* Make sure to request permissions before subscribing to changes */ - const unsubscribe = HealthKit.subscribeToChanges(HKQuantityTypeIdentifier.heartRate, () => { - // refetch whichever queries you need - }); + /* Subscribe to data (Make sure to request permissions before subscribing to changes) */ + const [hasRequestedAuthorization, setHasRequestedAuthorization] = useState(false); + + useEffect(() => { + HealthKit.requestAuthorization([HKQuantityTypeIdentifier.heartRate]).then(() => { + setHasRequestedAuthorization(true); + }); + }, []); + + useEffect(() => { + if (hasRequestedAuthorization) { + const unsubscribe = HealthKit.subscribeToChanges(HKQuantityTypeIdentifier.heartRate, () => { + // refetch data as needed + }); + } + + return () => unsubscribe(); + }, [hasRequestedAuthorization]); /* write data */ await HealthKit.requestAuthorization([], [HKQuantityTypeIdentifier.insulinDelivery]); // request write permission for insulin delivery diff --git a/src/hooks/useStatisticsForQuantity.ts b/src/hooks/useStatisticsForQuantity.ts index 4776f17..d1d2082 100644 --- a/src/hooks/useStatisticsForQuantity.ts +++ b/src/hooks/useStatisticsForQuantity.ts @@ -32,7 +32,7 @@ function useStatisticsForQuantity { void update() - }, []) + }, [update]) useSubscribeToChanges(identifier, update) diff --git a/src/native-types.ts b/src/native-types.ts index a66f050..8a9b2d4 100644 --- a/src/native-types.ts +++ b/src/native-types.ts @@ -1548,7 +1548,7 @@ export enum HKUnits { Percent = '%', Count = 'count', InternationalUnit = 'IU', - AppleEffortScore = "appleEffortScore" + AppleEffortScore = 'appleEffortScore' } export type MeterUnit =