react-native-rook-sdk 4.0.0 → 5.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +35 -3
- package/RNRookSdk.podspec +22 -0
- package/android/build.gradle +21 -40
- package/android/gradle.properties +5 -5
- package/android/src/main/AndroidManifest.xml +1 -2
- package/android/src/main/java/com/rooksdk/RookSdkModule.kt +1709 -117
- package/android/src/main/java/com/rooksdk/RookSdkPackage.kt +26 -42
- package/android/src/main/java/com/rooksdk/broadcasts/AndroidPermissionsReceiverTransmitter.kt +34 -37
- package/android/src/main/java/com/rooksdk/broadcasts/HealthConnectPermissionsReceiverTransmitter.kt +8 -14
- package/android/src/main/java/com/rooksdk/broadcasts/SamsungHealthPermissionsReceiverTransmitter.kt +8 -16
- package/android/src/main/java/com/rooksdk/utils/{DatasourcesUtils.kt → DatasourceUtils.kt} +10 -9
- package/android/src/main/java/com/rooksdk/utils/{PermissionConversion.kt → PermissionConvertions.kt} +1 -1
- package/android/src/main/java/com/rooksdk/utils/ReadableToWritable.kt +0 -1
- package/android/src/main/java/com/rooksdk/utils/RookGsonBuilder.kt +3 -0
- package/android/src/main/java/com/rooksdk/utils/SamsungAvailability.kt +1 -0
- package/android/src/main/java/com/rooksdk/utils/serializers/InstantSerializer.kt +0 -1
- package/android/src/main/java/com/rooksdk/utils/serializers/ZoneDateTimeSerializer.kt +18 -0
- package/ios/DateHelper.swift +4 -4
- package/ios/Encodable.swift +25 -0
- package/ios/EncodableDataSource.swift +2 -3
- package/ios/EncodableDataSourceAuthorizer.swift +2 -3
- package/ios/EncodableSDKState.swift +3 -2
- package/ios/{EncodableHeartRate.swift → HearRateMapper.swift} +3 -3
- package/ios/NutritionMapper.swift +3 -3
- package/ios/RookBackground.swift +5 -8
- package/ios/RookConfiguration.swift +33 -30
- package/ios/{RookLocalData.swift → RookData.swift} +76 -69
- package/ios/RookEntry.swift +62 -0
- package/ios/RookPermissions.swift +36 -40
- package/ios/RookSdk-Bridging-Header.h +12 -0
- package/ios/RookSdk.h +3 -3
- package/ios/RookSdk.mm +390 -134
- package/ios/RookSources.swift +60 -54
- package/ios/RookSync.swift +48 -78
- package/ios/boost-boost_privacy-Bridging-Header.h +4 -0
- package/lib/{commonjs/types/AppleHealthPermissions.js → module/NativeRookSdk.js} +50 -6
- package/lib/module/NativeRookSdk.js.map +1 -0
- package/lib/module/context/RookSyncGateContext.js +2 -0
- package/lib/module/context/RookSyncGateContext.js.map +1 -1
- package/lib/module/context/RookSyncGateProvider.js +17 -18
- package/lib/module/context/RookSyncGateProvider.js.map +1 -1
- package/lib/module/context/RookSyncGateReducer.js +2 -0
- package/lib/module/context/RookSyncGateReducer.js.map +1 -1
- package/lib/module/context/RookSyncGateTypes.js +2 -0
- package/lib/module/hooks/useRookAPISources.js +10 -16
- package/lib/module/hooks/useRookAPISources.js.map +1 -1
- package/lib/module/hooks/useRookAndroidBackgroundSteps.js +11 -12
- package/lib/module/hooks/useRookAndroidBackgroundSteps.js.map +1 -1
- package/lib/module/hooks/useRookAppleHealth.js +9 -10
- package/lib/module/hooks/useRookAppleHealth.js.map +1 -1
- package/lib/module/hooks/useRookConfiguration.js +17 -22
- package/lib/module/hooks/useRookConfiguration.js.map +1 -1
- package/lib/module/hooks/useRookData.js +33 -38
- package/lib/module/hooks/useRookData.js.map +1 -1
- package/lib/module/hooks/useRookHealthConnect.js +9 -10
- package/lib/module/hooks/useRookHealthConnect.js.map +1 -1
- package/lib/module/hooks/useRookPermissions.js +34 -45
- package/lib/module/hooks/useRookPermissions.js.map +1 -1
- package/lib/module/hooks/useRookSamsungHealth.js +8 -20
- package/lib/module/hooks/useRookSamsungHealth.js.map +1 -1
- package/lib/module/hooks/useRookSync.js +23 -32
- package/lib/module/hooks/useRookSync.js.map +1 -1
- package/lib/module/hooks/useRookVariables.js +26 -29
- package/lib/module/hooks/useRookVariables.js.map +1 -1
- package/lib/module/index.js +18 -10
- package/lib/module/index.js.map +1 -1
- package/lib/module/modules/components/RookStateManager.js +31 -0
- package/lib/module/modules/components/RookStateManager.js.map +1 -0
- package/lib/module/modules/hook/useRookAutoSync.js +24 -36
- package/lib/module/modules/hook/useRookAutoSync.js.map +1 -1
- package/lib/module/modules/hook/useRookEmitter.js +61 -0
- package/lib/module/modules/hook/useRookEmitter.js.map +1 -0
- package/lib/module/modules/types/sendMessageBroadcast.js +1 -1
- package/lib/module/modules/utils/errors.js +2 -0
- package/lib/module/modules/utils/errors.js.map +1 -1
- package/lib/module/package.json +1 -0
- package/lib/module/types/ActivityEvent.js +2 -0
- package/lib/module/types/AppleHealthNutritionEvent.js +2 -0
- package/lib/module/types/AppleHealthNutritionEvent.js.map +1 -1
- package/lib/module/types/AppleHealthPermissions.js +2 -0
- package/lib/module/types/AppleHealthPermissions.js.map +1 -1
- package/lib/module/types/AuthorizedSources.js +2 -0
- package/lib/module/types/BodySummary.js +2 -0
- package/lib/module/types/DataSource.js +2 -0
- package/lib/module/types/DataSource.js.map +1 -1
- package/lib/module/types/HCMealData.js +3 -1
- package/lib/module/types/HCMealData.js.map +1 -1
- package/lib/module/types/HeartRateEvent.js +1 -1
- package/lib/module/types/PermissionsType.js +1 -1
- package/lib/module/types/PhysicalSummary.js +2 -0
- package/lib/module/types/Rook.js +2 -0
- package/lib/{commonjs/types/BodySummary.js.map → module/types/Rook.js.map} +1 -1
- package/lib/module/types/SDKSources.js +2 -0
- package/lib/module/types/SDKSources.js.map +1 -1
- package/lib/module/types/SamsungHealthPermissions.js +3 -0
- package/lib/module/types/SamsungHealthPermissions.js.map +1 -1
- package/lib/module/types/SleepSummary.js +2 -0
- package/lib/module/types/SummaryTypes.js +1 -1
- package/lib/module/types/SyncTypes.js +2 -0
- package/lib/module/types/SyncTypes.js.map +1 -1
- package/lib/module/types/WriteNutrition.js +2 -0
- package/lib/module/types/WriteNutrition.js.map +1 -1
- package/lib/module/utils/isModuleReady.js +2 -0
- package/lib/module/utils/isModuleReady.js.map +1 -1
- package/lib/module/utils/isRunningOnAndroid.js +2 -0
- package/lib/module/utils/isRunningOnAndroid.js.map +1 -1
- package/lib/module/utils/isRunningOniOS.js +2 -0
- package/lib/module/utils/isRunningOniOS.js.map +1 -1
- package/lib/module/utils/isValidDate.js +2 -0
- package/lib/module/utils/isValidDate.js.map +1 -1
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeRookSdk.d.ts +705 -0
- package/lib/typescript/src/NativeRookSdk.d.ts.map +1 -0
- package/lib/typescript/src/context/RookSyncGateContext.d.ts.map +1 -1
- package/lib/typescript/src/context/RookSyncGateProvider.d.ts.map +1 -1
- package/lib/typescript/src/context/RookSyncGateReducer.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookAPISources.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookAndroidBackgroundSteps.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookAppleHealth.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookConfiguration.d.ts +1 -1
- package/lib/typescript/src/hooks/useRookConfiguration.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookData.d.ts +1 -1
- package/lib/typescript/src/hooks/useRookData.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookHealthConnect.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookPermissions.d.ts +1 -1
- package/lib/typescript/src/hooks/useRookPermissions.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookSamsungHealth.d.ts +0 -6
- package/lib/typescript/src/hooks/useRookSamsungHealth.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookSync.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useRookVariables.d.ts +1 -1
- package/lib/typescript/src/hooks/useRookVariables.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +17 -8
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/modules/components/RookStateManager.d.ts +10 -0
- package/lib/typescript/src/modules/components/RookStateManager.d.ts.map +1 -0
- package/lib/typescript/src/modules/hook/useRookAutoSync.d.ts.map +1 -1
- package/lib/typescript/src/modules/hook/useRookEmitter.d.ts +4 -0
- package/lib/typescript/src/modules/hook/useRookEmitter.d.ts.map +1 -0
- package/lib/typescript/src/types/AppleHealthNutritionEvent.d.ts.map +1 -1
- package/lib/typescript/src/types/AuthorizedSources.d.ts +11 -0
- package/lib/typescript/src/types/AuthorizedSources.d.ts.map +1 -1
- package/lib/typescript/src/types/HCMealData.d.ts.map +1 -1
- package/lib/typescript/src/types/HeartRateEvent.d.ts +2 -2
- package/lib/typescript/src/types/HeartRateEvent.d.ts.map +1 -1
- package/lib/typescript/src/types/Rook.d.ts +9 -0
- package/lib/typescript/src/types/Rook.d.ts.map +1 -0
- package/lib/typescript/src/types/SamsungHealthPermissions.d.ts +1 -0
- package/lib/typescript/src/types/SamsungHealthPermissions.d.ts.map +1 -1
- package/lib/typescript/src/types/SyncTypes.d.ts +5 -1
- package/lib/typescript/src/types/SyncTypes.d.ts.map +1 -1
- package/lib/typescript/src/utils/isModuleReady.d.ts.map +1 -1
- package/lib/typescript/src/utils/isValidDate.d.ts.map +1 -1
- package/package.json +94 -98
- package/src/NativeRookSdk.ts +839 -0
- package/{lib/commonjs/context/RookSyncGateContext.js → src/context/RookSyncGateContext.ts} +10 -13
- package/src/context/RookSyncGateProvider.tsx +82 -0
- package/{lib/commonjs/context/RookSyncGateReducer.js → src/context/RookSyncGateReducer.ts} +14 -41
- package/src/context/RookSyncGateTypes.ts +40 -0
- package/{lib/commonjs/hooks/useRookAPISources.js → src/hooks/useRookAPISources.ts} +40 -36
- package/src/hooks/useRookAndroidBackgroundSteps.ts +81 -0
- package/src/hooks/useRookAppleHealth.ts +55 -0
- package/src/hooks/useRookConfiguration.ts +106 -0
- package/src/hooks/useRookData.ts +133 -0
- package/src/hooks/useRookHealthConnect.ts +59 -0
- package/src/hooks/useRookPermissions.ts +240 -0
- package/src/hooks/useRookSamsungHealth.ts +37 -0
- package/src/hooks/useRookSync.ts +157 -0
- package/src/hooks/useRookVariables.ts +98 -0
- package/src/index.tsx +28 -0
- package/src/modules/components/RookStateManager.tsx +41 -0
- package/{lib/commonjs/modules/hook/useRookAutoSync.js → src/modules/hook/useRookAutoSync.ts} +141 -95
- package/src/modules/hook/useRookEmitter.ts +94 -0
- package/src/modules/types/sendMessageBroadcast.ts +5 -0
- package/src/modules/utils/errors.ts +9 -0
- package/src/types/ActivityEvent.ts +159 -0
- package/src/types/AppleHealthNutritionEvent.ts +142 -0
- package/src/types/AppleHealthPermissions.ts +49 -0
- package/src/types/AuthorizedSources.ts +33 -0
- package/src/types/BodySummary.ts +116 -0
- package/src/types/DataSource.ts +16 -0
- package/{lib/commonjs/types/HCMealData.js → src/types/HCMealData.ts} +60 -11
- package/src/types/HeartRateEvent.ts +26 -0
- package/src/types/PermissionsType.ts +9 -0
- package/src/types/PhysicalSummary.ts +84 -0
- package/src/types/Rook.ts +8 -0
- package/src/types/SDKSources.ts +5 -0
- package/src/types/SamsungHealthPermissions.ts +16 -0
- package/src/types/SleepSummary.ts +66 -0
- package/src/types/SummaryTypes.ts +70 -0
- package/src/types/SyncTypes.ts +63 -0
- package/src/types/WriteNutrition.ts +66 -0
- package/{lib/commonjs/utils/isModuleReady.js → src/utils/isModuleReady.ts} +4 -10
- package/src/utils/isRunningOnAndroid.ts +10 -0
- package/src/utils/isRunningOniOS.ts +10 -0
- package/{lib/commonjs/utils/isValidDate.js → src/utils/isValidDate.ts} +12 -14
- package/android/src/main/AndroidManifestNew.xml +0 -36
- package/android/src/main/java/com/rooksdk/modules/RookBackgroundSyncModule.kt +0 -99
- package/android/src/main/java/com/rooksdk/modules/RookConfigurationModule.kt +0 -182
- package/android/src/main/java/com/rooksdk/modules/RookLocalData.kt +0 -301
- package/android/src/main/java/com/rooksdk/modules/RookPermissionsModule.kt +0 -359
- package/android/src/main/java/com/rooksdk/modules/RookSourcesModule.kt +0 -102
- package/android/src/main/java/com/rooksdk/modules/RookStepsModule.kt +0 -119
- package/android/src/main/java/com/rooksdk/modules/RookSyncModule.kt +0 -551
- package/android/src/main/res/drawable/ic_health_connect.xml +0 -19
- package/android/src/main/res/values/themes.xml +0 -3
- package/ios/EncodableRookDataSource.swift +0 -36
- package/ios/EncodableStatusDataSources.swift +0 -47
- package/ios/RookBackgroundModule.h +0 -5
- package/ios/RookBackgroundModule.mm +0 -15
- package/ios/RookConfigurationModule.h +0 -5
- package/ios/RookConfigurationModule.mm +0 -22
- package/ios/RookExternalModule.h +0 -5
- package/ios/RookExternalModule.m +0 -31
- package/ios/RookPermissionsModule.h +0 -5
- package/ios/RookPermissionsModule.mm +0 -20
- package/ios/RookSourcesModule.h +0 -5
- package/ios/RookSourcesModule.mm +0 -18
- package/ios/RookSync.h +0 -5
- package/ios/RookSync.m +0 -35
- package/ios/SwiftTest.swift +0 -18
- package/ios/react-native-rook-sdk-Bridging-Header.h +0 -12
- package/lib/commonjs/context/RookSyncGateContext.js.map +0 -1
- package/lib/commonjs/context/RookSyncGateProvider.js +0 -108
- package/lib/commonjs/context/RookSyncGateProvider.js.map +0 -1
- package/lib/commonjs/context/RookSyncGateReducer.js.map +0 -1
- package/lib/commonjs/context/RookSyncGateTypes.js +0 -6
- package/lib/commonjs/context/RookSyncGateTypes.js.map +0 -1
- package/lib/commonjs/context/index.js +0 -14
- package/lib/commonjs/context/index.js.map +0 -1
- package/lib/commonjs/hooks/index.js +0 -76
- package/lib/commonjs/hooks/index.js.map +0 -1
- package/lib/commonjs/hooks/useRookAPISources.js.map +0 -1
- package/lib/commonjs/hooks/useRookAndroidBackgroundSteps.js +0 -88
- package/lib/commonjs/hooks/useRookAndroidBackgroundSteps.js.map +0 -1
- package/lib/commonjs/hooks/useRookAppleHealth.js +0 -63
- package/lib/commonjs/hooks/useRookAppleHealth.js.map +0 -1
- package/lib/commonjs/hooks/useRookConfiguration.js +0 -113
- package/lib/commonjs/hooks/useRookConfiguration.js.map +0 -1
- package/lib/commonjs/hooks/useRookData.js +0 -116
- package/lib/commonjs/hooks/useRookData.js.map +0 -1
- package/lib/commonjs/hooks/useRookHealthConnect.js +0 -70
- package/lib/commonjs/hooks/useRookHealthConnect.js.map +0 -1
- package/lib/commonjs/hooks/useRookPermissions.js +0 -211
- package/lib/commonjs/hooks/useRookPermissions.js.map +0 -1
- package/lib/commonjs/hooks/useRookSamsungHealth.js +0 -54
- package/lib/commonjs/hooks/useRookSamsungHealth.js.map +0 -1
- package/lib/commonjs/hooks/useRookSync.js +0 -148
- package/lib/commonjs/hooks/useRookSync.js.map +0 -1
- package/lib/commonjs/hooks/useRookVariables.js +0 -93
- package/lib/commonjs/hooks/useRookVariables.js.map +0 -1
- package/lib/commonjs/index.js +0 -121
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/modules/hook/useRookAutoSync.js.map +0 -1
- package/lib/commonjs/modules/types/sendMessageBroadcast.js +0 -2
- package/lib/commonjs/modules/types/sendMessageBroadcast.js.map +0 -1
- package/lib/commonjs/modules/utils/errors.js +0 -12
- package/lib/commonjs/modules/utils/errors.js.map +0 -1
- package/lib/commonjs/types/ActivityEvent.js +0 -6
- package/lib/commonjs/types/ActivityEvent.js.map +0 -1
- package/lib/commonjs/types/AppleHealthNutritionEvent.js +0 -77
- package/lib/commonjs/types/AppleHealthNutritionEvent.js.map +0 -1
- package/lib/commonjs/types/AppleHealthPermissions.js.map +0 -1
- package/lib/commonjs/types/AuthorizedSources.js +0 -6
- package/lib/commonjs/types/AuthorizedSources.js.map +0 -1
- package/lib/commonjs/types/BodySummary.js +0 -6
- package/lib/commonjs/types/DataSource.js +0 -16
- package/lib/commonjs/types/DataSource.js.map +0 -1
- package/lib/commonjs/types/DiagnosticState.js +0 -2
- package/lib/commonjs/types/DiagnosticState.js.map +0 -1
- package/lib/commonjs/types/HCMealData.js.map +0 -1
- package/lib/commonjs/types/HeartRateEvent.js +0 -2
- package/lib/commonjs/types/HeartRateEvent.js.map +0 -1
- package/lib/commonjs/types/PermissionsType.js +0 -2
- package/lib/commonjs/types/PermissionsType.js.map +0 -1
- package/lib/commonjs/types/PhysicalSummary.js +0 -6
- package/lib/commonjs/types/PhysicalSummary.js.map +0 -1
- package/lib/commonjs/types/SDKSources.js +0 -13
- package/lib/commonjs/types/SDKSources.js.map +0 -1
- package/lib/commonjs/types/SamsungHealthPermissions.js +0 -23
- package/lib/commonjs/types/SamsungHealthPermissions.js.map +0 -1
- package/lib/commonjs/types/SleepSummary.js +0 -6
- package/lib/commonjs/types/SleepSummary.js.map +0 -1
- package/lib/commonjs/types/SummaryTypes.js +0 -2
- package/lib/commonjs/types/SummaryTypes.js.map +0 -1
- package/lib/commonjs/types/SyncTypes.js +0 -33
- package/lib/commonjs/types/SyncTypes.js.map +0 -1
- package/lib/commonjs/types/WriteNutrition.js +0 -18
- package/lib/commonjs/types/WriteNutrition.js.map +0 -1
- package/lib/commonjs/utils/getNativeModule.js +0 -29
- package/lib/commonjs/utils/getNativeModule.js.map +0 -1
- package/lib/commonjs/utils/getRookModule.js +0 -28
- package/lib/commonjs/utils/getRookModule.js.map +0 -1
- package/lib/commonjs/utils/isModuleReady.js.map +0 -1
- package/lib/commonjs/utils/isRunningOnAndroid.js +0 -15
- package/lib/commonjs/utils/isRunningOnAndroid.js.map +0 -1
- package/lib/commonjs/utils/isRunningOniOS.js +0 -15
- package/lib/commonjs/utils/isRunningOniOS.js.map +0 -1
- package/lib/commonjs/utils/isValidDate.js.map +0 -1
- package/lib/commonjs/utils/nativeModules.js +0 -29
- package/lib/commonjs/utils/nativeModules.js.map +0 -1
- package/lib/commonjs/utils/packageInfo.js +0 -13
- package/lib/commonjs/utils/packageInfo.js.map +0 -1
- package/lib/module/context/index.js +0 -2
- package/lib/module/context/index.js.map +0 -1
- package/lib/module/hooks/index.js +0 -11
- package/lib/module/hooks/index.js.map +0 -1
- package/lib/module/types/DiagnosticState.js +0 -2
- package/lib/module/types/DiagnosticState.js.map +0 -1
- package/lib/module/utils/getNativeModule.js +0 -23
- package/lib/module/utils/getNativeModule.js.map +0 -1
- package/lib/module/utils/getRookModule.js +0 -22
- package/lib/module/utils/getRookModule.js.map +0 -1
- package/lib/module/utils/nativeModules.js +0 -23
- package/lib/module/utils/nativeModules.js.map +0 -1
- package/lib/module/utils/packageInfo.js +0 -7
- package/lib/module/utils/packageInfo.js.map +0 -1
- package/lib/typescript/src/context/index.d.ts +0 -2
- package/lib/typescript/src/context/index.d.ts.map +0 -1
- package/lib/typescript/src/hooks/index.d.ts +0 -11
- package/lib/typescript/src/hooks/index.d.ts.map +0 -1
- package/lib/typescript/src/types/DiagnosticState.d.ts +0 -13
- package/lib/typescript/src/types/DiagnosticState.d.ts.map +0 -1
- package/lib/typescript/src/utils/getNativeModule.d.ts +0 -10
- package/lib/typescript/src/utils/getNativeModule.d.ts.map +0 -1
- package/lib/typescript/src/utils/getRookModule.d.ts +0 -9
- package/lib/typescript/src/utils/getRookModule.d.ts.map +0 -1
- package/lib/typescript/src/utils/nativeModules.d.ts +0 -24
- package/lib/typescript/src/utils/nativeModules.d.ts.map +0 -1
- package/lib/typescript/src/utils/packageInfo.d.ts +0 -6
- package/lib/typescript/src/utils/packageInfo.d.ts.map +0 -1
- package/react-native-rook-sdk.podspec +0 -45
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import RookSdk, { type WriteNutrition } from '../NativeRookSdk';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
3
|
+
|
|
4
|
+
import { isModuleReady } from '../utils/isModuleReady';
|
|
5
|
+
import { isValidDate, isValidZonedDateTime } from '../utils/isValidDate';
|
|
6
|
+
import { useRookSyncGateContext } from '../context/RookSyncGateContext';
|
|
7
|
+
import { SDKDataSource } from '../types/SDKSources';
|
|
8
|
+
import { type SleepSummary } from '../types/SleepSummary';
|
|
9
|
+
import { type PhysicalSummary } from '../types/PhysicalSummary';
|
|
10
|
+
import { type BodySummary } from '../types/BodySummary';
|
|
11
|
+
import { type ActivityEvent } from '../types/ActivityEvent';
|
|
12
|
+
import { parseWriteNutritionToHC } from '../types/HCMealData';
|
|
13
|
+
import { parseWriteNutritionToAH } from '../types/AppleHealthNutritionEvent';
|
|
14
|
+
|
|
15
|
+
type Params = {
|
|
16
|
+
date: string;
|
|
17
|
+
source: SDKDataSource;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const useRookData = () => {
|
|
21
|
+
const {
|
|
22
|
+
state: { ready },
|
|
23
|
+
} = useRookSyncGateContext();
|
|
24
|
+
|
|
25
|
+
const getSleepSummary = async ({
|
|
26
|
+
date,
|
|
27
|
+
source,
|
|
28
|
+
}: Params): Promise<SleepSummary[]> => {
|
|
29
|
+
isModuleReady(ready);
|
|
30
|
+
isValidDate(date);
|
|
31
|
+
|
|
32
|
+
if (Platform.OS === 'android' && source === SDKDataSource.SAMSUNG_HEALTH) {
|
|
33
|
+
const result = await RookSdk.getSamsungSleepSummary(date);
|
|
34
|
+
return JSON.parse(result);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (Platform.OS === 'android' && source === SDKDataSource.HEALTH_CONNECT) {
|
|
38
|
+
const result = await RookSdk.getHealthConnectSleepSummary(date);
|
|
39
|
+
return JSON.parse(result);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return RookSdk.getAppleHealthSleepSummary(date);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const getPhysicalSummary = async ({
|
|
46
|
+
date,
|
|
47
|
+
source,
|
|
48
|
+
}: Params): Promise<PhysicalSummary> => {
|
|
49
|
+
isModuleReady(ready);
|
|
50
|
+
isValidDate(date);
|
|
51
|
+
|
|
52
|
+
if (Platform.OS === 'android' && source === SDKDataSource.SAMSUNG_HEALTH) {
|
|
53
|
+
const result = await RookSdk.getSamsungPhysicalSummary(date);
|
|
54
|
+
return JSON.parse(result);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (Platform.OS === 'android' && source === SDKDataSource.HEALTH_CONNECT) {
|
|
58
|
+
const result = await RookSdk.getHealthConnectPhysicalSummary(date);
|
|
59
|
+
return JSON.parse(result);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return RookSdk.getAppleHealthPhysicalSummary(date);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const getBodySummary = async ({
|
|
66
|
+
date,
|
|
67
|
+
source,
|
|
68
|
+
}: Params): Promise<BodySummary> => {
|
|
69
|
+
isModuleReady(ready);
|
|
70
|
+
isValidDate(date);
|
|
71
|
+
|
|
72
|
+
if (Platform.OS === 'android' && source === SDKDataSource.SAMSUNG_HEALTH) {
|
|
73
|
+
const result = await RookSdk.getSamsungBodySummary(date);
|
|
74
|
+
return JSON.parse(result);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (Platform.OS === 'android' && source === SDKDataSource.HEALTH_CONNECT) {
|
|
78
|
+
const result = await RookSdk.getHealthConnectBodySummary(date);
|
|
79
|
+
return JSON.parse(result);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return RookSdk.getAppleHealthBodySummary(date);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const getActivityEvents = async ({
|
|
86
|
+
date,
|
|
87
|
+
source,
|
|
88
|
+
}: Params): Promise<ActivityEvent[]> => {
|
|
89
|
+
isModuleReady(ready);
|
|
90
|
+
isValidDate(date);
|
|
91
|
+
|
|
92
|
+
if (Platform.OS === 'android' && source === SDKDataSource.SAMSUNG_HEALTH) {
|
|
93
|
+
const result = await RookSdk.getSamsungActivityEvents(date);
|
|
94
|
+
return JSON.parse(result);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (Platform.OS === 'android' && source === SDKDataSource.HEALTH_CONNECT) {
|
|
98
|
+
const result = await RookSdk.getHealthConnectActivityEvents(date);
|
|
99
|
+
return JSON.parse(result);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return RookSdk.getAppleHealthActivityEvents(date);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const writeNutritionData = async (
|
|
106
|
+
data: WriteNutrition,
|
|
107
|
+
source: SDKDataSource
|
|
108
|
+
): Promise<boolean> => {
|
|
109
|
+
isModuleReady(ready);
|
|
110
|
+
isValidZonedDateTime(data.date);
|
|
111
|
+
|
|
112
|
+
if (Platform.OS === 'android' && source === SDKDataSource.HEALTH_CONNECT) {
|
|
113
|
+
const event = parseWriteNutritionToHC(data);
|
|
114
|
+
return RookSdk.writeHealthConnectMealData(event);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (Platform.OS !== 'android' && source === SDKDataSource.APPLE_HEALTH) {
|
|
118
|
+
const event = parseWriteNutritionToAH(data);
|
|
119
|
+
return RookSdk.writeAppleHealthMealData(event);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
throw new Error('Please check the data source');
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
ready,
|
|
127
|
+
getSleepSummary,
|
|
128
|
+
getPhysicalSummary,
|
|
129
|
+
getBodySummary,
|
|
130
|
+
getActivityEvents,
|
|
131
|
+
writeNutritionData,
|
|
132
|
+
};
|
|
133
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import RookSdk from '../NativeRookSdk';
|
|
2
|
+
import { useRookSyncGateContext } from '../context/RookSyncGateContext';
|
|
3
|
+
import { isModuleReady } from '../utils/isModuleReady';
|
|
4
|
+
import isRunningOnAndroid from '../utils/isRunningOnAndroid';
|
|
5
|
+
|
|
6
|
+
export const useRookHealthConnect = () => {
|
|
7
|
+
const {
|
|
8
|
+
state: { ready, enableLogs },
|
|
9
|
+
} = useRookSyncGateContext();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Schedules the background synchronization of health data using Rook.
|
|
13
|
+
*
|
|
14
|
+
* This function enables automatic background data synchronization with Rook.
|
|
15
|
+
* It ensures that health data is periodically fetched and sent to Rook’s API
|
|
16
|
+
* based on the defined configuration.
|
|
17
|
+
*
|
|
18
|
+
* If the scheduling is successful, the promise resolves with true.
|
|
19
|
+
* If an error occurs, the promise rejects with an error message.
|
|
20
|
+
*
|
|
21
|
+
* @param promise A React Native promise that resolves when the sync is scheduled successfully or rejects with an error.
|
|
22
|
+
*/
|
|
23
|
+
const scheduleBackgroundSync = (): Promise<boolean> => {
|
|
24
|
+
isRunningOnAndroid();
|
|
25
|
+
isModuleReady(ready);
|
|
26
|
+
return RookSdk.scheduleBackgroundSync(enableLogs);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Cancels the scheduled background synchronization of health data.
|
|
31
|
+
*
|
|
32
|
+
* This function stops the automatic background sync previously scheduled using Rook.
|
|
33
|
+
* It ensures that no further health data is fetched and sent until scheduled again.
|
|
34
|
+
*
|
|
35
|
+
* If the cancellation is successful, the promise resolves with true.
|
|
36
|
+
* If an error occurs, the promise rejects with an error message.
|
|
37
|
+
*
|
|
38
|
+
* @param promise A React Native promise that resolves when the sync is canceled successfully or rejects with an error.
|
|
39
|
+
*/
|
|
40
|
+
const cancelBackgroundSync = (): Promise<boolean> => {
|
|
41
|
+
isRunningOnAndroid();
|
|
42
|
+
isModuleReady(ready);
|
|
43
|
+
return RookSdk.cancelBackgroundSync();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const isBackgroundSyncEnabled = (): Promise<boolean> => {
|
|
47
|
+
isRunningOnAndroid();
|
|
48
|
+
isModuleReady(ready);
|
|
49
|
+
|
|
50
|
+
return RookSdk.isBackgroundSyncEnabled();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
ready,
|
|
55
|
+
scheduleBackgroundSync,
|
|
56
|
+
cancelBackgroundSync,
|
|
57
|
+
isBackgroundSyncEnabled,
|
|
58
|
+
};
|
|
59
|
+
};
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import RookSdk from '../NativeRookSdk';
|
|
2
|
+
import { useRookSyncGateContext } from '../context/RookSyncGateContext';
|
|
3
|
+
import { isModuleReady } from '../utils/isModuleReady';
|
|
4
|
+
import isRunningOnAndroid from '../utils/isRunningOnAndroid';
|
|
5
|
+
import { isRunningOniOS } from '../utils/isRunningOniOS';
|
|
6
|
+
import { SamsungHealthPermission } from '../types/SamsungHealthPermissions';
|
|
7
|
+
import { AppleHealthPermission } from '../types/AppleHealthPermissions';
|
|
8
|
+
import type {
|
|
9
|
+
BackgroundStatus,
|
|
10
|
+
PermissionStatus,
|
|
11
|
+
} from '../types/PermissionsType';
|
|
12
|
+
|
|
13
|
+
export type RequestStatusPermissions = 'REQUEST_SENT' | 'ALREADY_GRANTED';
|
|
14
|
+
type CheckAvailabilityResponse =
|
|
15
|
+
| 'INSTALLED'
|
|
16
|
+
| 'NOT_INSTALLED'
|
|
17
|
+
| 'DISABLED'
|
|
18
|
+
| 'NOT_READY'
|
|
19
|
+
| 'OUTDATED'
|
|
20
|
+
| 'NOT_SUPPORTED';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The `useRookPermissions` hook manages permissions for accessing Apple Health and Health Connect data,
|
|
24
|
+
* including requesting various types of permissions and checking their status.
|
|
25
|
+
* @returns The `useRookPermissions` hook is being returned, which provides various functions related
|
|
26
|
+
* to requesting and checking permissions for Apple Health. The hook returns an object containing the
|
|
27
|
+
* following properties and functions:
|
|
28
|
+
*/
|
|
29
|
+
export const useRookPermissions = () => {
|
|
30
|
+
const {
|
|
31
|
+
state: { ready },
|
|
32
|
+
dispatch,
|
|
33
|
+
} = useRookSyncGateContext();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The function `checkAvailability` checks the availability of the health services
|
|
37
|
+
* @returns The function `checkAvailability` returns a Promise that resolves to a
|
|
38
|
+
* `CheckAvailabilityResponse` | 'INSTALLED' | 'NOT_INSTALLED' | 'NOT_SUPPORTED';
|
|
39
|
+
*/
|
|
40
|
+
const checkHealthConnectAvailability =
|
|
41
|
+
(): Promise<CheckAvailabilityResponse> => {
|
|
42
|
+
isModuleReady(ready);
|
|
43
|
+
isRunningOnAndroid();
|
|
44
|
+
|
|
45
|
+
return RookSdk.checkHealthConnectAvailability();
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const checkSamsungAvailability = (): Promise<CheckAvailabilityResponse> => {
|
|
49
|
+
isModuleReady(ready);
|
|
50
|
+
isRunningOnAndroid();
|
|
51
|
+
|
|
52
|
+
return RookSdk.checkSamsungAvailability();
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The function `healthConnectHasPermissions` checks if the app has permissions for health connect data
|
|
57
|
+
* access.
|
|
58
|
+
* @returns The function `healthConnectHasPermissions` is returning a boolean`.
|
|
59
|
+
*/
|
|
60
|
+
const healthConnectHasPermissions = (): Promise<boolean> => {
|
|
61
|
+
isRunningOnAndroid();
|
|
62
|
+
isModuleReady(ready);
|
|
63
|
+
|
|
64
|
+
return RookSdk.healthConnectHasPermissions();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The function `healthConnectHasPermissions` checks if the app has at least one permission for health connect data
|
|
69
|
+
* access.
|
|
70
|
+
* @returns The function `healthConnectHasPermissions` is returning a boolean`.
|
|
71
|
+
*/
|
|
72
|
+
const healthConnectHasPartialPermissions = (): Promise<boolean> => {
|
|
73
|
+
isRunningOnAndroid();
|
|
74
|
+
isModuleReady(ready);
|
|
75
|
+
|
|
76
|
+
return RookSdk.healthConnectHasPartialPermissions();
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The function `appleHealthHasPermissions` checks if the app has permissions for apple health data
|
|
81
|
+
* access.
|
|
82
|
+
* @returns The function `healthConnectHasPermissions` is returning a boolean`.
|
|
83
|
+
*/
|
|
84
|
+
const appleHealthHasPermissions = (
|
|
85
|
+
param: AppleHealthPermission
|
|
86
|
+
): Promise<PermissionStatus> => {
|
|
87
|
+
isRunningOniOS();
|
|
88
|
+
isModuleReady(ready);
|
|
89
|
+
|
|
90
|
+
return RookSdk.appleHealthHasPermissions(param);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const samsungHealthHasPermissions = (): Promise<boolean> => {
|
|
94
|
+
isRunningOnAndroid();
|
|
95
|
+
isModuleReady(ready);
|
|
96
|
+
|
|
97
|
+
return RookSdk.samsungHealthHasPermissions();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const samsungHealthHasPartialPermissions = (): Promise<boolean> => {
|
|
101
|
+
isRunningOnAndroid();
|
|
102
|
+
isModuleReady(ready);
|
|
103
|
+
|
|
104
|
+
return RookSdk.samsungHealthHasPartialPermissions();
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The function `requestAllHealthConnectPermissions` requests all permissions for health connect
|
|
109
|
+
*/
|
|
110
|
+
const requestHealthConnectPermissions =
|
|
111
|
+
async (): Promise<RequestStatusPermissions> => {
|
|
112
|
+
isRunningOnAndroid();
|
|
113
|
+
isModuleReady(ready);
|
|
114
|
+
|
|
115
|
+
return RookSdk.requestHealthConnectPermissions();
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const requestAppleHealthPermissions = async (
|
|
119
|
+
permissions?: AppleHealthPermission[]
|
|
120
|
+
): Promise<boolean> => {
|
|
121
|
+
isModuleReady(ready);
|
|
122
|
+
isRunningOniOS();
|
|
123
|
+
|
|
124
|
+
const bridge = permissions || Object.values(AppleHealthPermission);
|
|
125
|
+
const result = await RookSdk.requestAppleHealthPermissions(bridge);
|
|
126
|
+
|
|
127
|
+
dispatch({ type: 'SET_PERMISSIONS' });
|
|
128
|
+
|
|
129
|
+
return result;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const requestSamsungHealthPermissions = async (
|
|
133
|
+
permissions?: SamsungHealthPermission[]
|
|
134
|
+
): Promise<RequestStatusPermissions> => {
|
|
135
|
+
isRunningOnAndroid();
|
|
136
|
+
isModuleReady(ready);
|
|
137
|
+
|
|
138
|
+
const bridge = permissions || Object.values(SamsungHealthPermission);
|
|
139
|
+
const result = await RookSdk.requestSamsungHealthPermissions(bridge);
|
|
140
|
+
|
|
141
|
+
return result;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The function `openAppleHealthSettings` opens the apple health settings
|
|
146
|
+
*/
|
|
147
|
+
const openAppleHealthSettings = async (): Promise<void> => {
|
|
148
|
+
isRunningOniOS();
|
|
149
|
+
isModuleReady(ready);
|
|
150
|
+
|
|
151
|
+
return RookSdk.openAppleHealthSettings();
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The function `openHealthConnectSettings` opens the health connect settings
|
|
156
|
+
*/
|
|
157
|
+
const openHealthConnectSettings = async (): Promise<void> => {
|
|
158
|
+
isRunningOnAndroid();
|
|
159
|
+
isModuleReady(ready);
|
|
160
|
+
|
|
161
|
+
return RookSdk.openHealthConnectSettings();
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The function `requestAndroidBackgroundPermissions` requests background permissions for an Android
|
|
166
|
+
* in other to extract data from Health Connect
|
|
167
|
+
* @returns The `requestBackgroundPermissions` function is being returned as a Promise<void>.
|
|
168
|
+
*/
|
|
169
|
+
const requestAndroidBackgroundPermissions =
|
|
170
|
+
(): Promise<RequestStatusPermissions> => {
|
|
171
|
+
isRunningOnAndroid();
|
|
172
|
+
isModuleReady(ready);
|
|
173
|
+
|
|
174
|
+
return RookSdk.requestAndroidBackgroundPermissions();
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The function `hasAndroidBackgroundPermissions` checks if the app has background permissions on an
|
|
179
|
+
* Android device.
|
|
180
|
+
* @returns The function `hasBackgroundPermissions()` is returning a boolean indicating the status of
|
|
181
|
+
* the permissions.
|
|
182
|
+
*/
|
|
183
|
+
const androidHasBackgroundPermissions = (): Promise<boolean> => {
|
|
184
|
+
isRunningOnAndroid();
|
|
185
|
+
isModuleReady(ready);
|
|
186
|
+
|
|
187
|
+
return RookSdk.androidHasBackgroundPermissions();
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const checkHealthConnectBackgroundReadStatus =
|
|
191
|
+
(): Promise<BackgroundStatus> => {
|
|
192
|
+
isRunningOnAndroid();
|
|
193
|
+
isModuleReady(ready);
|
|
194
|
+
|
|
195
|
+
return RookSdk.checkHealthConnectBackgroundReadStatus();
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const revokeHealthConnectPermissions = (): Promise<boolean> => {
|
|
199
|
+
isRunningOnAndroid();
|
|
200
|
+
isModuleReady(ready);
|
|
201
|
+
|
|
202
|
+
return RookSdk.revokeHealthConnectPermissions();
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const requestAppleWriteNutritionPermission = async (): Promise<void> => {
|
|
206
|
+
isRunningOniOS();
|
|
207
|
+
isModuleReady(ready);
|
|
208
|
+
|
|
209
|
+
return RookSdk.requestAppleWriteNutritionPermission();
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const shouldRequestAndroidBackgroundPermissions = (): Promise<boolean> => {
|
|
213
|
+
isRunningOnAndroid();
|
|
214
|
+
isModuleReady(ready);
|
|
215
|
+
|
|
216
|
+
return RookSdk.shouldRequestAndroidBackgroundPermissions();
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
ready,
|
|
221
|
+
androidHasBackgroundPermissions,
|
|
222
|
+
appleHealthHasPermissions,
|
|
223
|
+
checkHealthConnectAvailability,
|
|
224
|
+
checkHealthConnectBackgroundReadStatus,
|
|
225
|
+
checkSamsungAvailability,
|
|
226
|
+
healthConnectHasPartialPermissions,
|
|
227
|
+
healthConnectHasPermissions,
|
|
228
|
+
openAppleHealthSettings,
|
|
229
|
+
openHealthConnectSettings,
|
|
230
|
+
requestAndroidBackgroundPermissions,
|
|
231
|
+
requestAppleHealthPermissions,
|
|
232
|
+
requestHealthConnectPermissions,
|
|
233
|
+
requestSamsungHealthPermissions,
|
|
234
|
+
requestAppleWriteNutritionPermission,
|
|
235
|
+
samsungHealthHasPartialPermissions,
|
|
236
|
+
samsungHealthHasPermissions,
|
|
237
|
+
revokeHealthConnectPermissions,
|
|
238
|
+
shouldRequestAndroidBackgroundPermissions,
|
|
239
|
+
};
|
|
240
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import RookSdk from '../NativeRookSdk';
|
|
2
|
+
import { useRookSyncGateContext } from '../context/RookSyncGateContext';
|
|
3
|
+
import { isModuleReady } from '../utils/isModuleReady';
|
|
4
|
+
import isRunningOnAndroid from '../utils/isRunningOnAndroid';
|
|
5
|
+
|
|
6
|
+
export const useRookSamsungHealth = () => {
|
|
7
|
+
const {
|
|
8
|
+
state: { ready },
|
|
9
|
+
} = useRookSyncGateContext();
|
|
10
|
+
|
|
11
|
+
const enableSamsungSync = async (): Promise<boolean> => {
|
|
12
|
+
isRunningOnAndroid();
|
|
13
|
+
isModuleReady(ready);
|
|
14
|
+
|
|
15
|
+
return RookSdk.enableSamsungSync();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const isSamsungSyncEnabled = async (): Promise<boolean> => {
|
|
19
|
+
isRunningOnAndroid();
|
|
20
|
+
isModuleReady(ready);
|
|
21
|
+
|
|
22
|
+
return RookSdk.isSamsungSyncEnabled();
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const disableSamsungSync = async (): Promise<boolean> => {
|
|
26
|
+
isRunningOnAndroid();
|
|
27
|
+
isModuleReady(ready);
|
|
28
|
+
|
|
29
|
+
return RookSdk.disableSamsungSync();
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
ready,
|
|
33
|
+
enableSamsungSync,
|
|
34
|
+
disableSamsungSync,
|
|
35
|
+
isSamsungSyncEnabled,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import RookSdk from '../NativeRookSdk';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
3
|
+
|
|
4
|
+
import { isModuleReady } from '../utils/isModuleReady';
|
|
5
|
+
import { isValidDate } from '../utils/isValidDate';
|
|
6
|
+
import type {
|
|
7
|
+
Callback,
|
|
8
|
+
SyncEventParams,
|
|
9
|
+
SyncParams,
|
|
10
|
+
RookError,
|
|
11
|
+
} from '../types/SyncTypes';
|
|
12
|
+
import { SDKDataSource } from '../types/SDKSources';
|
|
13
|
+
import { useRookSyncGateContext } from '../context/RookSyncGateContext';
|
|
14
|
+
|
|
15
|
+
export const useRookSync = () => {
|
|
16
|
+
const {
|
|
17
|
+
state: { ready, enableLogs },
|
|
18
|
+
} = useRookSyncGateContext();
|
|
19
|
+
|
|
20
|
+
const createResultPayload = (result: any) => {
|
|
21
|
+
if (!result) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
return { status: result.result, error: result.error };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const convertToPromise = (params: any[], toPromise: Function) => {
|
|
28
|
+
return new Promise((resolve) => {
|
|
29
|
+
toPromise(...params, (error: Error, result: boolean) => {
|
|
30
|
+
resolve({ error, result });
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const syncWithOutParams = async (callback: Callback) => {
|
|
36
|
+
if (Platform.OS === 'android') {
|
|
37
|
+
const hcResult: any = await convertToPromise([], RookSdk.sync);
|
|
38
|
+
|
|
39
|
+
RookSdk.shSync(enableLogs, (err: RookError, result: boolean) => {
|
|
40
|
+
callback({
|
|
41
|
+
healthConnect: {
|
|
42
|
+
status: hcResult.result,
|
|
43
|
+
error: hcResult.error,
|
|
44
|
+
},
|
|
45
|
+
samsungHealth: {
|
|
46
|
+
status: result,
|
|
47
|
+
error: err,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
RookSdk.sync((error: RookError, result: boolean) => {
|
|
56
|
+
callback({
|
|
57
|
+
appleHealth: {
|
|
58
|
+
status: result,
|
|
59
|
+
error,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const syncWithParams = async (params: SyncParams, callback: Callback) => {
|
|
66
|
+
isValidDate(params.date);
|
|
67
|
+
|
|
68
|
+
const promisesToRun = [];
|
|
69
|
+
const { sources, summary, date } = params;
|
|
70
|
+
const isAndroid = Platform.OS === 'android';
|
|
71
|
+
|
|
72
|
+
const args = [date];
|
|
73
|
+
let bridgeMethod: Function = RookSdk.syncByDate;
|
|
74
|
+
let shBridgeMethod: Function = RookSdk.shSyncByDate;
|
|
75
|
+
|
|
76
|
+
if (summary) {
|
|
77
|
+
args.unshift(summary);
|
|
78
|
+
bridgeMethod = RookSdk.syncByDefinition;
|
|
79
|
+
shBridgeMethod = RookSdk.shSyncByDefinition;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const shouldSyncSO =
|
|
83
|
+
sources.includes(SDKDataSource.APPLE_HEALTH) ||
|
|
84
|
+
sources.includes(SDKDataSource.HEALTH_CONNECT);
|
|
85
|
+
|
|
86
|
+
const shouldSyncSH =
|
|
87
|
+
sources.includes(SDKDataSource.SAMSUNG_HEALTH) && isAndroid;
|
|
88
|
+
|
|
89
|
+
if (shouldSyncSO) {
|
|
90
|
+
promisesToRun.push(convertToPromise(args, bridgeMethod));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (shouldSyncSH) {
|
|
94
|
+
promisesToRun.push(convertToPromise(args, shBridgeMethod));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const results = await Promise.all(promisesToRun);
|
|
98
|
+
const SOResult = shouldSyncSO ? results.shift() : null;
|
|
99
|
+
const shResult = shouldSyncSH ? results.shift() : null;
|
|
100
|
+
|
|
101
|
+
callback({
|
|
102
|
+
appleHealth: !isAndroid ? createResultPayload(SOResult) : undefined,
|
|
103
|
+
healthConnect: isAndroid ? createResultPayload(SOResult) : undefined,
|
|
104
|
+
samsungHealth: createResultPayload(shResult),
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const sync = (callback: Callback, params?: SyncParams): void => {
|
|
109
|
+
isModuleReady(ready);
|
|
110
|
+
|
|
111
|
+
if (!params) {
|
|
112
|
+
syncWithOutParams(callback);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (params && params.summary && !params.date) {
|
|
117
|
+
throw new Error('Date is missing');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
syncWithParams(params, callback);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const syncEvents = async (params: SyncEventParams): Promise<boolean> => {
|
|
124
|
+
isModuleReady(ready);
|
|
125
|
+
|
|
126
|
+
const { date, event, sources } = params;
|
|
127
|
+
const isAndroid = Platform.OS === 'android';
|
|
128
|
+
|
|
129
|
+
const promisesToRun = [];
|
|
130
|
+
const parsedEvent = isAndroid ? event : event.toLowerCase();
|
|
131
|
+
|
|
132
|
+
const shouldSyncSO =
|
|
133
|
+
sources.includes(SDKDataSource.APPLE_HEALTH) ||
|
|
134
|
+
sources.includes(SDKDataSource.HEALTH_CONNECT);
|
|
135
|
+
|
|
136
|
+
const shouldSyncSH =
|
|
137
|
+
sources.includes(SDKDataSource.SAMSUNG_HEALTH) && isAndroid;
|
|
138
|
+
|
|
139
|
+
if (shouldSyncSO) {
|
|
140
|
+
if (['hydration', 'nutrition'].includes(params.event) && !isAndroid)
|
|
141
|
+
throw new Error(`${params.event} is not available on iOS`);
|
|
142
|
+
|
|
143
|
+
promisesToRun.push(RookSdk.syncEvent(date, parsedEvent));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (shouldSyncSH) promisesToRun.push(RookSdk.shSyncEvent(date, event));
|
|
147
|
+
|
|
148
|
+
await Promise.all(promisesToRun);
|
|
149
|
+
|
|
150
|
+
return true;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
sync,
|
|
155
|
+
syncEvents,
|
|
156
|
+
};
|
|
157
|
+
};
|