BlueWallet/helpers/scan-qr.ts

63 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-10-17 09:35:10 -04:00
import { Platform } from 'react-native';
import { check, request, PERMISSIONS, RESULTS } from 'react-native-permissions';
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}
* @param showFileImportButton {boolean}
2021-02-18 16:37:43 +03:00
*
* @param onDismiss {function} - if camera is closed via X button it gets triggered
* @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,
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 => {
let params = {};
2023-10-17 09:35:10 -04:00
if (useMerge) {
const onBarScanned = function (data: any) {
2024-07-23 13:44:04 -04:00
setTimeout(() => resolve(data.data || data), 1);
navigationRef.navigate({ name: currentScreenName, params: {}, merge: true });
2024-07-23 13:44:04 -04:00
};
params = {
showFileImportButton: Boolean(showFileImportButton),
onDismiss,
onBarScanned,
};
2024-07-23 13:44:04 -04:00
} else {
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
});
});
});
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 };