Skip to content

Commit 9456d9b

Browse files
authored
Merge pull request #1543 from alephium/camera
Camera improvements
2 parents 21d9f19 + 306a889 commit 9456d9b

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

.changeset/shaky-numbers-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@alephium/mobile-wallet': patch
3+
---
4+
5+
Show warning when using wrong QR code to import wallet from desktop wallet

apps/mobile-wallet/locales/en-US/translation.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,5 +508,7 @@
508508
"Unknown address": "Unknown address",
509509
"The destination address has never been used before and does not appear in your transaction history. Please, ensure the address is correct before sending.": "The destination address has never been used before and does not appear in your transaction history. Please, ensure the address is correct before sending.",
510510
"The app can adjust the brightness when displaying QR codes.": "The app can adjust the brightness when displaying QR codes.",
511-
"Allow": "Allow"
511+
"Allow": "Allow",
512+
"This is not the QR code you are looking for.": "This is not the QR code you are looking for.",
513+
"To import from the desktop wallet, find the \"Export current wallet\" feature in the desktop wallet app settings.": "To import from the desktop wallet, find the \"Export current wallet\" feature in the desktop wallet app settings."
512514
}

apps/mobile-wallet/src/components/QRCodeScannerModal.tsx

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import { BarcodeScanningResult, Camera, CameraView } from 'expo-camera'
1+
import { isValidAddress } from '@alephium/web3'
2+
import { BarcodeScanningResult, CameraView, useCameraPermissions } from 'expo-camera'
23
import { Camera as CameraIcon } from 'lucide-react-native'
34
import { areFramesComplete, framesToData, parseFramesReducer, progressOfFrames, State as FrameState } from 'qrloop'
4-
import { useEffect, useState } from 'react'
5+
import { useState } from 'react'
56
import { useTranslation } from 'react-i18next'
67
import { Dimensions } from 'react-native'
78
import { Bar as ProgressBar } from 'react-native-progress'
89
import styled, { useTheme } from 'styled-components/native'
910

1011
import AppText from '~/components/AppText'
12+
import Button from '~/components/buttons/Button'
1113
import InfoBox from '~/components/InfoBox'
1214
import Screen, { ScreenSection } from '~/components/layout/Screen'
1315
import ModalWithBackdrop from '~/components/ModalWithBackdrop'
14-
import { BORDER_RADIUS } from '~/style/globalStyle'
16+
import { BORDER_RADIUS, VERTICAL_GAP } from '~/style/globalStyle'
17+
import { showToast, ToastDuration } from '~/utils/layout'
1518

1619
interface QRCodeScannerModalProps {
1720
onClose: () => void
@@ -25,22 +28,26 @@ let frames: FrameState
2528
const QRCodeScannerModal = ({ onClose, onQRCodeScan, qrCodeMode = 'simple', text }: QRCodeScannerModalProps) => {
2629
const theme = useTheme()
2730
const { t } = useTranslation()
31+
const [permission, requestPermission] = useCameraPermissions()
2832

29-
const [hasPermission, setHasPermission] = useState<boolean>()
3033
const [scanned, setScanned] = useState(false)
3134
const [progress, setProgress] = useState(0)
3235

33-
useEffect(() => {
34-
const getCameraPermissions = async () => {
35-
const { status } = await Camera.requestCameraPermissionsAsync()
36-
setHasPermission(status === 'granted')
37-
}
38-
39-
getCameraPermissions()
40-
}, [qrCodeMode])
41-
4236
const handleBarCodeScanned = ({ data }: BarcodeScanningResult) => {
4337
if (qrCodeMode === 'animated') {
38+
if (isValidAddress(data)) {
39+
onClose()
40+
showToast({
41+
text1: t('This is not the QR code you are looking for.'),
42+
text2: t(
43+
'To import from the desktop wallet, find the "Export current wallet" feature in the desktop wallet app settings.'
44+
),
45+
type: 'error',
46+
visibilityTime: ToastDuration.LONG
47+
})
48+
return
49+
}
50+
4451
try {
4552
frames = parseFramesReducer(frames, data)
4653

@@ -88,7 +95,7 @@ const QRCodeScannerModal = ({ onClose, onQRCodeScan, qrCodeMode = 'simple', text
8895
return (
8996
<ModalWithBackdrop visible closeModal={onClose} color={theme.bg.primary} showCloseButton animationType="fade">
9097
<ScreenStyled>
91-
{!scanned && hasPermission && (
98+
{!scanned && permission?.status === 'granted' && (
9299
<CameraStyled
93100
facing="back"
94101
onBarcodeScanned={handleBarCodeScanned}
@@ -98,10 +105,13 @@ const QRCodeScannerModal = ({ onClose, onQRCodeScan, qrCodeMode = 'simple', text
98105
</CameraStyled>
99106
)}
100107

101-
{hasPermission === false && (
108+
{permission?.granted === false && (
102109
<ScreenSection fill verticallyCentered>
103110
<InfoBox title={t('Camera permissions required')} Icon={CameraIcon}>
104111
<AppText>{t('Please, enable access to camera through the settings of your device.')}</AppText>
112+
{permission.canAskAgain && (
113+
<Button title={t('Allow')} onPress={requestPermission} style={{ marginTop: VERTICAL_GAP }} />
114+
)}
105115
</InfoBox>
106116
</ScreenSection>
107117
)}

0 commit comments

Comments
 (0)