react-native-visit-rn-sdk 6.0.1 → 6.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.
@@ -1,127 +1,208 @@
1
1
  import React, { useRef, useEffect, useState, useCallback } from 'react';
2
- import { EventRegister } from 'react-native-event-listeners';
3
-
4
2
  import {
5
- SafeAreaView,
6
- PermissionsAndroid,
3
+ Alert,
4
+ AppState,
7
5
  BackHandler,
8
6
  Linking,
9
- Alert,
7
+ NativeModules,
8
+ SafeAreaView,
10
9
  } from 'react-native';
11
-
10
+ import {
11
+ check,
12
+ openSettings,
13
+ PERMISSIONS,
14
+ request,
15
+ RESULTS,
16
+ } from 'react-native-permissions';
12
17
  import WebView from 'react-native-webview';
13
18
 
14
- import LocationEnabler from '@visit-health/react-native-location-enabler';
15
-
16
19
  import VideoCallComponent from './components/VideoCallComponent';
17
20
 
18
- const {
19
- PRIORITIES: { HIGH_ACCURACY },
20
- useLocationSettings,
21
- addListener,
22
- } = LocationEnabler;
21
+ const LOCATION_SOURCE_SETTINGS = 'android.settings.LOCATION_SOURCE_SETTINGS';
22
+ const GENERAL_SETTINGS = 'android.settings.SETTINGS';
23
+ const { VisitRnSdkLocation } = NativeModules;
23
24
 
24
25
  const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
25
26
  const source = typeof ssoLink === 'string' ? ssoLink.trim() : '';
27
+ const webviewRef = useRef(null);
28
+ const videoCallRef = useRef(null);
29
+ const locationFlowInProgressRef = useRef(false);
30
+ const pendingSettingsRef = useRef(null);
31
+ const appStateRef = useRef(AppState.currentState);
32
+ const [canGoBack, setCanGoBack] = useState(false);
26
33
 
27
- const [
28
- showPermissionAlreadyDeniedDialog,
29
- setShowPermissionAlreadyDeniedDialog,
30
- ] = useState(false);
31
-
32
- const [enabled, requestResolution] = useLocationSettings(
33
- {
34
- priority: HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
35
- alwaysShow: true, // default false
36
- needBle: true, // default false
34
+ const warn = useCallback(
35
+ (message, error) => {
36
+ if (isLoggingEnabled) {
37
+ console.warn(message, error);
38
+ }
37
39
  },
38
- false /* optional: default undefined */
40
+ [isLoggingEnabled]
39
41
  );
40
42
 
41
- const webviewRef = useRef(null);
42
- const videoCallRef = useRef(null);
43
+ const finishLocationFlow = useCallback((granted) => {
44
+ locationFlowInProgressRef.current = false;
45
+ pendingSettingsRef.current = null;
46
+ webviewRef.current?.injectJavaScript(
47
+ `window.checkTheGpsPermission(${granted}); true;`
48
+ );
49
+ }, []);
43
50
 
44
- const showLocationPermissionAlert = () => {
51
+ const showLocationPermissionSettingsAlert = useCallback(() => {
45
52
  Alert.alert(
46
- 'Permission Required',
47
- 'Allow location permission from app settings',
53
+ 'Location permission required',
54
+ 'Allow location access in app settings to continue.',
48
55
  [
49
56
  {
50
57
  text: 'Cancel',
51
- onPress: () => {
52
- console.log('Cancel clicked');
53
- },
58
+ style: 'cancel',
59
+ onPress: () => finishLocationFlow(false),
54
60
  },
55
61
  {
56
- text: 'Go to Settings',
57
- onPress: () => {
58
- Linking.openSettings();
62
+ text: 'Open Settings',
63
+ onPress: async () => {
64
+ pendingSettingsRef.current = 'location-permission';
65
+ try {
66
+ await openSettings();
67
+ } catch (error) {
68
+ warn('Unable to open application settings.', error);
69
+ finishLocationFlow(false);
70
+ }
59
71
  },
60
72
  },
61
- ]
73
+ ],
74
+ { cancelable: false }
62
75
  );
63
- };
76
+ }, [finishLocationFlow, warn]);
64
77
 
65
- const requestLocationPermission = async () => {
78
+ const openLocationServicesSettings = useCallback(async () => {
79
+ pendingSettingsRef.current = 'location-services';
66
80
  try {
67
- console.log('requestLocationPermission called');
68
-
69
- const isLocationPermissionPresent = await PermissionsAndroid.check(
70
- PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
81
+ await Linking.sendIntent(LOCATION_SOURCE_SETTINGS);
82
+ } catch (locationSettingsError) {
83
+ warn(
84
+ 'Unable to open Location Services settings; opening general settings.',
85
+ locationSettingsError
71
86
  );
87
+ try {
88
+ await Linking.sendIntent(GENERAL_SETTINGS);
89
+ } catch (generalSettingsError) {
90
+ warn('Unable to open Android settings.', generalSettingsError);
91
+ finishLocationFlow(false);
92
+ }
93
+ }
94
+ }, [finishLocationFlow, warn]);
95
+
96
+ const checkLocationServices = useCallback(
97
+ async ({ promptIfDisabled }) => {
98
+ try {
99
+ if (!VisitRnSdkLocation?.isLocationServicesEnabled) {
100
+ throw new Error('VisitRnSdkLocation native module is unavailable.');
101
+ }
72
102
 
73
- console.log(
74
- 'isLocationPermissionPresent: ' +
75
- isLocationPermissionPresent +
76
- ' showPermissionAlreadyDeniedDialog: ' +
77
- showPermissionAlreadyDeniedDialog
103
+ const isEnabled = await VisitRnSdkLocation.isLocationServicesEnabled();
104
+ if (isEnabled) {
105
+ finishLocationFlow(true);
106
+ return;
107
+ }
108
+
109
+ if (!promptIfDisabled) {
110
+ finishLocationFlow(false);
111
+ return;
112
+ }
113
+
114
+ Alert.alert(
115
+ 'Turn on Location Services',
116
+ 'Enable Location Services in phone settings to continue.',
117
+ [
118
+ {
119
+ text: 'Cancel',
120
+ style: 'cancel',
121
+ onPress: () => finishLocationFlow(false),
122
+ },
123
+ {
124
+ text: 'Open Settings',
125
+ onPress: openLocationServicesSettings,
126
+ },
127
+ ],
128
+ { cancelable: false }
129
+ );
130
+ } catch (error) {
131
+ warn('Unable to check Location Services.', error);
132
+ finishLocationFlow(false);
133
+ }
134
+ },
135
+ [finishLocationFlow, openLocationServicesSettings, warn]
136
+ );
137
+
138
+ const requestLocationPermission = useCallback(async () => {
139
+ if (
140
+ locationFlowInProgressRef.current ||
141
+ pendingSettingsRef.current !== null
142
+ ) {
143
+ return;
144
+ }
145
+
146
+ locationFlowInProgressRef.current = true;
147
+
148
+ try {
149
+ let permissionStatus = await check(
150
+ PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION
78
151
  );
79
152
 
80
- if (!isLocationPermissionPresent && showPermissionAlreadyDeniedDialog) {
81
- console.log('showLocationPermissionAlert() called');
82
-
83
- showLocationPermissionAlert();
84
- } else {
85
- console.log('requesting location permission');
86
-
87
- const granted = await PermissionsAndroid.request(
88
- PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
89
- {
90
- title: 'Need Location Permission',
91
- message: 'Need access to location permission',
92
- buttonNeutral: 'Ask Me Later',
93
- buttonNegative: 'Cancel',
94
- buttonPositive: 'OK',
95
- }
153
+ if (permissionStatus === RESULTS.DENIED) {
154
+ permissionStatus = await request(
155
+ PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION
96
156
  );
97
- if (granted === PermissionsAndroid.RESULTS.GRANTED) {
98
- if (isLoggingEnabled) {
99
- console.log('Location permission granted');
100
- }
101
- setShowPermissionAlreadyDeniedDialog(false);
157
+ }
102
158
 
103
- if (!enabled) {
104
- requestResolution();
105
- } else {
106
- var finalString = `window.checkTheGpsPermission(true)`;
107
- console.log('requestLocationPermission: ' + finalString);
159
+ if (permissionStatus === RESULTS.GRANTED) {
160
+ await checkLocationServices({ promptIfDisabled: true });
161
+ return;
162
+ }
108
163
 
109
- webviewRef.current?.injectJavaScript(finalString);
110
- }
111
- } else {
112
- setShowPermissionAlreadyDeniedDialog(true);
113
- console.log('Location permission denied');
164
+ if (permissionStatus === RESULTS.BLOCKED) {
165
+ showLocationPermissionSettingsAlert();
166
+ return;
167
+ }
114
168
 
115
- var finalString = `window.checkTheGpsPermission(false)`;
116
- console.log('requestLocationPermission: ' + finalString);
169
+ finishLocationFlow(false);
170
+ } catch (error) {
171
+ warn('Unable to check or request location permission.', error);
172
+ finishLocationFlow(false);
173
+ }
174
+ }, [
175
+ checkLocationServices,
176
+ finishLocationFlow,
177
+ showLocationPermissionSettingsAlert,
178
+ warn,
179
+ ]);
180
+
181
+ const recheckAfterSettings = useCallback(async () => {
182
+ const settingsType = pendingSettingsRef.current;
183
+ if (!settingsType) {
184
+ return;
185
+ }
117
186
 
118
- webviewRef.current?.injectJavaScript(finalString);
119
- }
187
+ pendingSettingsRef.current = null;
188
+
189
+ try {
190
+ const permissionStatus = await check(
191
+ PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION
192
+ );
193
+ if (permissionStatus !== RESULTS.GRANTED) {
194
+ finishLocationFlow(false);
195
+ return;
120
196
  }
121
- } catch (e) {
122
- console.error(e);
197
+
198
+ await checkLocationServices({
199
+ promptIfDisabled: settingsType === 'location-permission',
200
+ });
201
+ } catch (error) {
202
+ warn('Unable to recheck location access after settings.', error);
203
+ finishLocationFlow(false);
123
204
  }
124
- };
205
+ }, [checkLocationServices, finishLocationFlow, warn]);
125
206
 
126
207
  const runBeforeFirst = `
127
208
  window.isNativeApp = true;
@@ -164,10 +245,10 @@ const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
164
245
  const handleMessage = (event) => {
165
246
  if (event.nativeEvent.data != null) {
166
247
  try {
248
+ const parsedObject = JSON.parse(event.nativeEvent.data);
167
249
  if (isLoggingEnabled) {
168
- console.log('Event :' + event.nativeEvent.data);
250
+ console.log('Received WebView method:', parsedObject.method);
169
251
  }
170
- const parsedObject = JSON.parse(event.nativeEvent.data);
171
252
  if (parsedObject.method != null) {
172
253
  switch (parsedObject.method) {
173
254
  case 'startVideoCall':
@@ -179,87 +260,54 @@ const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
179
260
  );
180
261
  break;
181
262
  case 'GET_LOCATION_PERMISSIONS':
182
- console.log('GET_LOCATION_PERMISSIONS');
183
263
  requestLocationPermission();
184
264
  break;
185
265
  case 'OPEN_PDF':
186
- {
187
- let pdfUrl = parsedObject.url;
188
- // console.log("pdfUrl "+pdfUrl);
189
-
190
- Linking.openURL(pdfUrl);
191
- }
192
- break;
193
- case 'OPEN_FACE_SCAN_FLOW':
194
- EventRegister.emitEvent('visit-event', {
195
- message: 'OPEN_FACE_SCAN_FLOW',
196
- });
266
+ Linking.openURL(parsedObject.url);
197
267
  break;
198
268
  case 'CLOSE_VIEW':
199
269
  break;
200
-
201
270
  default:
202
271
  break;
203
272
  }
204
273
  }
205
- } catch (exception) {
206
- console.log('Exception occured:' + exception.message);
274
+ } catch (error) {
275
+ warn('Unable to handle WebView message.', error);
207
276
  }
208
277
  }
209
278
  };
210
279
 
211
- const [canGoBack, setCanGoBack] = useState(false);
212
-
213
280
  const handleBack = useCallback(() => {
214
281
  if (canGoBack && webviewRef.current) {
215
- webviewRef.current?.goBack();
282
+ webviewRef.current.goBack();
216
283
  return true;
217
284
  }
218
285
  return false;
219
286
  }, [canGoBack]);
220
287
 
221
- const checkLocationPermissionAndSendCallback = useCallback(async () => {
222
- const isLocationPermissionAvailable = await PermissionsAndroid.check(
223
- PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
224
- );
225
-
226
- if (isLoggingEnabled) {
227
- console.log(
228
- 'checkLocationPermissionAndSendCallback() isLocationPermissionAvailable: ' +
229
- isLocationPermissionAvailable +
230
- 'isGPSPermissionAvailabe: true'
231
- );
232
- }
233
-
234
- if (isLocationPermissionAvailable) {
235
- var finalString = `window.checkTheGpsPermission(true)`;
236
-
237
- console.log('listener: ' + finalString);
238
-
239
- webviewRef.current?.injectJavaScript(finalString);
240
- }
241
- }, [isLoggingEnabled]);
242
-
243
288
  useEffect(() => {
244
- // Subscribe to GPS/location setting changes
245
- const locationSub = addListener(({ locationEnabled }) => {
246
- if (locationEnabled) {
247
- checkLocationPermissionAndSendCallback();
248
- }
249
- });
250
-
251
- // Subscribe to Android hardware back press
252
289
  const backSub = BackHandler.addEventListener(
253
290
  'hardwareBackPress',
254
291
  handleBack
255
292
  );
293
+ const appStateSub = AppState.addEventListener('change', (nextAppState) => {
294
+ const wasInBackground = /inactive|background/.test(appStateRef.current);
295
+ appStateRef.current = nextAppState;
296
+
297
+ if (
298
+ wasInBackground &&
299
+ nextAppState === 'active' &&
300
+ pendingSettingsRef.current
301
+ ) {
302
+ recheckAfterSettings();
303
+ }
304
+ });
256
305
 
257
- // Cleanup subscriptions on unmount
258
306
  return () => {
259
307
  backSub?.remove();
260
- locationSub?.remove();
308
+ appStateSub?.remove();
261
309
  };
262
- }, [handleBack, checkLocationPermissionAndSendCallback]);
310
+ }, [handleBack, recheckAfterSettings]);
263
311
 
264
312
  return (
265
313
  // eslint-disable-next-line react-native/no-inline-styles
@@ -278,10 +326,6 @@ const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
278
326
  javaScriptEnabled={true}
279
327
  onLoadProgress={(event) => setCanGoBack(event.nativeEvent.canGoBack)}
280
328
  onError={(errorMessage) => {
281
- EventRegister.emitEvent('visit-event', {
282
- message: 'web-view-error',
283
- errorMessage: errorMessage,
284
- });
285
329
  if (isLoggingEnabled) {
286
330
  console.warn('Webview error: ', errorMessage);
287
331
  }
@@ -310,30 +354,6 @@ const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
310
354
  );
311
355
  };
312
356
 
313
- // debounce, deferred
314
- // function debounce(task, ms) {
315
- // let t = { promise: null, cancel: (_) => void 0 };
316
- // return async (...args) => {
317
- // try {
318
- // t.cancel();
319
- // t = deferred(ms);
320
- // await t.promise;
321
- // await task(...args);
322
- // } catch (_) {
323
- // console.log('cleaning up cancelled promise');
324
- // }
325
- // };
326
- // }
327
-
328
- // function deferred(ms) {
329
- // let cancel,
330
- // promise = new Promise((resolve, reject) => {
331
- // cancel = reject;
332
- // setTimeout(resolve, ms);
333
- // });
334
- // return { promise, cancel };
335
- // }
336
-
337
357
  export default VisitRnSdkView;
338
358
 
339
359
  VisitRnSdkView.defaultProps = {
package/src/index.ios.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import React, { useRef, useCallback } from 'react';
2
2
  import { StyleSheet, SafeAreaView, Linking, Dimensions } from 'react-native';
3
- import { EventRegister } from 'react-native-event-listeners';
4
3
  import { WebView } from 'react-native-webview';
5
4
  import VideoCallComponent from './components/VideoCallComponent';
6
5
 
@@ -29,8 +28,6 @@ const unescapeHTML = (str) =>
29
28
  }
30
29
  });
31
30
 
32
- const visitEvent = 'visit-event';
33
-
34
31
  const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
35
32
  const source = typeof ssoLink === 'string' ? ssoLink.trim() : '';
36
33
 
@@ -75,11 +72,12 @@ const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
75
72
  [isLoggingEnabled]
76
73
  );
77
74
 
78
- const handleMessage = async (event) => {
75
+ const handleMessage = (event) => {
79
76
  const data = JSON.parse(unescapeHTML(event.nativeEvent.data));
80
77
  const { method, url } = data;
81
- console.log('handleMessage data is', data);
82
- console.log(unescapeHTML(event.nativeEvent.data));
78
+ if (isLoggingEnabled) {
79
+ console.log('Received WebView method:', method);
80
+ }
83
81
  switch (method) {
84
82
  case 'startVideoCall':
85
83
  startVideoConsultation(data);
@@ -91,11 +89,6 @@ const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
91
89
  case 'OPEN_PDF':
92
90
  Linking.openURL(url);
93
91
  break;
94
- case 'OPEN_FACE_SCAN_FLOW':
95
- EventRegister.emitEvent('visit-event', {
96
- message: 'OPEN_FACE_SCAN_FLOW',
97
- });
98
- break;
99
92
  case 'CLOSE_VIEW':
100
93
  break;
101
94
  case 'GET_LOCATION_PERMISSIONS':
@@ -122,10 +115,6 @@ const VisitRnSdkView = ({ ssoLink, isLoggingEnabled }) => {
122
115
  onMessage={handleMessage}
123
116
  injectedJavaScriptBeforeContentLoaded={runBeforeFirst}
124
117
  onError={(errorMessage) => {
125
- EventRegister.emitEvent(visitEvent, {
126
- message: 'web-view-error',
127
- errorMessage: errorMessage,
128
- });
129
118
  if (isLoggingEnabled) {
130
119
  console.warn('Webview error: ', errorMessage);
131
120
  }
@@ -1 +0,0 @@
1
- //# sourceMappingURL=index.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/index.test.tsx"],"names":[],"mappings":""}