react-native-visit-rn-sdk 0.5.5 → 2.0.2

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.
@@ -7,7 +7,8 @@ import {
7
7
  PermissionsAndroid,
8
8
  BackHandler,
9
9
  Linking,
10
- Alert
10
+ Alert,
11
+ AppState,
11
12
  } from 'react-native';
12
13
 
13
14
  import WebView from 'react-native-webview';
@@ -41,14 +42,20 @@ const VisitRnSdkView = ({
41
42
  isLoggingEnabled,
42
43
  }) => {
43
44
  const [source, setSource] = useState('');
45
+ const [appState, setAppState] = useState(AppState.currentState);
44
46
 
45
47
  const [
46
48
  showPermissionAlreadyDeniedDialog,
47
49
  setShowPermissionAlreadyDeniedDialog,
48
50
  ] = useState(false);
49
51
 
50
-
51
52
  useEffect(() => {
53
+ if (isLoggingEnabled) {
54
+ console.log('useEffect ran');
55
+ }
56
+
57
+ NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
58
+
52
59
  if ((magicLink?.trim()?.length || 0) > 0) {
53
60
  setSource(magicLink);
54
61
  } else {
@@ -98,8 +105,9 @@ const VisitRnSdkView = ({
98
105
  .then((response) => {
99
106
  let data = response.data;
100
107
  // let visitMagicLink = data.result; //@Deprecated. Superseded by magic code usage.
101
- let errorMessage = data.errorMessage;
102
- let magicCode = data.magicCode;
108
+ const errorMessage = data.errorMessage;
109
+ const magicCode = data.magicCode;
110
+ const responseReferenceId = data.responseReferenceId;
103
111
 
104
112
  let finalBaseUrl = '';
105
113
 
@@ -116,6 +124,13 @@ const VisitRnSdkView = ({
116
124
  finalUrl += `&tab=${moduleName}`;
117
125
  }
118
126
 
127
+ if (
128
+ typeof responseReferenceId === 'string' &&
129
+ responseReferenceId.trim().length > 0
130
+ ) {
131
+ finalUrl += `&responseReferenceId=${responseReferenceId}`;
132
+ }
133
+
119
134
  if (isLoggingEnabled) {
120
135
  console.log('magicLink: ' + finalUrl);
121
136
  }
@@ -188,6 +203,34 @@ const VisitRnSdkView = ({
188
203
  isLoggingEnabled,
189
204
  ]);
190
205
 
206
+ useEffect(() => {
207
+ const subscription = AppState.addEventListener(
208
+ 'change',
209
+ handleAppStateChange
210
+ );
211
+ console.log(`AppState.addEventListener added, current state: ${appState}`);
212
+
213
+ return () => {
214
+ subscription.remove();
215
+ };
216
+ }, [appState]); // Include appState in the dependency array to ensure it's up to date.
217
+
218
+ const handleAppStateChange = (nextAppState) => {
219
+ // console.log(
220
+ // "nextAppState: " + nextAppState + ", previousState: " + appState
221
+ // );
222
+
223
+ // Instead of relying on the old appState, use the nextAppState directly
224
+ if (appState.match(/inactive|background/) && nextAppState === 'active') {
225
+ if (isLoggingEnabled) {
226
+ console.log('App has come to the foreground!');
227
+ }
228
+ getHealthConnectStatus();
229
+ }
230
+
231
+ setAppState(nextAppState); // Update the state with the new app state
232
+ };
233
+
191
234
  const [enabled, requestResolution] = useLocationSettings(
192
235
  {
193
236
  priority: HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
@@ -199,30 +242,6 @@ const VisitRnSdkView = ({
199
242
 
200
243
  const webviewRef = useRef(null);
201
244
 
202
- const requestActivityRecognitionPermission = async () => {
203
- console.log('inside requestActivityRecognitionPermission()');
204
- try {
205
- const granted = await PermissionsAndroid.request(
206
- PermissionsAndroid.PERMISSIONS.ACTIVITY_RECOGNITION,
207
- {
208
- title: 'Need Activity Recognition Permission',
209
- message: 'This needs access to your Fitness Permission',
210
- buttonNeutral: 'Ask Me Later',
211
- buttonNegative: 'Cancel',
212
- buttonPositive: 'OK',
213
- }
214
- );
215
- if (granted === PermissionsAndroid.RESULTS.GRANTED) {
216
- console.log('ACTIVITY_RECOGNITION granted');
217
- askForGoogleFitPermission();
218
- } else {
219
- console.log('Fitness permission denied');
220
- }
221
- } catch (err) {
222
- console.warn(err);
223
- }
224
- };
225
-
226
245
  const showLocationPermissionAlert = () => {
227
246
  Alert.alert(
228
247
  'Permission Required',
@@ -240,27 +259,32 @@ const VisitRnSdkView = ({
240
259
  Linking.openSettings();
241
260
  },
242
261
  },
243
- ],
262
+ ]
244
263
  );
245
264
  };
246
265
 
247
266
  const requestLocationPermission = async () => {
248
267
  try {
249
- console.log("requestLocationPermission called");
250
-
268
+ console.log('requestLocationPermission called');
251
269
 
252
- const isLocationPermissionPresent = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
270
+ const isLocationPermissionPresent = await PermissionsAndroid.check(
271
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
272
+ );
253
273
 
254
- console.log("isLocationPermissionPresent: "+isLocationPermissionPresent + " showPermissionAlreadyDeniedDialog: "+showPermissionAlreadyDeniedDialog);
255
-
274
+ console.log(
275
+ 'isLocationPermissionPresent: ' +
276
+ isLocationPermissionPresent +
277
+ ' showPermissionAlreadyDeniedDialog: ' +
278
+ showPermissionAlreadyDeniedDialog
279
+ );
256
280
 
257
281
  if (!isLocationPermissionPresent && showPermissionAlreadyDeniedDialog) {
282
+ console.log('showLocationPermissionAlert() called');
258
283
 
259
- console.log("showLocationPermissionAlert() called");
260
-
261
284
  showLocationPermissionAlert();
262
285
  } else {
263
- console.log("requesting location permission");
286
+ console.log('requesting location permission');
287
+
264
288
  const granted = await PermissionsAndroid.request(
265
289
  PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
266
290
  {
@@ -272,15 +296,17 @@ const VisitRnSdkView = ({
272
296
  }
273
297
  );
274
298
  if (granted === PermissionsAndroid.RESULTS.GRANTED) {
275
- console.log('Location permission granted');
299
+ if (isLoggingEnabled) {
300
+ console.log('Location permission granted');
301
+ }
276
302
  setShowPermissionAlreadyDeniedDialog(false);
277
-
303
+
278
304
  if (!enabled) {
279
305
  requestResolution();
280
306
  } else {
281
307
  var finalString = `window.checkTheGpsPermission(true)`;
282
308
  console.log('requestLocationPermission: ' + finalString);
283
-
309
+
284
310
  webviewRef.current?.injectJavaScript(finalString);
285
311
  }
286
312
  } else {
@@ -288,50 +314,106 @@ const VisitRnSdkView = ({
288
314
  console.log('Location permission denied');
289
315
  }
290
316
  }
291
-
292
317
  } catch (e) {
293
318
  console.error(e);
294
319
  }
295
320
  };
296
321
 
297
- const askForGoogleFitPermission = async () => {
322
+ const askForHealthConnectPermission = async () => {
298
323
  try {
299
- NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
300
-
301
324
  const isPermissionGranted =
302
325
  await NativeModules.VisitFitnessModule.askForFitnessPermission();
303
- if (isPermissionGranted == 'GRANTED') {
326
+
327
+ if (isLoggingEnabled) {
328
+ console.log('isPermissionGranted: ' + isPermissionGranted);
329
+ }
330
+
331
+ if (isPermissionGranted === 'GRANTED') {
304
332
  getDailyFitnessData();
305
333
  }
306
- console.log(`Google Fit Permissionl: ${isPermissionGranted}`);
307
334
  } catch (e) {
308
- console.error(e);
335
+ if (isLoggingEnabled) {
336
+ console.error(e);
337
+ }
309
338
  }
310
339
  };
311
340
 
341
+ const getHealthConnectStatus = async () => {
342
+ try {
343
+ const healthConnectStatus =
344
+ await NativeModules.VisitFitnessModule.getHealthConnectStatus();
312
345
 
346
+ if (isLoggingEnabled) {
347
+ console.log('getHealthConnectStatus: ' + healthConnectStatus);
348
+ }
313
349
 
314
- const getDailyFitnessData = () => {
315
- console.log('getDailyFitnessData() called');
350
+ if (healthConnectStatus === 'NOT_SUPPORTED') {
351
+ webviewRef.current?.injectJavaScript(
352
+ 'window.healthConnectNotSupported()'
353
+ );
354
+ } else if (healthConnectStatus === 'NOT_INSTALLED') {
355
+ webviewRef.current?.injectJavaScript(
356
+ 'window.healthConnectNotInstall()'
357
+ );
358
+ webviewRef.current?.injectJavaScript(
359
+ 'window.updateFitnessPermissions(false,0,0)'
360
+ );
361
+ } else if (healthConnectStatus === 'INSTALLED') {
362
+ webviewRef.current?.injectJavaScript('window.healthConnectAvailable()');
316
363
 
317
- NativeModules.VisitFitnessModule.requestDailyFitnessData((data) => {
318
- // console.log(`getDailyFitnessData() data: ` + data);
319
- webviewRef.current?.injectJavaScript(data);
320
- });
364
+ webviewRef.current?.injectJavaScript(
365
+ 'window.updateFitnessPermissions(false,0,0)'
366
+ );
367
+ } else if (healthConnectStatus === 'CONNECTED') {
368
+ //don't do anything here because it is responsibility of askForHealthConnectPermission
369
+ }
370
+ } catch (e) {
371
+ if (isLoggingEnabled) {
372
+ console.error(e);
373
+ }
374
+ }
321
375
  };
322
376
 
323
- const requestActivityData = (type, frequency, timeStamp) => {
324
- console.log('requestActivityData() called');
377
+ const getDailyFitnessData = async () => {
378
+ if (isLoggingEnabled) {
379
+ console.log('getDailyFitnessData() called');
380
+ }
381
+
382
+ try {
383
+ const dailyFitnessData =
384
+ await NativeModules.VisitFitnessModule.requestDailyFitnessData();
325
385
 
326
- NativeModules.VisitFitnessModule.requestActivityDataFromGoogleFit(
327
- type,
328
- frequency,
329
- timeStamp,
330
- (data) => {
331
- // console.log(`requestActivityData() data: ` + data);
332
- webviewRef.current?.injectJavaScript('window.' + data);
386
+ webviewRef.current?.injectJavaScript(dailyFitnessData);
387
+ } catch (error) {
388
+ if (isLoggingEnabled) {
389
+ console.log(error);
333
390
  }
334
- );
391
+ }
392
+ };
393
+
394
+ const requestActivityData = async (type, frequency, timeStamp) => {
395
+ if (isLoggingEnabled) {
396
+ console.log('requestActivityData() called');
397
+ }
398
+
399
+ try {
400
+ const graphData =
401
+ await NativeModules.VisitFitnessModule.requestActivityDataFromHealthConnect(
402
+ type,
403
+ frequency,
404
+ timeStamp
405
+ );
406
+
407
+ if (isLoggingEnabled) {
408
+ console.log(`requestActivityData() data: ` + graphData);
409
+ }
410
+
411
+ webviewRef.current?.injectJavaScript('window.' + graphData);
412
+ } catch (error) {
413
+ if (isLoggingEnabled) {
414
+ console.log(error);
415
+ }
416
+ }
335
417
  };
336
418
 
337
419
  const updateApiBaseUrl = (
@@ -340,8 +422,9 @@ const VisitRnSdkView = ({
340
422
  googleFitLastSync,
341
423
  gfHourlyLastSync
342
424
  ) => {
343
- console.log('updateApiBaseUrl() called.');
344
- NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
425
+ if (isLoggingEnabled) {
426
+ console.log('updateApiBaseUrl() called.');
427
+ }
345
428
 
346
429
  NativeModules.VisitFitnessModule.updateApiBaseUrl(
347
430
  apiBaseUrl,
@@ -361,16 +444,17 @@ const VisitRnSdkView = ({
361
444
  const handleMessage = (event) => {
362
445
  if (event.nativeEvent.data != null) {
363
446
  try {
364
- // console.log("Event :"+event.nativeEvent.data);
447
+ if (isLoggingEnabled) {
448
+ // console.log('Event :' + event.nativeEvent.data);
449
+ }
365
450
  const parsedObject = JSON.parse(event.nativeEvent.data);
366
451
  if (parsedObject.method != null) {
367
452
  switch (parsedObject.method) {
453
+ case 'GET_HEALTH_CONNECT_STATUS':
454
+ getHealthConnectStatus();
455
+ break;
368
456
  case 'CONNECT_TO_GOOGLE_FIT':
369
- if (parseInt(DeviceInfo.getSystemVersion(), 10) >= 10) {
370
- requestActivityRecognitionPermission();
371
- } else {
372
- askForGoogleFitPermission();
373
- }
457
+ askForHealthConnectPermission();
374
458
  break;
375
459
  case 'UPDATE_PLATFORM':
376
460
  webviewRef.current?.injectJavaScript(
@@ -385,16 +469,18 @@ const VisitRnSdkView = ({
385
469
  let googleFitLastSync = parsedObject.googleFitLastSync;
386
470
  let gfHourlyLastSync = parsedObject.gfHourlyLastSync;
387
471
 
388
- console.log(
389
- 'apiBaseUrl: ' +
390
- 'NOT SHOWN' +
391
- ' authtoken: ' +
392
- 'NOT SHOWN' +
393
- ' googleFitLastSync: ' +
394
- googleFitLastSync +
395
- ' gfHourlyLastSync: ' +
396
- gfHourlyLastSync
397
- );
472
+ if (isLoggingEnabled) {
473
+ console.log(
474
+ 'apiBaseUrl: ' +
475
+ 'NOT SHOWN' +
476
+ ' authtoken: ' +
477
+ 'NOT SHOWN' +
478
+ ' googleFitLastSync: ' +
479
+ googleFitLastSync +
480
+ ' gfHourlyLastSync: ' +
481
+ gfHourlyLastSync
482
+ );
483
+ }
398
484
 
399
485
  updateApiBaseUrl(
400
486
  apiBaseUrl,
@@ -410,23 +496,23 @@ const VisitRnSdkView = ({
410
496
  let frequency = parsedObject.frequency;
411
497
  let timeStamp = parsedObject.timestamp;
412
498
 
413
- console.log(
414
- 'type: ' +
415
- type +
416
- ' frequency:' +
417
- frequency +
418
- ' timeStamp: ' +
419
- timeStamp
420
- );
499
+ if (isLoggingEnabled) {
500
+ console.log(
501
+ 'type: ' +
502
+ type +
503
+ ' frequency:' +
504
+ frequency +
505
+ ' timeStamp: ' +
506
+ timeStamp
507
+ );
508
+ }
421
509
 
422
510
  requestActivityData(type, frequency, timeStamp);
423
511
  }
424
512
  break;
425
513
  case 'GET_LOCATION_PERMISSIONS':
426
- {
427
- console.log("GET_LOCATION_PERMISSIONS")
428
- requestLocationPermission();
429
- }
514
+ console.log('GET_LOCATION_PERMISSIONS');
515
+ requestLocationPermission();
430
516
  break;
431
517
  case 'OPEN_PDF':
432
518
  {
@@ -437,8 +523,6 @@ const VisitRnSdkView = ({
437
523
  }
438
524
  break;
439
525
  case 'CLOSE_VIEW':
440
- {
441
- }
442
526
  break;
443
527
 
444
528
  default:
@@ -464,9 +548,7 @@ const VisitRnSdkView = ({
464
548
  useEffect(() => {
465
549
  const gpsListener = addListener(({ locationEnabled }) => {
466
550
  if (locationEnabled) {
467
-
468
551
  checkLocationPermissionAndSendCallback();
469
-
470
552
  }
471
553
  });
472
554
 
@@ -477,24 +559,30 @@ const VisitRnSdkView = ({
477
559
  };
478
560
  }, [handleBack]);
479
561
 
480
- const checkLocationPermissionAndSendCallback = async () =>{
481
-
482
- const isLocationPermissionAvailable = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
562
+ const checkLocationPermissionAndSendCallback = async () => {
563
+ const isLocationPermissionAvailable = await PermissionsAndroid.check(
564
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
565
+ );
483
566
 
484
- console.log("checkLocationPermissionAndSendCallback() isLocationPermissionAvailable: "+isLocationPermissionAvailable + "isGPSPermissionAvailabe: true");
567
+ if (isLoggingEnabled) {
568
+ console.log(
569
+ 'checkLocationPermissionAndSendCallback() isLocationPermissionAvailable: ' +
570
+ isLocationPermissionAvailable +
571
+ 'isGPSPermissionAvailabe: true'
572
+ );
573
+ }
485
574
 
486
- if(isLocationPermissionAvailable){
575
+ if (isLocationPermissionAvailable) {
487
576
  var finalString = `window.checkTheGpsPermission(true)`;
488
577
 
489
578
  console.log('listener: ' + finalString);
490
-
579
+
491
580
  webviewRef.current?.injectJavaScript(finalString);
492
581
  }
493
-
494
- }
495
-
582
+ };
496
583
 
497
584
  return (
585
+ // eslint-disable-next-line react-native/no-inline-styles
498
586
  <SafeAreaView style={{ flex: 1 }}>
499
587
  {source ? (
500
588
  <WebView
@@ -528,8 +616,6 @@ export const fetchDailyFitnessData = (startTimeStamp, isLoggingEnabled) => {
528
616
  return new Promise((resolve, reject) => {
529
617
  console.log('fetchDailyFitnessData called: ' + startTimeStamp);
530
618
 
531
- NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
532
-
533
619
  NativeModules.VisitFitnessModule.fetchDailyFitnessData(startTimeStamp)
534
620
  .then((result) => {
535
621
  resolve(result);
@@ -540,9 +626,9 @@ export const fetchDailyFitnessData = (startTimeStamp, isLoggingEnabled) => {
540
626
 
541
627
  export const fetchHourlyFitnessData = (startTimeStamp, isLoggingEnabled) => {
542
628
  return new Promise((resolve, reject) => {
543
- console.log('fetchHourlyFitnessData called: ' + startTimeStamp);
544
-
545
- NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
629
+ if (isLoggingEnabled) {
630
+ console.log('fetchHourlyFitnessData called: ' + startTimeStamp);
631
+ }
546
632
 
547
633
  NativeModules.VisitFitnessModule.fetchHourlyFitnessData(startTimeStamp)
548
634
  .then((result) => {
@@ -552,6 +638,30 @@ export const fetchHourlyFitnessData = (startTimeStamp, isLoggingEnabled) => {
552
638
  });
553
639
  };
554
640
 
641
+ // debounce, deferred
642
+ // function debounce(task, ms) {
643
+ // let t = { promise: null, cancel: (_) => void 0 };
644
+ // return async (...args) => {
645
+ // try {
646
+ // t.cancel();
647
+ // t = deferred(ms);
648
+ // await t.promise;
649
+ // await task(...args);
650
+ // } catch (_) {
651
+ // console.log('cleaning up cancelled promise');
652
+ // }
653
+ // };
654
+ // }
655
+
656
+ // function deferred(ms) {
657
+ // let cancel,
658
+ // promise = new Promise((resolve, reject) => {
659
+ // cancel = reject;
660
+ // setTimeout(resolve, ms);
661
+ // });
662
+ // return { promise, cancel };
663
+ // }
664
+
555
665
  export default VisitRnSdkView;
556
666
 
557
667
  VisitRnSdkView.defaultProps = {
package/src/index.ios.js CHANGED
@@ -37,6 +37,7 @@ const escapeChars = {
37
37
 
38
38
  const unescapeHTML = (str) =>
39
39
  // modified from underscore.string and string.js
40
+ // eslint-disable-next-line no-useless-escape
40
41
  str.replace(/\&([^;]+);/g, (entity, entityCode) => {
41
42
  let match;
42
43
 
@@ -45,7 +46,7 @@ const unescapeHTML = (str) =>
45
46
  } else if ((match = entityCode.match(/^#x([\da-fA-F]+)$/))) {
46
47
  return String.fromCharCode(parseInt(match[1], 16));
47
48
  } else if ((match = entityCode.match(/^#(\d+)$/))) {
48
- return String.fromCharCode(~~match[1]);
49
+ return String.fromCharCode(match[1]);
49
50
  } else {
50
51
  return entity;
51
52
  }
@@ -104,6 +105,7 @@ const VisitRnSdkView = ({
104
105
  }
105
106
  } else if (res.data.message === 'success') {
106
107
  const magicCode = res.data?.magicCode;
108
+ const responseReferenceId = res.data?.responseReferenceId;
107
109
  let finalBaseUrl = '';
108
110
  if (magicCode) {
109
111
  if (environment.toUpperCase() === 'PROD') {
@@ -117,6 +119,14 @@ const VisitRnSdkView = ({
117
119
  if (moduleName?.trim()) {
118
120
  finalUrl += `&tab=${moduleName}`;
119
121
  }
122
+
123
+ if (
124
+ typeof responseReferenceId === 'string' &&
125
+ responseReferenceId.trim().length > 0
126
+ ) {
127
+ finalUrl += `&responseReferenceId=${responseReferenceId}`;
128
+ }
129
+
120
130
  setSource(finalUrl);
121
131
  }
122
132
  } else {
@@ -219,6 +229,7 @@ const VisitRnSdkView = ({
219
229
  type,
220
230
  frequency,
221
231
  timestamp,
232
+ // eslint-disable-next-line no-shadow
222
233
  apiBaseUrl,
223
234
  authtoken,
224
235
  googleFitLastSync,
@@ -297,6 +308,7 @@ const VisitRnSdkView = ({
297
308
 
298
309
  const { height, width } = Dimensions.get('screen');
299
310
  return (
311
+ // eslint-disable-next-line react-native/no-inline-styles
300
312
  <SafeAreaView style={{ flex: 1, backgroundColor: 'white', height, width }}>
301
313
  {loading ? (
302
314
  <LoadingIndicator />
@@ -347,6 +359,7 @@ const LoadingIndicator = () => {
347
359
  <ActivityIndicator
348
360
  color="#000"
349
361
  size="small"
362
+ // eslint-disable-next-line react-native/no-inline-styles
350
363
  style={{
351
364
  flex: 1,
352
365
  zIndex: 100,