@kingstinct/react-native-healthkit
Version:
React Native bindings for HealthKit
37 lines (36 loc) • 1.51 kB
JavaScript
import { useCallback, useEffect, useRef, useState } from 'react';
import { Core } from '../modules';
/**
* @description Hook to retrieve the current authorization status for the given types, and request authorization if needed.
* @see {@link https://developer.apple.com/documentation/healthkit/hkhealthstore/1614152-requestauthorization Apple Docs - requestAuthorization}
* @see {@link https://developer.apple.com/documentation/healthkit/authorizing_access_to_health_data Apple Docs - Authorizing access to health data}
*/
export const useHealthkitAuthorization = ({ toWrite, toRead, }) => {
const [status, setStatus] = useState(null);
const readMemo = useRef(toRead);
const writeMemo = useRef(toWrite);
useEffect(() => {
readMemo.current = toRead;
writeMemo.current = toWrite;
}, [toRead, toWrite]);
const refreshAuthStatus = useCallback(async () => {
const auth = await Core.getRequestStatusForAuthorization({
toShare: writeMemo.current,
toRead: readMemo.current,
});
setStatus(auth);
return auth;
}, []);
const request = useCallback(async () => {
await Core.requestAuthorization({
toShare: writeMemo.current,
toRead: readMemo.current,
});
return refreshAuthStatus();
}, [refreshAuthStatus]);
useEffect(() => {
void refreshAuthStatus();
}, [refreshAuthStatus]);
return [status, request];
};
export default useHealthkitAuthorization;