2023-10-17 09:35:10 -04:00
|
|
|
import { Platform } from 'react-native';
|
|
|
|
import { check, request, PERMISSIONS, RESULTS } from 'react-native-permissions';
|
2024-05-26 10:44:50 -04:00
|
|
|
import { navigationRef } from '../NavigationService';
|
2024-07-23 13:44:04 -04:00
|
|
|
|
2021-02-18 16:37:43 +03:00
|
|
|
/**
|
|
|
|
* Helper function that navigates to ScanQR screen, and returns promise that will resolve with the result of a scan,
|
|
|
|
* and then navigates back. If QRCode scan was closed, promise resolves to null.
|
|
|
|
*
|
|
|
|
* @param currentScreenName {string}
|
2021-03-20 13:00:01 +00:00
|
|
|
* @param showFileImportButton {boolean}
|
2021-02-18 16:37:43 +03:00
|
|
|
*
|
2024-01-30 21:26:43 +00:00
|
|
|
* @param onDismiss {function} - if camera is closed via X button it gets triggered
|
2024-07-22 23:04:18 -04:00
|
|
|
* @param useMerge {boolean} - if true, will merge the new screen with the current screen, otherwise will replace the current screen
|
2021-02-18 16:37:43 +03:00
|
|
|
* @return {Promise<string>}
|
|
|
|
*/
|
2024-07-14 22:54:30 -04:00
|
|
|
function scanQrHelper(
|
|
|
|
currentScreenName: string,
|
|
|
|
showFileImportButton = true,
|
|
|
|
onDismiss?: () => void,
|
2024-07-22 23:04:18 -04:00
|
|
|
useMerge = true,
|
2024-07-14 22:54:30 -04:00
|
|
|
): Promise<string | null> {
|
2023-10-17 09:35:10 -04:00
|
|
|
return requestCameraAuthorization().then(() => {
|
|
|
|
return new Promise(resolve => {
|
2024-07-22 23:04:18 -04:00
|
|
|
let params = {};
|
2023-10-17 09:35:10 -04:00
|
|
|
|
2024-07-22 23:04:18 -04:00
|
|
|
if (useMerge) {
|
|
|
|
const onBarScanned = function (data: any) {
|
2024-07-23 13:44:04 -04:00
|
|
|
setTimeout(() => resolve(data.data || data), 1);
|
2024-07-22 23:04:18 -04:00
|
|
|
navigationRef.navigate({ name: currentScreenName, params: {}, merge: true });
|
2024-07-23 13:44:04 -04:00
|
|
|
};
|
|
|
|
|
2024-07-22 23:04:18 -04:00
|
|
|
params = {
|
|
|
|
showFileImportButton: Boolean(showFileImportButton),
|
|
|
|
onDismiss,
|
|
|
|
onBarScanned,
|
|
|
|
};
|
2024-07-23 13:44:04 -04:00
|
|
|
} else {
|
2024-07-22 23:04:18 -04:00
|
|
|
params = { launchedBy: currentScreenName, showFileImportButton: Boolean(showFileImportButton) };
|
2024-07-23 13:44:04 -04:00
|
|
|
}
|
2021-02-18 16:37:43 +03:00
|
|
|
|
2024-07-25 11:58:45 -04:00
|
|
|
navigationRef.navigate({
|
|
|
|
name: 'ScanQRCodeRoot',
|
|
|
|
params: {
|
|
|
|
screen: 'ScanQRCode',
|
|
|
|
params,
|
|
|
|
},
|
|
|
|
merge: true,
|
2023-10-17 09:35:10 -04:00
|
|
|
});
|
2024-01-30 21:26:43 +00:00
|
|
|
});
|
|
|
|
});
|
2023-10-17 09:35:10 -04:00
|
|
|
}
|
2024-07-23 13:44:04 -04:00
|
|
|
|
2023-10-17 09:35:10 -04:00
|
|
|
const isCameraAuthorizationStatusGranted = async () => {
|
|
|
|
const status = await check(Platform.OS === 'android' ? PERMISSIONS.ANDROID.CAMERA : PERMISSIONS.IOS.CAMERA);
|
|
|
|
return status === RESULTS.GRANTED;
|
|
|
|
};
|
2021-02-18 16:37:43 +03:00
|
|
|
|
2023-10-17 09:35:10 -04:00
|
|
|
const requestCameraAuthorization = () => {
|
|
|
|
return request(Platform.OS === 'android' ? PERMISSIONS.ANDROID.CAMERA : PERMISSIONS.IOS.CAMERA);
|
2021-02-18 16:37:43 +03:00
|
|
|
};
|
2022-09-05 19:34:02 +01:00
|
|
|
|
2023-10-17 09:35:10 -04:00
|
|
|
export { scanQrHelper, isCameraAuthorizationStatusGranted, requestCameraAuthorization };
|