@shenai/react-native-sdk 3.1.1

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.
@@ -0,0 +1,37 @@
1
+ #import <React/RCTViewManager.h>
2
+ #import <ShenaiSDK/ShenaiView.h>
3
+
4
+ @interface ShenaiSdkViewManager : RCTViewManager
5
+ @end
6
+
7
+ @interface ShenaiContainerView : UIView
8
+ @property(strong, nonatomic) ShenaiView *shenaiViewController;
9
+ @end
10
+
11
+ @implementation ShenaiContainerView
12
+
13
+ - (instancetype)init {
14
+ self = [super init];
15
+ if (self) {
16
+ _shenaiViewController = [[ShenaiView alloc] init];
17
+ [self addSubview:_shenaiViewController.view];
18
+ }
19
+ return self;
20
+ }
21
+
22
+ - (void)layoutSubviews {
23
+ [super layoutSubviews];
24
+ self.shenaiViewController.view.frame = self.bounds;
25
+ }
26
+
27
+ @end
28
+
29
+ @implementation ShenaiSdkViewManager
30
+
31
+ RCT_EXPORT_MODULE(ShenaiSdkView)
32
+
33
+ - (UIView *)view {
34
+ return [[ShenaiContainerView alloc] init];
35
+ }
36
+
37
+ @end
@@ -0,0 +1,83 @@
1
+ import { useState, useEffect, useCallback } from "react";
2
+ import { isInitialized, getHeartRate4s, getHeartRate10s, getRealtimeMetrics, getMeasurementResults, getMeasurementProgressPercentage, getRealtimeHeartbeats } from "./index";
3
+
4
+ /**
5
+ * Type utility to unwrap the type from a Promise.
6
+ * @template T - The promise type to be unwrapped.
7
+ */
8
+
9
+ /**
10
+ * Custom hook for polling data from the Shen.AI SDK.
11
+ *
12
+ * @template T - The type of the fetch function.
13
+ * @param {T} fetchFunction - The function to fetch data from the SDK.
14
+ * @param {number} intervalDuration - The duration between each data fetch in milliseconds.
15
+ * @returns {UnwrappedPromise<ReturnType<T>> | null} - The latest fetched data or null if uninitialized or on error.
16
+ */
17
+ const useSDKPolling = (fetchFunction, intervalDuration) => {
18
+ const [data, setData] = useState(null);
19
+ useEffect(() => {
20
+ const intervalId = setInterval(async () => {
21
+ try {
22
+ const isInit = await isInitialized();
23
+ if (!isInit) {
24
+ setData(null);
25
+ return;
26
+ }
27
+ const fetchedData = await fetchFunction();
28
+ setData(fetchedData ?? null);
29
+ } catch (error) {
30
+ console.error("Error fetching data:", error);
31
+ setData(null);
32
+ }
33
+ }, intervalDuration);
34
+ return () => clearInterval(intervalId);
35
+ }, [fetchFunction, intervalDuration]);
36
+ return data;
37
+ };
38
+
39
+ /**
40
+ * Hook to continuously fetch the heart rate computed from the last 10 seconds of video.
41
+ *
42
+ * @returns The latest heart rate in BPM or null if uninitialized/error/bad signal.
43
+ */
44
+ export const useRealtimeHeartRate = () => useSDKPolling(getHeartRate10s, 1000);
45
+
46
+ /**
47
+ * Hook to continuously fetch the heart rate computed from the last 4 seconds of video.
48
+ *
49
+ * @returns The latest heart rate in BPM or null if uninitialized/error/bad signal.
50
+ */
51
+ export const useRealtimeHeartRate4s = () => useSDKPolling(getHeartRate4s, 1000);
52
+
53
+ /**
54
+ * Hook to get realtime metrics of the measurement computed from the last `period_sec` seconds of video.
55
+ *
56
+ * @param period_sec Duration in seconds of the period of interest.
57
+ * @returns The latest realtime metrics or null if uninitialized/error/bad signal.
58
+ */
59
+ export const useRealtimeMetrics = period_sec => {
60
+ const callback = useCallback(() => getRealtimeMetrics(period_sec), [period_sec]);
61
+ return useSDKPolling(callback, 1000);
62
+ };
63
+
64
+ /**
65
+ * Hook to get results of the measurement.
66
+ *
67
+ * @returns The latest measurement results or null if the measurement hasn't finished yet.
68
+ */
69
+ export const useMeasurementResults = () => useSDKPolling(getMeasurementResults, 1000);
70
+
71
+ /**
72
+ * Hook to get the measurement progress percentage.
73
+ *
74
+ * @returns The current measurement progress percentage.
75
+ */
76
+ export const useMeasurementProgress = (update_interval_ms = 1000) => useSDKPolling(getMeasurementProgressPercentage, update_interval_ms);
77
+
78
+ /**
79
+ * Hook to get the realtime heartbeats.
80
+ *
81
+ * @returns The latest heartbeats or null if uninitialized/error/bad signal.
82
+ */
83
+ export const useRealtimeHeartbeats = () => useSDKPolling(getRealtimeHeartbeats, 1000);