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';
|
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
|
2021-02-18 16:37:43 +03:00
|
|
|
* @return {Promise<string>}
|
|
|
|
*/
|
2024-05-26 10:44:50 -04:00
|
|
|
function scanQrHelper(currentScreenName: string, showFileImportButton = true, onDismiss?: () => void): Promise<string | null> {
|
2023-10-17 09:35:10 -04:00
|
|
|
return requestCameraAuthorization().then(() => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const params = {
|
|
|
|
showFileImportButton: Boolean(showFileImportButton),
|
|
|
|
onBarScanned: (data: any) => {},
|
2024-01-07 15:14:04 -04:00
|
|
|
onDismiss,
|
2023-10-17 09:35:10 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
params.onBarScanned = function (data: any) {
|
|
|
|
setTimeout(() => resolve(data.data || data), 1);
|
2024-05-26 10:51:09 -04:00
|
|
|
navigationRef.navigate({ name: currentScreenName, params: {}, merge: true });
|
2023-10-17 09:35:10 -04:00
|
|
|
};
|
2021-02-18 16:37:43 +03:00
|
|
|
|
2024-05-26 10:44:50 -04:00
|
|
|
navigationRef.navigate('ScanQRCodeRoot', {
|
2023-10-17 09:35:10 -04:00
|
|
|
screen: 'ScanQRCode',
|
|
|
|
params,
|
|
|
|
});
|
2024-01-30 21:26:43 +00: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 };
|