-
Notifications
You must be signed in to change notification settings - Fork 3
/
Scanner.js
83 lines (71 loc) · 2.14 KB
/
Scanner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useState, useEffect } from 'react';
import { Button, Text, TextInput, View, StyleSheet } from 'react-native';
import { BarCodeScanner } from 'expo-barcode-scanner';
// https://github.com/expo/expo/issues/9619#issuecomment-1031499101
import { useIsFocused } from "@react-navigation/native";
import {ROUTE_NAME_HOME} from "./constants";
export default function Scanner({ navigation }) {
const [hasPermission, setHasPermission] = useState(null);
const [walletConnectUrl, setWalletConnectUrl] = useState(null);
const isFocused = useIsFocused();
useEffect(() => {
(async () => {
const { status } = await BarCodeScanner.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
const navigateToWalletScreen = (url) => {
if (walletConnectUrl) {
setWalletConnectUrl(null);
}
navigation.navigate({
name: ROUTE_NAME_HOME,
params: { scannedWalletConnectUrl: url },
merge: true
});
}
const handleBarCodeScanned = ({ type, data }) => {
navigateToWalletScreen(data);
};
const manualWalletConnect =
(<>
<TextInput style={styles.input} placeholder="Wallet Connect URL" value={walletConnectUrl} onChangeText={setWalletConnectUrl}/>
<Button title={'Connect'} disabled={walletConnectUrl ? false : true} onPress={() => navigateToWalletScreen(walletConnectUrl)}/>
</>);
if (hasPermission === null) {
return <Text>Requesting for camera permission</Text>;
}
if (hasPermission === false) {
return (
<>
<Text>No access to camera</Text>
{manualWalletConnect}
</>
);
}
return (
<View style={styles.container}>
<View style={styles.container}>
{isFocused && <BarCodeScanner
onBarCodeScanned={handleBarCodeScanned}
style={StyleSheet.absoluteFillObject}
/>
}
</View>
{manualWalletConnect}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
},
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
});