Skip to content

Commit

Permalink
bugfix: QR code scanning was failing to retrieve recipient's address …
Browse files Browse the repository at this point in the history
…in send flow
  • Loading branch information
mcayuelas-ledger committed Jan 9, 2025
1 parent e785a43 commit cc15ba0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 177 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-jobs-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": minor
---

QR code scanning was failling to retrieve recipient's address in send flow

This file was deleted.

49 changes: 0 additions & 49 deletions apps/ledger-live-mobile/src/components/CameraScreen/index.tsx

This file was deleted.

141 changes: 87 additions & 54 deletions apps/ledger-live-mobile/src/components/Scanner.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,97 @@
import React, { useContext } from "react";
import { StyleSheet, View } from "react-native";
import { BarCodeScanningResult, Camera, CameraType } from "expo-camera/legacy";
import { Flex } from "@ledgerhq/native-ui";
import StyledStatusBar from "./StyledStatusBar";
import CameraScreen from "./CameraScreen";
import getWindowDimensions from "~/logic/getWindowDimensions";
import RequiresCameraPermissions from "./RequiresCameraPermissions";
import CameraPermissionContext from "./RequiresCameraPermissions/CameraPermissionContext";
import ForceTheme from "./theme/ForceTheme";
import React from "react";
import { Box, Flex, Icons, NumberedList, Text } from "@ledgerhq/native-ui";
import { Trans, useTranslation } from "react-i18next";
import { useTheme } from "styled-components/native";
import { CameraView, BarcodeScanningResult } from "expo-camera";
import RequiresCameraPermissions from "~/components/RequiresCameraPermissions";
import CameraPermissionContext from "~/components/RequiresCameraPermissions/CameraPermissionContext";
import ScanTargetSvg from "./CameraScreen/ScanTargetSvg";

type Props = {
onResult: (_: string) => void;
liveQrCode?: boolean;
progress?: number;
instruction?: React.ReactNode | string;
onResult: (data: string) => void;
liveQRCode?: boolean;
};

const Scanner = ({ onResult, liveQrCode, progress, instruction }: Props) => {
const hasPermission = useContext(CameraPermissionContext).permissionGranted;
const { width, height } = getWindowDimensions();

if (!hasPermission) return <View />;
const ScanQrCode = ({ onResult, liveQRCode = false }: Props) => {
const { t } = useTranslation();
const { colors } = useTheme();
const onBarCodeScanned = ({ data }: BarcodeScanningResult) => {
onResult(data);
};

return (
<ForceTheme selectedPalette="dark">
<Flex flex={1}>
<StyledStatusBar barStyle="light-content" />
<Camera
style={styles.camera}
type={CameraType.back}
ratio="16:9"
onBarCodeScanned={({ data }: BarCodeScanningResult) => onResult(data)}
barCodeScannerSettings={{
barCodeTypes: ["qr"],
}}
<Flex
flex={1}
justifyContent="flex-start"
alignItems={"center"}
rowGap={24}
testID={"scan-camera"}
>
<Box height={100} />
<RequiresCameraPermissions optimisticallyMountChildren fallBackHasNoBackground>
<Flex
borderRadius={36}
overflow={"hidden"}
position={"relative"}
width={288}
height={288}
justifyContent={"center"}
alignItems={"center"}
>
<CameraScreen
liveQrCode={liveQrCode}
progress={progress}
width={width}
height={height}
instruction={instruction}
/>
</Camera>
</Flex>
</ForceTheme>
);
};

const ScannerWrappedInRequiresCameraPermission: React.FC<Props> = props => {
return (
<RequiresCameraPermissions optimisticallyMountChildren>
<Scanner {...props} />
</RequiresCameraPermissions>
<CameraPermissionContext.Consumer>
{({ permissionGranted }) =>
permissionGranted ? (
<CameraView
active={permissionGranted ?? false}
style={{
backgroundColor: colors.neutral.c50,
width: 280,
height: 280,
}}
barcodeScannerSettings={{
barcodeTypes: ["qr"],
}}
onBarcodeScanned={onBarCodeScanned}
/>
) : null
}
</CameraPermissionContext.Consumer>
<ScanTargetSvg style={{ position: "absolute" }} />
</Flex>
<Flex flexDirection={"row"} alignItems={"center"} columnGap={8}>
<Text variant="bodyLineHeight">
{t("walletSync.synchronize.qrCode.scan.description")}
</Text>
<Icons.QrCode />
</Flex>
{liveQRCode && (
<>
<Text variant={"h5"} fontWeight={"semiBold"} mb={6}>
<Trans
i18nKey={liveQRCode ? "account.import.newScan.title" : "send.scan.descBottom"}
/>
</Text>
<Flex alignContent="center" justifyContent="center" p={4}>
<NumberedList
items={[
{
description: <Trans i18nKey={"account.import.newScan.instructions.line1"} />,
},
{
description: <Trans i18nKey={"account.import.newScan.instructions.line2"} />,
},
{
description: <Trans i18nKey={"account.import.newScan.instructions.line3"} />,
},
]}
itemContainerProps={{ mb: 3 }}
/>
</Flex>
</>
)}
</RequiresCameraPermissions>
</Flex>
);
};

export default ScannerWrappedInRequiresCameraPermission;
const styles = StyleSheet.create({
camera: {
flex: 1,
},
});
export default ScanQrCode;
2 changes: 1 addition & 1 deletion apps/ledger-live-mobile/src/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -3725,7 +3725,7 @@
"highFeeModal": "Be careful, network fees are greater than <1>10%</1> of the amount. Do you want to continue?",
"scan": {
"title": "Scan QR code",
"descBottom": "Please center the QR code inside the square.",
"descBottom": "Scan QR code",
"fallback": {
"header": "Scan QR code",
"title": "Enable camera",
Expand Down
4 changes: 2 additions & 2 deletions apps/ledger-live-mobile/src/screens/ImportAccounts/Scan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ class Scan extends PureComponent<
};

render() {
const { progress, error } = this.state;
const { error } = this.state;
return (
<View style={styles.root}>
<TrackScreen category="Account Import Sync" />
<Scanner onResult={this.onBarCodeRead} progress={progress} liveQrCode />
<Scanner onResult={this.onBarCodeRead} liveQRCode />
<GenericErrorBottomModal error={error} onClose={this.onCloseError} />
</View>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BenchmarkQRStream extends PureComponent<Props, State> {
},
]}
>
<Scanner onResult={this.onBarCodeRead} liveQrCode />
<Scanner onResult={this.onBarCodeRead} liveQRCode />
<View style={[darkenStyle]} />
<View style={[darkenStyle, styles.centered]}>
<View style={styles.centered}>
Expand Down

0 comments on commit cc15ba0

Please sign in to comment.