react-native-visit-rn-sdk 6.0.4 → 7.0.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.
Files changed (93) hide show
  1. package/README.md +286 -195
  2. package/android/build.gradle +4 -1
  3. package/android/gradle.properties +4 -4
  4. package/android/src/main/AndroidManifest.xml +11 -5
  5. package/android/src/main/AndroidManifestNew.xml +11 -5
  6. package/android/src/main/java/com/visitrnsdk/VisitFitnessModule.kt +642 -0
  7. package/android/src/main/java/com/visitrnsdk/VisitRnSdkPackage.kt +1 -1
  8. package/android/src/main/java/com/visitrnsdk/VisitSessionStorage.kt +50 -0
  9. package/ios/VisitRnSdkViewManager.h +23 -1
  10. package/ios/VisitRnSdkViewManager.m +1259 -8
  11. package/lib/commonjs/Services.js +35 -0
  12. package/lib/commonjs/Services.js.map +1 -0
  13. package/lib/commonjs/constants.js +11 -0
  14. package/lib/commonjs/constants.js.map +1 -0
  15. package/lib/commonjs/index.android.js +451 -206
  16. package/lib/commonjs/index.android.js.map +1 -1
  17. package/lib/commonjs/index.ios.js +266 -114
  18. package/lib/commonjs/index.ios.js.map +1 -1
  19. package/lib/module/Services.js +27 -0
  20. package/lib/module/Services.js.map +1 -0
  21. package/lib/module/constants.js +5 -0
  22. package/lib/module/constants.js.map +1 -0
  23. package/lib/module/index.android.js +451 -207
  24. package/lib/module/index.android.js.map +1 -1
  25. package/lib/module/index.ios.js +268 -116
  26. package/lib/module/index.ios.js.map +1 -1
  27. package/lib/typescript/Services.d.ts +3 -0
  28. package/lib/typescript/Services.d.ts.map +1 -0
  29. package/lib/typescript/constants.d.ts +6 -0
  30. package/lib/typescript/constants.d.ts.map +1 -0
  31. package/lib/typescript/index.android.d.ts +26 -3
  32. package/lib/typescript/index.android.d.ts.map +1 -1
  33. package/lib/typescript/index.ios.d.ts +20 -3
  34. package/lib/typescript/index.ios.d.ts.map +1 -1
  35. package/package.json +16 -26
  36. package/react-native-visit-rn-sdk.podspec +2 -1
  37. package/src/Services.js +37 -0
  38. package/src/constants.js +4 -0
  39. package/src/index.android.js +773 -0
  40. package/src/index.ios.js +429 -0
  41. package/android/src/main/java/com/visitrnsdk/VisitRnSdkLocationModule.kt +0 -37
  42. package/lib/commonjs/assets/video-call/connecting-lottie.gif +0 -0
  43. package/lib/commonjs/assets/video-call/end-call.png +0 -0
  44. package/lib/commonjs/assets/video-call/mic-off.png +0 -0
  45. package/lib/commonjs/assets/video-call/reconnecting.gif +0 -0
  46. package/lib/commonjs/assets/video-call/switch-camera.png +0 -0
  47. package/lib/commonjs/assets/video-call/toggle-down.png +0 -0
  48. package/lib/commonjs/assets/video-call/toggle-up.png +0 -0
  49. package/lib/commonjs/assets/video-call/video-off.png +0 -0
  50. package/lib/commonjs/components/VideoCallComponent.js +0 -1088
  51. package/lib/commonjs/components/VideoCallComponent.js.map +0 -1
  52. package/lib/commonjs/index.js +0 -6
  53. package/lib/commonjs/index.js.map +0 -1
  54. package/lib/commonjs/shims.d.js +0 -2
  55. package/lib/commonjs/shims.d.js.map +0 -1
  56. package/lib/commonjs/types.js +0 -2
  57. package/lib/commonjs/types.js.map +0 -1
  58. package/lib/module/assets/video-call/connecting-lottie.gif +0 -0
  59. package/lib/module/assets/video-call/end-call.png +0 -0
  60. package/lib/module/assets/video-call/mic-off.png +0 -0
  61. package/lib/module/assets/video-call/reconnecting.gif +0 -0
  62. package/lib/module/assets/video-call/switch-camera.png +0 -0
  63. package/lib/module/assets/video-call/toggle-down.png +0 -0
  64. package/lib/module/assets/video-call/toggle-up.png +0 -0
  65. package/lib/module/assets/video-call/video-off.png +0 -0
  66. package/lib/module/components/VideoCallComponent.js +0 -1081
  67. package/lib/module/components/VideoCallComponent.js.map +0 -1
  68. package/lib/module/index.js +0 -2
  69. package/lib/module/index.js.map +0 -1
  70. package/lib/module/shims.d.js +0 -2
  71. package/lib/module/shims.d.js.map +0 -1
  72. package/lib/module/types.js +0 -2
  73. package/lib/module/types.js.map +0 -1
  74. package/lib/typescript/components/VideoCallComponent.d.ts +0 -79
  75. package/lib/typescript/components/VideoCallComponent.d.ts.map +0 -1
  76. package/lib/typescript/index.d.ts +0 -17
  77. package/lib/typescript/index.d.ts.map +0 -1
  78. package/lib/typescript/types.d.ts +0 -24
  79. package/lib/typescript/types.d.ts.map +0 -1
  80. package/src/assets/video-call/connecting-lottie.gif +0 -0
  81. package/src/assets/video-call/end-call.png +0 -0
  82. package/src/assets/video-call/mic-off.png +0 -0
  83. package/src/assets/video-call/reconnecting.gif +0 -0
  84. package/src/assets/video-call/switch-camera.png +0 -0
  85. package/src/assets/video-call/toggle-down.png +0 -0
  86. package/src/assets/video-call/toggle-up.png +0 -0
  87. package/src/assets/video-call/video-off.png +0 -0
  88. package/src/components/VideoCallComponent.tsx +0 -1484
  89. package/src/index.android.tsx +0 -459
  90. package/src/index.ios.tsx +0 -238
  91. package/src/index.tsx +0 -25
  92. package/src/shims.d.ts +0 -49
  93. package/src/types.ts +0 -25
@@ -0,0 +1,773 @@
1
+ import React, { useRef, useEffect, useState, useCallback } from 'react';
2
+ import { EventRegister } from 'react-native-event-listeners';
3
+
4
+ import {
5
+ SafeAreaView,
6
+ NativeModules,
7
+ PermissionsAndroid,
8
+ BackHandler,
9
+ Linking,
10
+ Alert,
11
+ AppState,
12
+ NativeEventEmitter,
13
+ } from 'react-native';
14
+
15
+ import WebView from 'react-native-webview';
16
+
17
+ import LocationEnabler from 'react-native-location-enabler';
18
+
19
+ import DeviceInfo from 'react-native-device-info';
20
+
21
+ import axios from 'axios';
22
+
23
+ import constants from './constants';
24
+
25
+ export const httpClient = axios.create({
26
+ timeout: 60000,
27
+ });
28
+
29
+ const messageEmitter = new NativeEventEmitter(NativeModules.VisitFitnessModule);
30
+
31
+ const {
32
+ PRIORITIES: { HIGH_ACCURACY },
33
+ useLocationSettings,
34
+ addListener,
35
+ } = LocationEnabler;
36
+
37
+ const VisitRnSdkView = ({
38
+ cpsid,
39
+ baseUrl,
40
+ errorBaseUrl,
41
+ token,
42
+ moduleName,
43
+ environment,
44
+ magicLink,
45
+ isLoggingEnabled,
46
+ }) => {
47
+ const [source, setSource] = useState('');
48
+ const [appState, setAppState] = useState(AppState.currentState);
49
+
50
+ const [
51
+ showPermissionAlreadyDeniedDialog,
52
+ setShowPermissionAlreadyDeniedDialog,
53
+ ] = useState(false);
54
+
55
+ useEffect(() => {
56
+ if (isLoggingEnabled) {
57
+ console.log('useEffect ran');
58
+ }
59
+
60
+ NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
61
+
62
+ if ((magicLink?.trim()?.length || 0) > 0) {
63
+ setSource(magicLink);
64
+ } else {
65
+ DeviceInfo.getAndroidId()
66
+ .then((deviceId) => {
67
+ var buildNumber = DeviceInfo.getBuildNumber();
68
+ let systemVersion = DeviceInfo.getSystemVersion();
69
+ let version = DeviceInfo.getVersion();
70
+
71
+ if (isLoggingEnabled) {
72
+ console.log(
73
+ ' baseUrl : ' +
74
+ baseUrl +
75
+ 'token: ' +
76
+ token +
77
+ ' cpsid: ' +
78
+ cpsid +
79
+ ' environment: ' +
80
+ environment +
81
+ 'buildNumber:' +
82
+ buildNumber +
83
+ ' systemVersion:' +
84
+ systemVersion +
85
+ ' version : ' +
86
+ version +
87
+ ' deviceId',
88
+ deviceId
89
+ );
90
+ }
91
+
92
+ var finalEndPoint = `${baseUrl}/partners/v3/generate-magic-link-star-health`;
93
+
94
+ if (isLoggingEnabled) {
95
+ console.log('finalEndPoint: ' + finalEndPoint);
96
+ }
97
+
98
+ httpClient
99
+ .post(finalEndPoint, {
100
+ cpsid: cpsid,
101
+ token: token,
102
+ srcClientId: 'Android',
103
+ deviceId: deviceId,
104
+ appVersion: version,
105
+ deviceVersion: systemVersion,
106
+ userEnv: environment,
107
+ })
108
+ .then((response) => {
109
+ let data = response.data;
110
+ // let visitMagicLink = data.result; //@Deprecated. Superseded by magic code usage.
111
+ const errorMessage = data.errorMessage;
112
+ const magicCode = data.magicCode;
113
+ const responseReferenceId = data.responseReferenceId;
114
+ const otherValues = data.otherValues;
115
+
116
+ let finalBaseUrl = '';
117
+
118
+ if (environment.toUpperCase() === 'PROD') {
119
+ finalBaseUrl = constants.PROD_BASE_URL;
120
+ } else {
121
+ finalBaseUrl = constants.STAGE_BASE_URL;
122
+ }
123
+
124
+ let finalUrl = `${finalBaseUrl}=${magicCode}`;
125
+
126
+ if (data.message === 'success') {
127
+ if ((moduleName?.trim()?.length || 0) > 0) {
128
+ finalUrl += `&tab=${moduleName}`;
129
+ }
130
+
131
+ if (
132
+ typeof responseReferenceId === 'string' &&
133
+ responseReferenceId.trim().length > 0
134
+ ) {
135
+ finalUrl += `&responseReferenceId=${responseReferenceId}`;
136
+ }
137
+
138
+ if (
139
+ typeof otherValues === 'string' &&
140
+ otherValues.trim().length > 0
141
+ ) {
142
+ finalUrl += `&otherValues=${otherValues}`;
143
+ }
144
+
145
+ if (isLoggingEnabled) {
146
+ console.log('magicLink: ' + finalUrl);
147
+ }
148
+
149
+ setSource(finalUrl);
150
+ } else {
151
+ var errorUrl = `${errorBaseUrl}/star-health?error=${errorMessage}`;
152
+ setSource(errorUrl);
153
+
154
+ if (errorMessage != null) {
155
+ if (errorMessage === 'Please login again') {
156
+ EventRegister.emitEvent('visit-event', {
157
+ message: 'unauthorized-wellness-access',
158
+ errorMessage: errorMessage,
159
+ });
160
+ } else if (errorMessage.includes('External Server Error')) {
161
+ EventRegister.emitEvent('visit-event', {
162
+ message: 'external-server-error',
163
+ errorMessage: errorMessage,
164
+ });
165
+ }
166
+ }
167
+
168
+ if (isLoggingEnabled) {
169
+ console.log(
170
+ 'erorMessage: ' +
171
+ data.errorMessage +
172
+ ' errorUrl: ' +
173
+ errorUrl
174
+ );
175
+ }
176
+ }
177
+ })
178
+ .catch((error) => {
179
+ var errorUrl = `${errorBaseUrl}/star-health?error=${error}`;
180
+ setSource(errorUrl);
181
+
182
+ EventRegister.emitEvent('visit-event', {
183
+ message: 'generate-magic-link-failed',
184
+ errorMessage: `${error}`,
185
+ });
186
+
187
+ if (isLoggingEnabled) {
188
+ console.log('error: ' + error);
189
+ }
190
+ });
191
+ })
192
+ .catch((err) => {
193
+ var errorUrl = `${errorBaseUrl}/star-health?error=${err}`;
194
+ setSource(errorUrl);
195
+
196
+ EventRegister.emitEvent('visit-event', {
197
+ message: 'getDeviceInfo-failed',
198
+ errorMessage: `${err}`,
199
+ });
200
+
201
+ if (isLoggingEnabled) {
202
+ console.log('getDeviceInfo err', err);
203
+ }
204
+ });
205
+ }
206
+ }, [
207
+ cpsid,
208
+ token,
209
+ baseUrl,
210
+ errorBaseUrl,
211
+ moduleName,
212
+ environment,
213
+ magicLink,
214
+ isLoggingEnabled,
215
+ ]);
216
+
217
+ useEffect(() => {
218
+ const subscription = messageEmitter.addListener('onMessage', (message) => {
219
+ console.log('Received message:', message);
220
+
221
+ if (typeof message === 'string' && message.includes('RemoteException')) {
222
+ Alert.alert(
223
+ 'Error!',
224
+ 'Something went wrong while connecting to Health Connect. Please try again.',
225
+ []
226
+ );
227
+ }
228
+
229
+ EventRegister.emitEvent('visit-event', {
230
+ message: 'health-connect-error-event',
231
+ errorMessage: message,
232
+ });
233
+ });
234
+
235
+ return () => {
236
+ // Clean up the subscription when the component unmounts
237
+ subscription.remove();
238
+ };
239
+ }, []);
240
+
241
+ useEffect(() => {
242
+ const subscription = AppState.addEventListener(
243
+ 'change',
244
+ handleAppStateChange
245
+ );
246
+ console.log(`AppState.addEventListener added, current state: ${appState}`);
247
+
248
+ return () => {
249
+ subscription.remove();
250
+ };
251
+ }, [appState]); // Include appState in the dependency array to ensure it's up to date.
252
+
253
+ const handleAppStateChange = (nextAppState) => {
254
+ // console.log(
255
+ // "nextAppState: " + nextAppState + ", previousState: " + appState
256
+ // );
257
+
258
+ // Instead of relying on the old appState, use the nextAppState directly
259
+ if (appState.match(/inactive|background/) && nextAppState === 'active') {
260
+ if (isLoggingEnabled) {
261
+ console.log('App has come to the foreground!');
262
+ }
263
+ console.log(
264
+ 'getHealthConnectStatus commented out, because it it not required for MCHI since we rely on callback from the web to get the health connect status.'
265
+ );
266
+ // getHealthConnectStatus();
267
+ }
268
+
269
+ setAppState(nextAppState); // Update the state with the new app state
270
+ };
271
+
272
+ const [enabled, requestResolution] = useLocationSettings(
273
+ {
274
+ priority: HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
275
+ alwaysShow: true, // default false
276
+ needBle: true, // default false
277
+ },
278
+ false /* optional: default undefined */
279
+ );
280
+
281
+ const webviewRef = useRef(null);
282
+
283
+ const showLocationPermissionAlert = () => {
284
+ Alert.alert(
285
+ 'Permission Required',
286
+ 'Allow location permission from app settings',
287
+ [
288
+ {
289
+ text: 'Cancel',
290
+ onPress: () => {
291
+ console.log('Cancel clicked');
292
+ },
293
+ },
294
+ {
295
+ text: 'Go to Settings',
296
+ onPress: () => {
297
+ Linking.openSettings();
298
+ },
299
+ },
300
+ ]
301
+ );
302
+ };
303
+
304
+ const requestLocationPermission = async () => {
305
+ try {
306
+ console.log('requestLocationPermission called');
307
+
308
+ const isLocationPermissionPresent = await PermissionsAndroid.check(
309
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
310
+ );
311
+
312
+ console.log(
313
+ 'isLocationPermissionPresent: ' +
314
+ isLocationPermissionPresent +
315
+ ' showPermissionAlreadyDeniedDialog: ' +
316
+ showPermissionAlreadyDeniedDialog
317
+ );
318
+
319
+ if (!isLocationPermissionPresent && showPermissionAlreadyDeniedDialog) {
320
+ console.log('showLocationPermissionAlert() called');
321
+
322
+ showLocationPermissionAlert();
323
+ } else {
324
+ console.log('requesting location permission');
325
+
326
+ const granted = await PermissionsAndroid.request(
327
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
328
+ {
329
+ title: 'Need Location Permission',
330
+ message: 'Need access to location permission',
331
+ buttonNeutral: 'Ask Me Later',
332
+ buttonNegative: 'Cancel',
333
+ buttonPositive: 'OK',
334
+ }
335
+ );
336
+ if (granted === PermissionsAndroid.RESULTS.GRANTED) {
337
+ if (isLoggingEnabled) {
338
+ console.log('Location permission granted');
339
+ }
340
+ setShowPermissionAlreadyDeniedDialog(false);
341
+
342
+ if (!enabled) {
343
+ requestResolution();
344
+ } else {
345
+ var finalString = `window.checkTheGpsPermission(true)`;
346
+ console.log('requestLocationPermission: ' + finalString);
347
+
348
+ webviewRef.current?.injectJavaScript(finalString);
349
+ }
350
+ } else {
351
+ setShowPermissionAlreadyDeniedDialog(true);
352
+ console.log('Location permission denied');
353
+
354
+ var finalString = `window.checkTheGpsPermission(false)`;
355
+ console.log('requestLocationPermission: ' + finalString);
356
+
357
+ webviewRef.current?.injectJavaScript(finalString);
358
+ }
359
+ }
360
+ } catch (e) {
361
+ console.error(e);
362
+ }
363
+ };
364
+
365
+ const showHealthConnectPermissionDeniedDialog = () => {
366
+ Alert.alert(
367
+ 'Permission Denied',
368
+ 'Go to Health Connect App to allow app permission',
369
+ [
370
+ {
371
+ text: 'Cancel',
372
+ onPress: () => {
373
+ console.log('Cancel clicked');
374
+ },
375
+ },
376
+ {
377
+ text: 'Open Health Connect',
378
+ onPress: () => {
379
+ openHealthConnectApp();
380
+ },
381
+ },
382
+ ]
383
+ );
384
+ };
385
+
386
+ const openHealthConnectApp = async () => {
387
+ NativeModules.VisitFitnessModule.openHealthConnectApp();
388
+ };
389
+
390
+ const askForHealthConnectPermission = async () => {
391
+ try {
392
+ const isPermissionGranted =
393
+ await NativeModules.VisitFitnessModule.askForFitnessPermission();
394
+
395
+ if (isLoggingEnabled) {
396
+ console.log('isPermissionGranted: ' + isPermissionGranted);
397
+ }
398
+
399
+ if (isPermissionGranted === 'GRANTED') {
400
+ getHealthConnectStatus();
401
+ // getDailyFitnessData();
402
+ } else if (isPermissionGranted === 'CANCELLED') {
403
+ showHealthConnectPermissionDeniedDialog();
404
+ }
405
+ } catch (e) {
406
+ if (isLoggingEnabled) {
407
+ console.error(e);
408
+ }
409
+ }
410
+ };
411
+
412
+ const getHealthConnectStatus = async () => {
413
+ try {
414
+ const healthConnectStatus =
415
+ await NativeModules.VisitFitnessModule.getHealthConnectStatus();
416
+
417
+ if (isLoggingEnabled) {
418
+ console.log('getHealthConnectStatus: ' + healthConnectStatus);
419
+ }
420
+
421
+ if (healthConnectStatus === 'NOT_SUPPORTED') {
422
+ webviewRef.current?.injectJavaScript(
423
+ 'window.healthConnectNotSupported()'
424
+ );
425
+ } else if (healthConnectStatus === 'NOT_INSTALLED') {
426
+ webviewRef.current?.injectJavaScript(
427
+ 'window.healthConnectNotInstall()'
428
+ );
429
+ webviewRef.current?.injectJavaScript(
430
+ 'window.updateFitnessPermissions(false,0,0)'
431
+ );
432
+ } else if (healthConnectStatus === 'INSTALLED') {
433
+ webviewRef.current?.injectJavaScript('window.healthConnectAvailable()');
434
+
435
+ webviewRef.current?.injectJavaScript(
436
+ 'window.updateFitnessPermissions(false,0,0)'
437
+ );
438
+ } else if (healthConnectStatus === 'CONNECTED') {
439
+ getDailyFitnessData();
440
+ }
441
+ } catch (e) {
442
+ if (isLoggingEnabled) {
443
+ console.error(e);
444
+ }
445
+ }
446
+ };
447
+
448
+ const getDailyFitnessData = async () => {
449
+ if (isLoggingEnabled) {
450
+ console.log('getDailyFitnessData() called');
451
+ }
452
+
453
+ try {
454
+ const dailyFitnessData =
455
+ await NativeModules.VisitFitnessModule.requestDailyFitnessData();
456
+
457
+ if (dailyFitnessData) {
458
+ webviewRef.current?.injectJavaScript(dailyFitnessData);
459
+ }
460
+ } catch (error) {
461
+ if (isLoggingEnabled) {
462
+ console.log(error);
463
+ }
464
+ }
465
+ };
466
+
467
+ const requestActivityData = async (type, frequency, timeStamp) => {
468
+ if (isLoggingEnabled) {
469
+ console.log('requestActivityData() called');
470
+ }
471
+
472
+ try {
473
+ const graphData =
474
+ await NativeModules.VisitFitnessModule.requestActivityDataFromHealthConnect(
475
+ type,
476
+ frequency,
477
+ timeStamp
478
+ );
479
+
480
+ if (isLoggingEnabled) {
481
+ console.log(`requestActivityData() data: ` + graphData);
482
+ }
483
+
484
+ if (graphData) {
485
+ webviewRef.current?.injectJavaScript('window.' + graphData);
486
+ }
487
+ } catch (error) {
488
+ if (isLoggingEnabled) {
489
+ console.log(error);
490
+ }
491
+ }
492
+ };
493
+
494
+ const updateApiBaseUrl = async (
495
+ apiBaseUrl,
496
+ authtoken,
497
+ googleFitLastSync,
498
+ gfHourlyLastSync
499
+ ) => {
500
+ if (isLoggingEnabled) {
501
+ console.log('updateApiBaseUrl() called.');
502
+ }
503
+
504
+ try {
505
+ const message = await NativeModules.VisitFitnessModule.updateApiBaseUrl(
506
+ apiBaseUrl,
507
+ authtoken,
508
+ googleFitLastSync,
509
+ gfHourlyLastSync
510
+ );
511
+
512
+ if (isLoggingEnabled) {
513
+ console.log(message);
514
+ }
515
+ } catch (error) {
516
+ if (isLoggingEnabled) {
517
+ console.log(error);
518
+ }
519
+ }
520
+ };
521
+
522
+ const runBeforeFirst = `
523
+ window.isNativeApp = true;
524
+ window.platform = "ANDROID";
525
+ window.setSdkPlatform('ANDROID');
526
+ true; // note: this is required, or you'll sometimes get silent failures
527
+ `;
528
+
529
+ const handleMessage = (event) => {
530
+ if (event.nativeEvent.data != null) {
531
+ try {
532
+ if (isLoggingEnabled) {
533
+ console.log('Event :' + event.nativeEvent.data);
534
+ }
535
+ const parsedObject = JSON.parse(event.nativeEvent.data);
536
+ if (parsedObject.method != null) {
537
+ switch (parsedObject.method) {
538
+ case 'GET_HEALTH_CONNECT_STATUS':
539
+ getHealthConnectStatus();
540
+ break;
541
+ case 'CONNECT_TO_GOOGLE_FIT':
542
+ askForHealthConnectPermission();
543
+ break;
544
+ case 'UPDATE_PLATFORM':
545
+ webviewRef.current?.injectJavaScript(
546
+ 'window.setSdkPlatform("ANDROID")'
547
+ );
548
+ break;
549
+ case 'UPDATE_API_BASE_URL':
550
+ {
551
+ let apiBaseUrl = parsedObject.apiBaseUrl;
552
+ let authtoken = parsedObject.authtoken;
553
+
554
+ let googleFitLastSync = parsedObject.googleFitLastSync;
555
+ let gfHourlyLastSync = parsedObject.gfHourlyLastSync;
556
+
557
+ if (isLoggingEnabled) {
558
+ console.log(
559
+ 'apiBaseUrl: ' +
560
+ 'NOT SHOWN' +
561
+ ' authtoken: ' +
562
+ 'NOT SHOWN' +
563
+ ' googleFitLastSync: ' +
564
+ googleFitLastSync +
565
+ ' gfHourlyLastSync: ' +
566
+ gfHourlyLastSync
567
+ );
568
+ }
569
+
570
+ updateApiBaseUrl(
571
+ apiBaseUrl,
572
+ authtoken,
573
+ googleFitLastSync,
574
+ gfHourlyLastSync
575
+ );
576
+ }
577
+ break;
578
+ case 'GET_DATA_TO_GENERATE_GRAPH':
579
+ {
580
+ let type = parsedObject.type;
581
+ let frequency = parsedObject.frequency;
582
+ let timeStamp = parsedObject.timestamp;
583
+
584
+ if (isLoggingEnabled) {
585
+ console.log(
586
+ 'type: ' +
587
+ type +
588
+ ' frequency:' +
589
+ frequency +
590
+ ' timeStamp: ' +
591
+ timeStamp
592
+ );
593
+ }
594
+
595
+ requestActivityData(type, frequency, timeStamp);
596
+ }
597
+ break;
598
+ case 'GET_LOCATION_PERMISSIONS':
599
+ console.log('GET_LOCATION_PERMISSIONS');
600
+ requestLocationPermission();
601
+ break;
602
+ case 'OPEN_PDF':
603
+ {
604
+ let pdfUrl = parsedObject.url;
605
+ // console.log("pdfUrl "+pdfUrl);
606
+
607
+ Linking.openURL(pdfUrl);
608
+ }
609
+ break;
610
+ case 'OPEN_FACE_SCAN_FLOW':
611
+ EventRegister.emitEvent('visit-event', {
612
+ message: 'OPEN_FACE_SCAN_FLOW',
613
+ });
614
+ break;
615
+ case 'CLOSE_VIEW':
616
+ break;
617
+
618
+ default:
619
+ break;
620
+ }
621
+ }
622
+ } catch (exception) {
623
+ console.log('Exception occured:' + exception.message);
624
+ }
625
+ }
626
+ };
627
+
628
+ const [canGoBack, setCanGoBack] = useState(false);
629
+
630
+ const handleBack = useCallback(() => {
631
+ if (canGoBack && webviewRef.current) {
632
+ webviewRef.current?.goBack();
633
+ return true;
634
+ }
635
+ return false;
636
+ }, [canGoBack]);
637
+
638
+ useEffect(() => {
639
+ // Subscribe to GPS/location setting changes
640
+ const locationSub = addListener(({ locationEnabled }) => {
641
+ if (locationEnabled) {
642
+ checkLocationPermissionAndSendCallback();
643
+ }
644
+ });
645
+
646
+ // Subscribe to Android hardware back press
647
+ const backSub = BackHandler.addEventListener(
648
+ 'hardwareBackPress',
649
+ handleBack
650
+ );
651
+
652
+ // Cleanup subscriptions on unmount
653
+ return () => {
654
+ backSub?.remove();
655
+ locationSub?.remove();
656
+ };
657
+ }, [handleBack]);
658
+
659
+ const checkLocationPermissionAndSendCallback = async () => {
660
+ const isLocationPermissionAvailable = await PermissionsAndroid.check(
661
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
662
+ );
663
+
664
+ if (isLoggingEnabled) {
665
+ console.log(
666
+ 'checkLocationPermissionAndSendCallback() isLocationPermissionAvailable: ' +
667
+ isLocationPermissionAvailable +
668
+ 'isGPSPermissionAvailabe: true'
669
+ );
670
+ }
671
+
672
+ if (isLocationPermissionAvailable) {
673
+ var finalString = `window.checkTheGpsPermission(true)`;
674
+
675
+ console.log('listener: ' + finalString);
676
+
677
+ webviewRef.current?.injectJavaScript(finalString);
678
+ }
679
+ };
680
+
681
+ return (
682
+ // eslint-disable-next-line react-native/no-inline-styles
683
+ <SafeAreaView style={{ flex: 1 }}>
684
+ {source ? (
685
+ <WebView
686
+ ref={webviewRef}
687
+ source={{
688
+ uri: source,
689
+ headers: {
690
+ platform: 'ANDROID',
691
+ },
692
+ }}
693
+ onMessage={handleMessage}
694
+ injectedJavaScriptBeforeContentLoaded={runBeforeFirst}
695
+ javaScriptEnabled={true}
696
+ onLoadProgress={(event) => setCanGoBack(event.nativeEvent.canGoBack)}
697
+ onError={(errorMessage) => {
698
+ EventRegister.emitEvent('visit-event', {
699
+ message: 'web-view-error',
700
+ errorMessage: errorMessage,
701
+ });
702
+ if (isLoggingEnabled) {
703
+ console.warn('Webview error: ', errorMessage);
704
+ }
705
+ }}
706
+ />
707
+ ) : null}
708
+ </SafeAreaView>
709
+ );
710
+ };
711
+
712
+ export const fetchDailyFitnessData = (startTimeStamp, isLoggingEnabled) => {
713
+ return new Promise((resolve, reject) => {
714
+ console.log('fetchDailyFitnessData called: ' + startTimeStamp);
715
+
716
+ NativeModules.VisitFitnessModule.fetchDailyFitnessData(startTimeStamp)
717
+ .then((result) => {
718
+ resolve(result);
719
+ })
720
+ .catch((err) => reject(err));
721
+ });
722
+ };
723
+
724
+ export const fetchHourlyFitnessData = (startTimeStamp, isLoggingEnabled) => {
725
+ return new Promise((resolve, reject) => {
726
+ if (isLoggingEnabled) {
727
+ console.log('fetchHourlyFitnessData called: ' + startTimeStamp);
728
+ }
729
+
730
+ NativeModules.VisitFitnessModule.fetchHourlyFitnessData(startTimeStamp)
731
+ .then((result) => {
732
+ resolve(result);
733
+ })
734
+ .catch((err) => reject(err));
735
+ });
736
+ };
737
+
738
+ // debounce, deferred
739
+ // function debounce(task, ms) {
740
+ // let t = { promise: null, cancel: (_) => void 0 };
741
+ // return async (...args) => {
742
+ // try {
743
+ // t.cancel();
744
+ // t = deferred(ms);
745
+ // await t.promise;
746
+ // await task(...args);
747
+ // } catch (_) {
748
+ // console.log('cleaning up cancelled promise');
749
+ // }
750
+ // };
751
+ // }
752
+
753
+ // function deferred(ms) {
754
+ // let cancel,
755
+ // promise = new Promise((resolve, reject) => {
756
+ // cancel = reject;
757
+ // setTimeout(resolve, ms);
758
+ // });
759
+ // return { promise, cancel };
760
+ // }
761
+
762
+ export default VisitRnSdkView;
763
+
764
+ VisitRnSdkView.defaultProps = {
765
+ cpsid: '',
766
+ token: '',
767
+ baseUrl: '',
768
+ errorBaseUrl: '',
769
+ moduleName: '',
770
+ environment: '',
771
+ magicLink: '',
772
+ isLoggingEnabled: false,
773
+ };