react-native-visit-rn-sdk 0.4.8 → 0.4.10

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,6 +7,7 @@ import {
7
7
  PermissionsAndroid,
8
8
  BackHandler,
9
9
  Linking,
10
+ Alert,
10
11
  } from 'react-native';
11
12
 
12
13
  import WebView from 'react-native-webview';
@@ -40,7 +41,19 @@ const VisitRnSdkView = ({
40
41
  isLoggingEnabled,
41
42
  }) => {
42
43
  const [source, setSource] = useState('');
44
+
45
+ const [
46
+ showPermissionAlreadyDeniedDialog,
47
+ setShowPermissionAlreadyDeniedDialog,
48
+ ] = useState(false);
49
+
43
50
  useEffect(() => {
51
+ if (isLoggingEnabled) {
52
+ console.log('useEffect ran');
53
+ }
54
+
55
+ NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
56
+
44
57
  if ((magicLink?.trim()?.length || 0) > 0) {
45
58
  setSource(magicLink);
46
59
  } else {
@@ -90,8 +103,9 @@ const VisitRnSdkView = ({
90
103
  .then((response) => {
91
104
  let data = response.data;
92
105
  // let visitMagicLink = data.result; //@Deprecated. Superseded by magic code usage.
93
- let errorMessage = data.errorMessage;
94
- let magicCode = data.magicCode;
106
+ const errorMessage = data.errorMessage;
107
+ const magicCode = data.magicCode;
108
+ const responseReferenceId = data.responseReferenceId;
95
109
 
96
110
  let finalBaseUrl = '';
97
111
 
@@ -108,6 +122,13 @@ const VisitRnSdkView = ({
108
122
  finalUrl += `&tab=${moduleName}`;
109
123
  }
110
124
 
125
+ if (
126
+ typeof responseReferenceId === 'string' &&
127
+ responseReferenceId.trim().length > 0
128
+ ) {
129
+ finalUrl += `&responseReferenceId=${responseReferenceId}`;
130
+ }
131
+
111
132
  if (isLoggingEnabled) {
112
133
  console.log('magicLink: ' + finalUrl);
113
134
  }
@@ -191,97 +212,169 @@ const VisitRnSdkView = ({
191
212
 
192
213
  const webviewRef = useRef(null);
193
214
 
194
- const requestActivityRecognitionPermission = async () => {
195
- console.log('inside requestActivityRecognitionPermission()');
196
- try {
197
- const granted = await PermissionsAndroid.request(
198
- PermissionsAndroid.PERMISSIONS.ACTIVITY_RECOGNITION,
215
+ const showLocationPermissionAlert = () => {
216
+ Alert.alert(
217
+ 'Permission Required',
218
+ 'Allow location permission from app settings',
219
+ [
199
220
  {
200
- title: 'Need Activity Recognition Permission',
201
- message: 'This needs access to your Fitness Permission',
202
- buttonNeutral: 'Ask Me Later',
203
- buttonNegative: 'Cancel',
204
- buttonPositive: 'OK',
205
- }
206
- );
207
- if (granted === PermissionsAndroid.RESULTS.GRANTED) {
208
- console.log('ACTIVITY_RECOGNITION granted');
209
- askForGoogleFitPermission();
210
- } else {
211
- console.log('Fitness permission denied');
212
- }
213
- } catch (err) {
214
- console.warn(err);
215
- }
221
+ text: 'Cancel',
222
+ onPress: () => {
223
+ console.log('Cancel clicked');
224
+ },
225
+ },
226
+ {
227
+ text: 'Go to Settings',
228
+ onPress: () => {
229
+ Linking.openSettings();
230
+ },
231
+ },
232
+ ]
233
+ );
216
234
  };
217
235
 
218
236
  const requestLocationPermission = async () => {
219
237
  try {
220
- const granted = await PermissionsAndroid.request(
221
- PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
222
- {
223
- title: 'Need Location Permission',
224
- message: 'Need access to location permission',
225
- buttonNeutral: 'Ask Me Later',
226
- buttonNegative: 'Cancel',
227
- buttonPositive: 'OK',
228
- }
238
+ console.log('requestLocationPermission called');
239
+
240
+ const isLocationPermissionPresent = await PermissionsAndroid.check(
241
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
229
242
  );
230
- if (granted === PermissionsAndroid.RESULTS.GRANTED) {
231
- console.log('Location permission granted');
232
243
 
233
- if (!enabled) {
234
- requestResolution();
235
- } else {
236
- var finalString = `window.checkTheGpsPermission(true)`;
237
- console.log('requestLocationPermission: ' + finalString);
244
+ console.log(
245
+ 'isLocationPermissionPresent: ' +
246
+ isLocationPermissionPresent +
247
+ ' showPermissionAlreadyDeniedDialog: ' +
248
+ showPermissionAlreadyDeniedDialog
249
+ );
238
250
 
239
- webviewRef.current?.injectJavaScript(finalString);
240
- }
251
+ if (!isLocationPermissionPresent && showPermissionAlreadyDeniedDialog) {
252
+ console.log('showLocationPermissionAlert() called');
253
+
254
+ showLocationPermissionAlert();
241
255
  } else {
242
- console.log('Location permission denied');
256
+ console.log('requesting location permission');
257
+
258
+ const granted = await PermissionsAndroid.request(
259
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
260
+ {
261
+ title: 'Need Location Permission',
262
+ message: 'Need access to location permission',
263
+ buttonNeutral: 'Ask Me Later',
264
+ buttonNegative: 'Cancel',
265
+ buttonPositive: 'OK',
266
+ }
267
+ );
268
+ if (granted === PermissionsAndroid.RESULTS.GRANTED) {
269
+ if (isLoggingEnabled) {
270
+ console.log('Location permission granted');
271
+ }
272
+ setShowPermissionAlreadyDeniedDialog(false);
273
+
274
+ if (!enabled) {
275
+ requestResolution();
276
+ } else {
277
+ var finalString = `window.checkTheGpsPermission(true)`;
278
+ console.log('requestLocationPermission: ' + finalString);
279
+
280
+ webviewRef.current?.injectJavaScript(finalString);
281
+ }
282
+ } else {
283
+ setShowPermissionAlreadyDeniedDialog(true);
284
+ console.log('Location permission denied');
285
+ }
243
286
  }
244
287
  } catch (e) {
245
288
  console.error(e);
246
289
  }
247
290
  };
248
291
 
249
- const askForGoogleFitPermission = async () => {
292
+ const askForHealthConnectPermission = async () => {
250
293
  try {
251
- NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
252
-
253
294
  const isPermissionGranted =
254
295
  await NativeModules.VisitFitnessModule.askForFitnessPermission();
255
- if (isPermissionGranted == 'GRANTED') {
296
+
297
+ if (isLoggingEnabled) {
298
+ console.log('isPermissionGranted: ' + isPermissionGranted);
299
+ }
300
+
301
+ if (isPermissionGranted === 'GRANTED') {
256
302
  getDailyFitnessData();
257
303
  }
258
- console.log(`Google Fit Permissionl: ${isPermissionGranted}`);
259
304
  } catch (e) {
260
- console.error(e);
305
+ if (isLoggingEnabled) {
306
+ console.error(e);
307
+ }
261
308
  }
262
309
  };
263
310
 
264
- const getDailyFitnessData = () => {
265
- console.log('getDailyFitnessData() called');
311
+ const getHealthConnectStatus = async () => {
312
+ try {
313
+ const healthConnectStatus =
314
+ await NativeModules.VisitFitnessModule.getHealthConnectStatus();
266
315
 
267
- NativeModules.VisitFitnessModule.requestDailyFitnessData((data) => {
268
- // console.log(`getDailyFitnessData() data: ` + data);
269
- webviewRef.current?.injectJavaScript(data);
270
- });
316
+ if (isLoggingEnabled) {
317
+ console.log('getHealthConnectStatus: ' + healthConnectStatus);
318
+ }
319
+
320
+ if (healthConnectStatus === 'NOT_SUPPORTED') {
321
+ webviewRef.current?.injectJavaScript(
322
+ 'window.healthConnectNotSupported()'
323
+ );
324
+ } else if (healthConnectStatus === 'NOT_INSTALLED') {
325
+ webviewRef.current?.injectJavaScript(
326
+ 'window.healthConnectNotInstall()'
327
+ );
328
+ } else if (healthConnectStatus === 'INSTALLED') {
329
+ webviewRef.current?.injectJavaScript('window.healthConnectAvailable()');
330
+ }
331
+ } catch (e) {
332
+ if (isLoggingEnabled) {
333
+ console.error(e);
334
+ }
335
+ }
336
+ };
337
+
338
+ const getDailyFitnessData = async () => {
339
+ if (isLoggingEnabled) {
340
+ console.log('getDailyFitnessData() called');
341
+ }
342
+
343
+ try {
344
+ const dailyFitnessData =
345
+ await NativeModules.VisitFitnessModule.requestDailyFitnessData();
346
+
347
+ webviewRef.current?.injectJavaScript(dailyFitnessData);
348
+ } catch (error) {
349
+ if (isLoggingEnabled) {
350
+ console.log(error);
351
+ }
352
+ }
271
353
  };
272
354
 
273
- const requestActivityData = (type, frequency, timeStamp) => {
274
- console.log('requestActivityData() called');
355
+ const requestActivityData = async (type, frequency, timeStamp) => {
356
+ if (isLoggingEnabled) {
357
+ console.log('requestActivityData() called');
358
+ }
359
+
360
+ try {
361
+ const graphData =
362
+ await NativeModules.VisitFitnessModule.requestActivityDataFromHealthConnect(
363
+ type,
364
+ frequency,
365
+ timeStamp
366
+ );
367
+
368
+ if (isLoggingEnabled) {
369
+ console.log(`requestActivityData() data: ` + graphData);
370
+ }
275
371
 
276
- NativeModules.VisitFitnessModule.requestActivityDataFromGoogleFit(
277
- type,
278
- frequency,
279
- timeStamp,
280
- (data) => {
281
- // console.log(`requestActivityData() data: ` + data);
282
- webviewRef.current?.injectJavaScript('window.' + data);
372
+ webviewRef.current?.injectJavaScript('window.' + graphData);
373
+ } catch (error) {
374
+ if (isLoggingEnabled) {
375
+ console.log(error);
283
376
  }
284
- );
377
+ }
285
378
  };
286
379
 
287
380
  const updateApiBaseUrl = (
@@ -290,8 +383,9 @@ const VisitRnSdkView = ({
290
383
  googleFitLastSync,
291
384
  gfHourlyLastSync
292
385
  ) => {
293
- console.log('updateApiBaseUrl() called.');
294
- NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
386
+ if (isLoggingEnabled) {
387
+ console.log('updateApiBaseUrl() called.');
388
+ }
295
389
 
296
390
  NativeModules.VisitFitnessModule.updateApiBaseUrl(
297
391
  apiBaseUrl,
@@ -311,16 +405,17 @@ const VisitRnSdkView = ({
311
405
  const handleMessage = (event) => {
312
406
  if (event.nativeEvent.data != null) {
313
407
  try {
314
- // console.log("Event :"+event.nativeEvent.data);
408
+ if (isLoggingEnabled) {
409
+ console.log('Event :' + event.nativeEvent.data);
410
+ }
315
411
  const parsedObject = JSON.parse(event.nativeEvent.data);
316
412
  if (parsedObject.method != null) {
317
413
  switch (parsedObject.method) {
414
+ case 'GET_HEALTH_CONNECT_STATUS':
415
+ getHealthConnectStatus();
416
+ break;
318
417
  case 'CONNECT_TO_GOOGLE_FIT':
319
- if (parseInt(DeviceInfo.getSystemVersion(), 10) >= 10) {
320
- requestActivityRecognitionPermission();
321
- } else {
322
- askForGoogleFitPermission();
323
- }
418
+ askForHealthConnectPermission();
324
419
  break;
325
420
  case 'UPDATE_PLATFORM':
326
421
  webviewRef.current?.injectJavaScript(
@@ -335,16 +430,18 @@ const VisitRnSdkView = ({
335
430
  let googleFitLastSync = parsedObject.googleFitLastSync;
336
431
  let gfHourlyLastSync = parsedObject.gfHourlyLastSync;
337
432
 
338
- console.log(
339
- 'apiBaseUrl: ' +
340
- 'NOT SHOWN' +
341
- ' authtoken: ' +
342
- 'NOT SHOWN' +
343
- ' googleFitLastSync: ' +
344
- googleFitLastSync +
345
- ' gfHourlyLastSync: ' +
346
- gfHourlyLastSync
347
- );
433
+ if (isLoggingEnabled) {
434
+ console.log(
435
+ 'apiBaseUrl: ' +
436
+ 'NOT SHOWN' +
437
+ ' authtoken: ' +
438
+ 'NOT SHOWN' +
439
+ ' googleFitLastSync: ' +
440
+ googleFitLastSync +
441
+ ' gfHourlyLastSync: ' +
442
+ gfHourlyLastSync
443
+ );
444
+ }
348
445
 
349
446
  updateApiBaseUrl(
350
447
  apiBaseUrl,
@@ -360,22 +457,23 @@ const VisitRnSdkView = ({
360
457
  let frequency = parsedObject.frequency;
361
458
  let timeStamp = parsedObject.timestamp;
362
459
 
363
- console.log(
364
- 'type: ' +
365
- type +
366
- ' frequency:' +
367
- frequency +
368
- ' timeStamp: ' +
369
- timeStamp
370
- );
460
+ if (isLoggingEnabled) {
461
+ console.log(
462
+ 'type: ' +
463
+ type +
464
+ ' frequency:' +
465
+ frequency +
466
+ ' timeStamp: ' +
467
+ timeStamp
468
+ );
469
+ }
371
470
 
372
471
  requestActivityData(type, frequency, timeStamp);
373
472
  }
374
473
  break;
375
474
  case 'GET_LOCATION_PERMISSIONS':
376
- {
377
- requestLocationPermission();
378
- }
475
+ console.log('GET_LOCATION_PERMISSIONS');
476
+ requestLocationPermission();
379
477
  break;
380
478
  case 'OPEN_PDF':
381
479
  {
@@ -386,8 +484,6 @@ const VisitRnSdkView = ({
386
484
  }
387
485
  break;
388
486
  case 'CLOSE_VIEW':
389
- {
390
- }
391
487
  break;
392
488
 
393
489
  default:
@@ -413,11 +509,7 @@ const VisitRnSdkView = ({
413
509
  useEffect(() => {
414
510
  const gpsListener = addListener(({ locationEnabled }) => {
415
511
  if (locationEnabled) {
416
- var finalString = `window.checkTheGpsPermission(true)`;
417
-
418
- console.log('listener: ' + finalString);
419
-
420
- webviewRef.current?.injectJavaScript(finalString);
512
+ checkLocationPermissionAndSendCallback();
421
513
  }
422
514
  });
423
515
 
@@ -428,7 +520,28 @@ const VisitRnSdkView = ({
428
520
  };
429
521
  }, [handleBack]);
430
522
 
523
+ const checkLocationPermissionAndSendCallback = async () => {
524
+ const isLocationPermissionAvailable = await PermissionsAndroid.check(
525
+ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
526
+ );
527
+
528
+ console.log(
529
+ 'checkLocationPermissionAndSendCallback() isLocationPermissionAvailable: ' +
530
+ isLocationPermissionAvailable +
531
+ 'isGPSPermissionAvailabe: true'
532
+ );
533
+
534
+ if (isLocationPermissionAvailable) {
535
+ var finalString = `window.checkTheGpsPermission(true)`;
536
+
537
+ console.log('listener: ' + finalString);
538
+
539
+ webviewRef.current?.injectJavaScript(finalString);
540
+ }
541
+ };
542
+
431
543
  return (
544
+ // eslint-disable-next-line react-native/no-inline-styles
432
545
  <SafeAreaView style={{ flex: 1 }}>
433
546
  {source ? (
434
547
  <WebView
@@ -462,8 +575,6 @@ export const fetchDailyFitnessData = (startTimeStamp, isLoggingEnabled) => {
462
575
  return new Promise((resolve, reject) => {
463
576
  console.log('fetchDailyFitnessData called: ' + startTimeStamp);
464
577
 
465
- NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
466
-
467
578
  NativeModules.VisitFitnessModule.fetchDailyFitnessData(startTimeStamp)
468
579
  .then((result) => {
469
580
  resolve(result);
@@ -474,9 +585,9 @@ export const fetchDailyFitnessData = (startTimeStamp, isLoggingEnabled) => {
474
585
 
475
586
  export const fetchHourlyFitnessData = (startTimeStamp, isLoggingEnabled) => {
476
587
  return new Promise((resolve, reject) => {
477
- console.log('fetchHourlyFitnessData called: ' + startTimeStamp);
478
-
479
- NativeModules.VisitFitnessModule.initiateSDK(isLoggingEnabled);
588
+ if (isLoggingEnabled) {
589
+ console.log('fetchHourlyFitnessData called: ' + startTimeStamp);
590
+ }
480
591
 
481
592
  NativeModules.VisitFitnessModule.fetchHourlyFitnessData(startTimeStamp)
482
593
  .then((result) => {
@@ -486,6 +597,30 @@ export const fetchHourlyFitnessData = (startTimeStamp, isLoggingEnabled) => {
486
597
  });
487
598
  };
488
599
 
600
+ // debounce, deferred
601
+ // function debounce(task, ms) {
602
+ // let t = { promise: null, cancel: (_) => void 0 };
603
+ // return async (...args) => {
604
+ // try {
605
+ // t.cancel();
606
+ // t = deferred(ms);
607
+ // await t.promise;
608
+ // await task(...args);
609
+ // } catch (_) {
610
+ // console.log('cleaning up cancelled promise');
611
+ // }
612
+ // };
613
+ // }
614
+
615
+ // function deferred(ms) {
616
+ // let cancel,
617
+ // promise = new Promise((resolve, reject) => {
618
+ // cancel = reject;
619
+ // setTimeout(resolve, ms);
620
+ // });
621
+ // return { promise, cancel };
622
+ // }
623
+
489
624
  export default VisitRnSdkView;
490
625
 
491
626
  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,