BlueWallet/helpers/scan-qr.ts

46 lines
1.7 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';
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
2021-02-18 16:37:43 +03:00
* @return {Promise<string>}
*/
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) => {},
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
navigationRef.navigate('ScanQRCodeRoot', {
2023-10-17 09:35:10 -04:00
screen: 'ScanQRCode',
params,
});
});
});
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 };