import RookSdk, { type HeartRateData } from '../NativeRookSdk'; import { Platform } from 'react-native'; import { isModuleReady } from '../utils/isModuleReady'; import { useRookSyncGateContext } from '../context/RookSyncGateContext'; import { SDKDataSource } from '../types/SDKSources'; export type Calories = { active: number; basal: number; }; /** * The `useRookVariables` hook returns an object with a `ready` boolean and an * asynchronous function `getTodaySteps` that retrieves the number of steps taken today. * @returns The `useRookVariable` hook is returning an object with two properties: * 1. `ready`: This property is coming from the `useRookSyncGateContext` hook and represents the state * of readiness. * 2. `getTodaySteps`: This property is a function that retrieves the number of steps taken today * asynchronously and returns a Promise representing the number of steps taken today. * 3. `getTodayCalories`: This property is a function that retrieves the number of calories burned today * asynchronously and returns a Promise representing the number of calories burned today. */ export const useRookVariables = () => { const { state: { ready }, } = useRookSyncGateContext(); /** * The function `getTodaySteps` retrieves the number of steps taken today asynchronously. * @returns The function `getTodaySteps` is returning a string representing the number of steps taken today. */ const getTodaySteps = async (source: SDKDataSource): Promise => { isModuleReady(ready); if (Platform.OS === 'android' && source === SDKDataSource.HEALTH_CONNECT) { return RookSdk.syncTodayHealthConnectStepsCount(); } if (Platform.OS === 'android' && source === SDKDataSource.SAMSUNG_HEALTH) { return RookSdk.syncTodaySamsungHealthStepsCount(); } if (Platform.OS !== 'android' && source === SDKDataSource.APPLE_HEALTH) { return RookSdk.getTodayAppleHealthSteps(); } throw new Error('Invalid SDK datasource'); }; /** * The function `syncTodayCaloriesCount` retrieves the number of calories burned today asynchronously as string * @returns The function `syncTodayCaloriesCount` is returning the result of calling */ const getTodayCalories = async (source: SDKDataSource): Promise => { isModuleReady(ready); if (Platform.OS === 'android' && source === SDKDataSource.HEALTH_CONNECT) { return RookSdk.syncTodayCaloriesCount(); } if (Platform.OS === 'android' && source === SDKDataSource.SAMSUNG_HEALTH) { return RookSdk.syncSHTodayCaloriesCount(); } if (Platform.OS !== 'android' && source === SDKDataSource.APPLE_HEALTH) { return RookSdk.syncTodayCaloriesCount(); } throw new Error('Invalid SDK datasource'); }; const getTodayHeartRate = async ( source: SDKDataSource ): Promise => { if (Platform.OS === 'android' && source === SDKDataSource.HEALTH_CONNECT) { const result = await RookSdk.getTodayHeartRate(); return JSON.parse(result as unknown as string); } if (Platform.OS === 'android' && source === SDKDataSource.SAMSUNG_HEALTH) { const result = await RookSdk.getSHTodayHeartRate(); return JSON.parse(result as unknown as string); } if (Platform.OS !== 'android' && source === SDKDataSource.APPLE_HEALTH) { return RookSdk.getTodayHeartRate(); } throw new Error('Please check the data source'); }; return { ready, getTodaySteps, getTodayCalories, getTodayHeartRate, }; };