Passed
Push — dev ( 960b6d...f9a638 )
by Kasper
01:04 queued 12s
created

QrScanner.tsx ➔ QrScanner   B

Complexity

Conditions 4

Size

Total Lines 60
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 60
rs 8.6363
c 0
b 0
f 0
cc 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import React, { useState, useEffect } from 'react';
2
import { Text, View, StyleSheet, Button, Modal, Pressable } from 'react-native';
3
import { BarCodeScanner } from 'expo-barcode-scanner';
4
import Icon from 'react-native-vector-icons/Octicons';
5
6
export default function QrScanner({navigation, cameraVisible, setCameraVisible}) {
7
  const [hasPermission, setHasPermission] = useState(null);
8
  const [scanned, setScanned] = useState(false);
9
10
  useEffect(() => {
11
    const getBarCodeScannerPermissions = async () => {
12
      const { status } = await BarCodeScanner.requestPermissionsAsync();
13
      setHasPermission(status === 'granted');
14
    };
15
16
    getBarCodeScannerPermissions();
17
  }, []);
18
19
  const handleBarCodeScanned = ({ type, data }) => {
20
    setScanned(true);
21
    alert(`Bar code with type ${type} and data ${data} has been scanned!`);
22
  };
23
24
  if (hasPermission === null) {
25
    return <Text>Requesting for camera permission</Text>;
26
  }
27
  if (hasPermission === false) {
28
    return <Text>No access to camera</Text>;
29
  }
30
31
  return (
32
    <Modal
33
        animationType="slide"
34
        transparent={true}
35
        visible={cameraVisible}
36
        onRequestClose={() => {
37
        setCameraVisible(!cameraVisible);
38
        }}
39
    >
40
        <View style={[styles.container, styles.shadowProp]}>
41
            <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => setCameraVisible(!cameraVisible)}>
42
                <Icon 
43
                    name='x' 
44
                    size={25} 
45
                    color='black'
46
                />
47
            </Pressable>
48
            
49
            <Text style={styles.title}> Scan the QR-code </Text>
50
51
        <BarCodeScanner
52
            onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
53
            style={styles.viewFinder}
54
        />
55
56
        <Pressable style={[styles.cameraButton, styles.shadowProp]} onPress={() => setScanned(false)} >
57
            <Icon 
58
                name='screen-full' 
59
                size={24} 
60
                color='black'
61
            />
62
        </Pressable>
63
        </View>
64
    </Modal>
65
66
  );
67
}
68
69
const styles = StyleSheet.create({
70
    container: {
71
    //   flex: 1,
72
      width: '100%',
73
      alignItems: 'center',
74
      backgroundColor: 'white',
75
      height: '80%',
76
      top: '20%',
77
      borderTopRightRadius: 25,
78
      borderTopLeftRadius: 25
79
80
    },
81
82
    viewFinder: {
83
        flex: 1,
84
        width: '70%',
85
    },
86
87
88
    shadowProp: {
89
        elevation: 5,
90
        shadowColor: 'black'
91
    },
92
    
93
    backButton: {
94
        position: 'absolute',
95
        width: 40,
96
        height: 40, 
97
        left: 20,
98
        backgroundColor: 'white',
99
        top: 20,
100
        borderRadius: 25,
101
        borderWidth: 1,
102
        borderColor: 'gray',
103
        alignItems: 'center',
104
        justifyContent: 'center'
105
    },
106
107
    cameraButton: {
108
        // position: 'absolute',
109
        width: 60,
110
        height: 60, 
111
        // left: 20,
112
        backgroundColor: 'white',
113
        bottom: 50,
114
        borderRadius: 50,
115
        borderWidth: 1,
116
        borderColor: 'gray',
117
        alignItems: 'center',
118
        justifyContent: 'center'
119
    },
120
121
    title: {
122
        position: 'absolute',
123
        fontWeight: 'bold',
124
        fontSize: 20,
125
        marginTop: 30
126
    },
127
})