Skip to content

Commit 284df8d

Browse files
-fix: fix a bug that modal can't present
1 parent 6664223 commit 284df8d

1 file changed

Lines changed: 36 additions & 37 deletions

File tree

  • BarcodeReaderSimpleSample

BarcodeReaderSimpleSample/App.js

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Button,
44
Modal,
55
PermissionsAndroid,
6+
Platform,
67
StyleSheet,
78
Text,
89
TouchableOpacity,
@@ -18,8 +19,6 @@ import {
1819
} from 'henry-capture-vision-react-native';
1920
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
2021

21-
let mdbr;
22-
2322
const option = {
2423
mediaType: 'photo',
2524
maxWidth: 2000,
@@ -29,9 +28,7 @@ const option = {
2928

3029
const requestPermissions = async () => {
3130
try {
32-
if (
33-
!(await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA))
34-
) {
31+
if (Platform.OS === 'android') {
3532
await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA);
3633
}
3734
} catch (err) {
@@ -41,7 +38,7 @@ const requestPermissions = async () => {
4138

4239
const decodeFile = async filePath => {
4340
try {
44-
return await mdbr.decodeFile(filePath);
41+
return await this.dbr.decodeFile(filePath);
4542
} catch (err) {
4643
return null;
4744
}
@@ -60,60 +57,62 @@ const mergeResultsText = (results: BarcodeResult[]) => {
6057
return str;
6158
};
6259

63-
const useImagePicker = (imagePickerLauncher, setModalVisible, setModalText) => {
64-
imagePickerLauncher(option).then(res => {
60+
const useImagePicker = (imagePickerLauncher, setModalState) => {
61+
imagePickerLauncher(option, res => {
6562
if (res.didCancel) {
66-
setModalVisible(false);
63+
setModalState(modalInitState);
6764
return false;
6865
}
66+
setModalState({isVisible: true, text: 'decoding...'});
6967
decodeFile(res.assets[0].uri.split('file://')[1]).then(results => {
7068
let str = mergeResultsText(results);
71-
setModalVisible(true);
72-
setModalText(str);
69+
setModalState({isVisible: true, text: str});
7370
});
7471
});
75-
setModalText('Decoding...');
76-
setModalVisible(true);
7772
};
7873

79-
function HomeScreen({navigation}) {
80-
(async () => {
81-
// Initialize the license so that you can use full feature of the Barcode Reader module.
82-
try {
83-
await DCVBarcodeReader.initLicense(
84-
'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9',
85-
);
74+
(async () => {
75+
// Initialize the license so that you can use full feature of the Barcode Reader module.
76+
try {
77+
await DCVBarcodeReader.initLicense(
78+
'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9',
79+
);
80+
} catch (e) {
81+
console.log(e);
82+
}
83+
this.dbr = await DCVBarcodeReader.createInstance();
84+
await this.dbr.updateRuntimeSettings(
85+
EnumDBRPresetTemplate.IMAGE_READ_RATE_FIRST,
86+
);
87+
})();
8688

87-
mdbr = await DCVBarcodeReader.createInstance();
88-
await mdbr.updateRuntimeSettings(
89-
EnumDBRPresetTemplate.IMAGE_READ_RATE_FIRST,
90-
);
91-
} catch (e) {
92-
console.log(e);
93-
}
94-
})();
89+
const modalInitState = {
90+
isVisible: false,
91+
text: '',
92+
};
9593

96-
const [modalVisible, setModalVisible] = useState(false);
97-
const [modalText, setModalText] = useState('');
94+
function HomeScreen({navigation}) {
95+
// const [modalVisible, setModalVisible] = useState(false);
96+
// const [modalText, setModalText] = useState('');
97+
const [modalState, setModalState] = useState(modalInitState);
9898

9999
return (
100100
<View style={styles.contentView}>
101101
<Modal
102102
animationType="slide"
103103
transparent={true}
104-
visible={modalVisible}
104+
visible={modalState.isVisible}
105105
onRequestClose={() => {
106-
setModalVisible(!modalVisible);
106+
setModalState(modalInitState);
107107
}}>
108108
<TouchableOpacity
109109
activeOpacity={1}
110110
onPress={() => {
111-
console.log('false');
112-
setModalVisible(false);
111+
setModalState(modalInitState);
113112
}}
114113
style={styles.centeredView}>
115114
<View style={styles.modalView}>
116-
<Text style={styles.modalText}>{modalText}</Text>
115+
<Text style={styles.modalText}>{modalState.text}</Text>
117116
</View>
118117
</TouchableOpacity>
119118
</Modal>
@@ -123,7 +122,7 @@ function HomeScreen({navigation}) {
123122
onPress={() => {
124123
requestPermissions().then(() => {
125124
// eslint-disable-next-line react-hooks/rules-of-hooks
126-
useImagePicker(launchCamera, setModalVisible, setModalText);
125+
useImagePicker(launchCamera, setModalState);
127126
});
128127
}}
129128
/>
@@ -132,7 +131,7 @@ function HomeScreen({navigation}) {
132131
title="Select Photo"
133132
onPress={() => {
134133
// eslint-disable-next-line react-hooks/rules-of-hooks
135-
useImagePicker(launchImageLibrary, setModalVisible, setModalText);
134+
useImagePicker(launchImageLibrary, setModalState);
136135
}}
137136
/>
138137
<View style={styles.splitView} />

0 commit comments

Comments
 (0)