import RookSdk from '../NativeRookSdk'; import { useRookSyncGateContext } from '../context/RookSyncGateContext'; import { isModuleReady } from '../utils/isModuleReady'; import isRunningOnAndroid from '../utils/isRunningOnAndroid'; export const useRookHealthConnect = () => { const { state: { ready, enableLogs }, } = useRookSyncGateContext(); /** * Schedules the background synchronization of health data using Rook. * * This function enables automatic background data synchronization with Rook. * It ensures that health data is periodically fetched and sent to Rook’s API * based on the defined configuration. * * If the scheduling is successful, the promise resolves with true. * If an error occurs, the promise rejects with an error message. * * @param promise A React Native promise that resolves when the sync is scheduled successfully or rejects with an error. */ const scheduleBackgroundSync = (): Promise => { isRunningOnAndroid(); isModuleReady(ready); return RookSdk.scheduleBackgroundSync(enableLogs); }; /** * Cancels the scheduled background synchronization of health data. * * This function stops the automatic background sync previously scheduled using Rook. * It ensures that no further health data is fetched and sent until scheduled again. * * If the cancellation is successful, the promise resolves with true. * If an error occurs, the promise rejects with an error message. * * @param promise A React Native promise that resolves when the sync is canceled successfully or rejects with an error. */ const cancelBackgroundSync = (): Promise => { isRunningOnAndroid(); isModuleReady(ready); return RookSdk.cancelBackgroundSync(); }; const isBackgroundSyncEnabled = (): Promise => { isRunningOnAndroid(); isModuleReady(ready); return RookSdk.isBackgroundSyncEnabled(); }; return { ready, scheduleBackgroundSync, cancelBackgroundSync, isBackgroundSyncEnabled, }; };