Files
inji-wallet/components/QrScanner.tsx
KiruthikaJeyashankar 68488f3f0c [INJIMOB-2381] fix compile and target SDK 34 is causing app crash in debug build (#1877)
* [INJIMOB-2381] update package.json and resolve dependencies for react 0.73.0

Signed-off-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com>

* [INJIMOB-2381] setup for 0.73.6

Signed-off-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com>

* [INJIMOB-2381] fix font & image not loading issue

changes include
- move common image assets to android & iOS folders
- update gradle version to 8.2
- add expo-font asset for Inter regular
- replace useFont with expo-font
- remove flipper (reason: 0.74 react native decoupled flipper)
- npm package updates
- close icon changed to close text in QRScanner.tsx

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>
Signed-off-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com>

* [INJIMOB-2381] update android:mosip script as per updated react native version

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] fix android build failure issue

Changes include:
- rename android assets folder as per convention
- remove unused biometrics dependency
- use env from react-native-dotenv as per module registered in babel config
- revert conversion of close icon to image in QRScanner component

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] fix iOS build issue

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] rm unused local images linking in adnroid

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] fix splash screen not rendering issue in android

Issue: Local assets were not loaded via React Native Image component in android, to fix this @react-native/assets-registry has been added
Reference: https://github.com/expo/expo/issues/31240#issuecomment-2459049911

Other changes:
- remove usage of expo-image as we dont have any explicit usecase for expo-image and its unused

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] rm unused intro slider image assets

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] revert back using local font assets in android

Reason: Previously expo-font was used for fonts, to bring back the same compatiblity it is bought back

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] use GestureHandlerRootView in root level to handle gestures

ref - https://docs.swmansion.com/react-native-gesture-handler/docs/1.x/#js

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] remove custom assets and linking for images in iOS

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] use default parameters instead of defaultProps

Reason: React 18 has marked defaultProps as deprecated and it is removed in React 19 version. To avoid the issue of deprated usage, its modified.

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] remove unused terminal option in android build script

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] remove font assets in android

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] optimize import

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] fix failing test

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] set package.resolved version to 2

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] fix setting of camera view in face scanner

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] fix setting of camera view in face compare

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] remove flipper config file

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] remove unused debug logs

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] refactor use component instead of type

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] refactor use GestureHandlerRootView only on root level

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] refactor remove unused config linking of font assets

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] refactor remove unused image assets

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] fix camera persmission issue in QRScanner

use expo-camera to request and getstatus of camera permission instead of expo-barcode-scanner to avoid multiple re-renders of scan screen in flow of camera permission denial

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] remove package expo-barcode-scanner

expo-barcode-scanner is deprecated and its suggested to use expo-camera to handle the scanning flows. The existing bar-code-scanner implementations have been replaced with expo-camera.

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] use extracted enums instead of legacy expo-camera

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] remove patch for react-native-0.71.8

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] revert date-fs version to 2.28.0

date-fns is not supporting filipino langauge, so we are maintaining patch for that. The patch created is for version 2.28.0 and updating the version of date-fns is causing conflicts in patch application if updated as well. To avoid this issue, version is kept as 2.28.0

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] update readme

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] remove usage of legacy expo-camera in getting image type

Other changes
- remove resolutions block from package.json
- sync pbxproj

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-2381] add active prop to expo-camera as per suggestions

expo-camera suggests "Only one Camera preview can be active at any given time. If you have multiple screens in your app, you should unmount Camera components whenever a screen is unfocused.". To handle this active prop is used which is set to false on unMount of the component whenever rendering Camera from expo-camera

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

---------

Signed-off-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com>
Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>
Signed-off-by: adityankannan-tw <109274996+adityankannan-tw@users.noreply.github.com>
Co-authored-by: Abhishek Paul <paul.apaul.abhishek.ap@gmail.com>
Co-authored-by: adityankannan-tw <109274996+adityankannan-tw@users.noreply.github.com>
2025-04-10 14:37:41 +05:30

186 lines
6.1 KiB
TypeScript

import React, {useContext, useEffect, useState} from 'react';
import Icon from 'react-native-vector-icons/MaterialIcons';
import {Linking, Pressable, TouchableOpacity, View} from 'react-native';
import {Theme} from './ui/styleUtils';
import {Column, Row, Text} from './ui';
import {GlobalContext} from '../shared/GlobalContext';
import {useSelector} from '@xstate/react';
import {selectIsActive} from '../machines/app';
import {useTranslation} from 'react-i18next';
import testIDProps from '../shared/commonUtil';
import {SvgImage} from './ui/svg';
import {isAndroid} from '../shared/constants';
import {CameraView, useCameraPermissions, PermissionStatus, BarcodeScanningResult, CameraType} from 'expo-camera';
import {CameraPosition} from "../shared/Utils";
export const QrScanner: React.FC<QrScannerProps> = props => {
const {t} = useTranslation('QrScanner');
const {appService} = useContext(GlobalContext);
const [scanned, setScanned] = useState(false);
const [cameraType, setCameraType] = useState<CameraType>(CameraPosition.BACK);
const [hasCameraPermission, setHasCameraPermission] = useState<
boolean | null
>(null);
const [
showCameraPermissionDeniedBanner,
setShowCameraPermissionDeniedBanner,
] = useState(false);
const [_, requestCameraPermission, getCameraPermissionsAsync] = useCameraPermissions();
const [isCameraActive, setIsCameraActive] = useState<boolean>(true);
useEffect(() => {
return () => {
setIsCameraActive(false);
};
}, []);
const isActive = useSelector(appService, selectIsActive);
const openSettings = () => {
if (isAndroid()) {
Linking.openSettings();
} else {
Linking.openURL('App-Prefs:Privacy');
}
};
useEffect(() => {
if (isActive && !Boolean(hasCameraPermission)) {
(async () => {
setShowCameraPermissionDeniedBanner(false);
const cameraPermissionResult = await getCameraPermissionsAsync();
if (cameraPermissionResult.status === PermissionStatus.UNDETERMINED) {
const response = await requestCameraPermission();
setHasCameraPermission(response.granted);
if (response.granted === false) {
setShowCameraPermissionDeniedBanner(true);
}
} else if (cameraPermissionResult.status === PermissionStatus.GRANTED) {
setHasCameraPermission(true);
} else {
setHasCameraPermission(false);
setShowCameraPermissionDeniedBanner(true);
}
})();
}
}, [isActive]);
if (hasCameraPermission === null) {
return <View />;
}
const CameraDisabledPopUp: React.FC = () => {
return (
<View
{...testIDProps('cameraDisabledPopup')}
style={Theme.CameraDisabledStyles.container}>
<Column style={Theme.CameraDisabledStyles.banner}>
<Row style={Theme.CameraDisabledStyles.bannerTextContainer}>
<Column>
<Text
testID="cameraAccessDisabled"
color={Theme.Colors.whiteText}
margin="0 0 5 0"
style={Theme.CameraDisabledStyles.bannerTitle}>
{t('cameraAccessDisabled')}
</Text>
<Text
testID="cameraPermissionGuide"
color={Theme.Colors.whiteText}
style={Theme.CameraDisabledStyles.bannerGuide}>
{t('cameraPermissionGuideLabel')}
</Text>
</Column>
<Pressable>
<Icon
testID="close"
name="close"
onPress={() => setShowCameraPermissionDeniedBanner(false)}
color={Theme.Colors.whiteText}
size={18}
/>
</Pressable>
</Row>
<Row
style={Theme.CameraDisabledStyles.bannerEnablePermissionContainer}>
<Pressable
onPress={openSettings}
style={Theme.CameraDisabledStyles.bannerEnablePermission}>
<Text
testID="EnablePermissionText"
color={Theme.Colors.whiteText}>
{t('EnablePermission')}
</Text>
</Pressable>
</Row>
</Column>
</View>
);
};
return (
<>
{hasCameraPermission ? (
<Column style={Theme.CameraEnabledStyles.container}>
<View style={Theme.CameraEnabledStyles.scannerContainer}>
<CameraView
{...testIDProps('camera')}
style={Theme.CameraEnabledStyles.scanner}
barcodeScannerSettings={{
barcodeTypes: [BarCodeTypes.QR],
}}
onBarcodeScanned={scanned ? undefined : onBarcodeScanned}
facing={cameraType}
active={isCameraActive}
/>
</View>
<Column fill align="space-between" style={{marginTop: 24}}>
{props.title && (
<Text
testID="holdPhoneSteadyMessage"
align="center"
style={Theme.CameraEnabledStyles.holdPhoneSteadyText}
margin="24 57 0 57">
{props.title}
</Text>
)}
<Column crossAlign="center">
<TouchableOpacity
onPress={() => {
setCameraType(
cameraType === CameraType.back
? CameraType.front
: CameraType.back,
);
}}>
{SvgImage.FlipCameraIcon()}
</TouchableOpacity>
<Text
testID="flipCameraText"
style={Theme.CameraEnabledStyles.iconText}>
{t('flipCamera')}
</Text>
</Column>
</Column>
</Column>
) : (
<View style={Theme.CameraDisabledStyles.scannerContainer} />
)}
{showCameraPermissionDeniedBanner && <CameraDisabledPopUp />}
</>
);
function onBarcodeScanned(event: BarcodeScanningResult) {
props.onQrFound(event.data);
setScanned(true);
}
};
interface QrScannerProps {
onQrFound: (data: string) => void;
title?: string;
}
enum BarCodeTypes {
QR = 'qr',
}