UNPKG

@kingstinct/react-native-healthkit

Version:
41 lines (40 loc) 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useHealthkitAuthorization = void 0; const react_1 = require("react"); const modules_1 = require("../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} */ const useHealthkitAuthorization = ({ toWrite, toRead, }) => { const [status, setStatus] = (0, react_1.useState)(null); const readMemo = (0, react_1.useRef)(toRead); const writeMemo = (0, react_1.useRef)(toWrite); (0, react_1.useEffect)(() => { readMemo.current = toRead; writeMemo.current = toWrite; }, [toRead, toWrite]); const refreshAuthStatus = (0, react_1.useCallback)(async () => { const auth = await modules_1.Core.getRequestStatusForAuthorization({ toShare: writeMemo.current, toRead: readMemo.current, }); setStatus(auth); return auth; }, []); const request = (0, react_1.useCallback)(async () => { await modules_1.Core.requestAuthorization({ toShare: writeMemo.current, toRead: readMemo.current, }); return refreshAuthStatus(); }, [refreshAuthStatus]); (0, react_1.useEffect)(() => { void refreshAuthStatus(); }, [refreshAuthStatus]); return [status, request]; }; exports.useHealthkitAuthorization = useHealthkitAuthorization; exports.default = exports.useHealthkitAuthorization;