Files
inji-wallet/screens/Scan/ScanScreen.tsx
Swati Goel 142228b0db INJI-473) Code clean up and fixing metro errors/warnings (#947)
* feat(INJI-473) - Removed unused injiTourGuide action.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - Removed unused logKey action.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - Removed unused backendInfo api and state.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - simplify isPasscodeSet logic.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - Move logState to commonUtil to remove cyclic dependency.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - Delete unused code.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - Refactor code to use util function for iOS or isAndroid.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - Move Issuers_Key_Ref into utils.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - Remove profile related resource from setting screen

Signed-off-by: Swati Goel <meet2swati@gmail.com>

* feat(INJI-473) - Remove unused code for locales.

Signed-off-by: Swati Goel <meet2swati@gmail.com>

---------

Signed-off-by: Swati Goel <meet2swati@gmail.com>
2023-10-20 12:50:39 +05:30

188 lines
5.2 KiB
TypeScript

import React, {useEffect, useState} from 'react';
import {useTranslation} from 'react-i18next';
import {
ErrorMessageOverlay,
MessageOverlay,
} from '../../components/MessageOverlay';
import {QrScanner} from '../../components/QrScanner';
import {Button, Centered, Column, Text} from '../../components/ui';
import {Theme} from '../../components/ui/styleUtils';
import {QrLogin} from '../QrLogin/QrLogin';
import {useScanScreen} from './ScanScreenController';
import BluetoothStateManager from 'react-native-bluetooth-state-manager';
import {Linking, Platform} from 'react-native';
import {useNavigation, NavigationProp} from '@react-navigation/native';
import {MainBottomTabParamList} from '../../routes/main';
import {BOTTOM_TAB_ROUTES} from '../../routes/routesConstants';
import {isIOS} from '../../shared/constants';
export const ScanScreen: React.FC = () => {
type ScanScreenNavigation = NavigationProp<MainBottomTabParamList>;
const {t} = useTranslation('ScanScreen');
const controller = useScanScreen();
const navigation = useNavigation<ScanScreenNavigation>();
const [isBluetoothOn, setIsBluetoothOn] = useState(false);
useEffect(() => {
(async () => {
await BluetoothStateManager.onStateChange(state => {
if (state === 'PoweredOff') {
setIsBluetoothOn(false);
} else {
setIsBluetoothOn(true);
}
}, true);
})();
}, [isBluetoothOn]);
// TODO(kludge): skip running this hook on every render
useEffect(() => {
if (controller.isStartPermissionCheck && !controller.isEmpty)
controller.START_PERMISSION_CHECK();
});
const openSettings = () => {
Linking.openSettings();
};
function noShareableVcText() {
return (
<Text align="center" color={Theme.Colors.errorMessage} margin="0 10">
{t('noShareableVcs')}
</Text>
);
}
function bluetoothIsOffText() {
return (
<Text align="center" color={Theme.Colors.errorMessage} margin="0 10">
{t(isIOS() ? 'bluetoothStateIos' : 'bluetoothStateAndroid')}
</Text>
);
}
function allowBluetoothPermissionComponent() {
return (
<Column padding="24" fill align="space-between">
<Centered fill>
<Text align="center" color={Theme.Colors.errorMessage}>
{t('enableBluetoothMessage')}
</Text>
</Centered>
<Button
title={t('enableBluetoothButtonText')}
onPress={openSettings}></Button>
</Column>
);
}
function allowNearbyDevicesPermissionComponent() {
return (
<Column padding="24" fill align="space-between">
<Centered fill>
<Text align="center" color={Theme.Colors.errorMessage}>
{t('errors.nearbyDevicesPermissionDenied.message')}
</Text>
</Centered>
<Button
title={t('errors.nearbyDevicesPermissionDenied.button')}
onPress={openSettings}></Button>
</Column>
);
}
function allowLocationComponent() {
return (
<Column padding="24" fill align="space-between">
<Centered fill>
<Text align="center" color={Theme.Colors.errorMessage}>
{controller.locationError.message}
</Text>
</Centered>
<Button
title={controller.locationError.button}
onPress={controller.LOCATION_REQUEST}
/>
</Column>
);
}
function qrScannerComponent() {
return (
<Column crossAlign="center" margin="0 0 0 -6">
<QrScanner onQrFound={controller.SCAN} title={t('scanningGuide')} />
</Column>
);
}
function loadQRScanner() {
if (controller.isEmpty) {
return noShareableVcText();
}
if (controller.isNearByDevicesPermissionDenied) {
return allowNearbyDevicesPermissionComponent();
}
if (
(controller.isBluetoothDenied || !isBluetoothOn) &&
controller.isReadyForBluetoothStateCheck
) {
return bluetoothIsOffText();
}
if (controller.isLocationDisabled || controller.isLocationDenied) {
return allowLocationComponent();
}
if (controller.isBluetoothPermissionDenied) {
return allowBluetoothPermissionComponent();
}
if (controller.isScanning) {
return qrScannerComponent();
}
}
function displayStorageLimitReachedError(): React.ReactNode {
return (
!controller.isEmpty && (
<ErrorMessageOverlay
isVisible={
controller.isMinimumStorageRequiredForAuditEntryLimitReached
}
translationPath={'ScanScreen'}
error="errors.storageLimitReached"
onDismiss={() => navigation.navigate(BOTTOM_TAB_ROUTES.home)}
/>
)
);
}
return (
<Column
fill
padding="24 0"
backgroundColor={Theme.Colors.whiteBackgroundColor}>
<Centered
fill
align="space-evenly"
backgroundColor={Theme.Colors.whiteBackgroundColor}>
{loadQRScanner()}
{controller.isQrLogin && (
<QrLogin
isVisible={controller.isQrLogin}
service={controller.isQrRef}
/>
)}
<MessageOverlay
isVisible={controller.isQrLoginstoring}
title={t('loading')}
progress
/>
</Centered>
{displayStorageLimitReachedError()}
</Column>
);
};