2019-09-21 19:37:37 +02:00
|
|
|
/* global alert */
|
2020-01-01 04:31:04 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { Image, View, TouchableOpacity, Platform } from 'react-native';
|
2019-05-02 22:33:03 +02:00
|
|
|
import { RNCamera } from 'react-native-camera';
|
2019-09-21 19:37:37 +02:00
|
|
|
import { Icon } from 'react-native-elements';
|
|
|
|
import ImagePicker from 'react-native-image-picker';
|
2020-01-01 04:31:04 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useNavigationParam, useNavigation } from 'react-navigation-hooks';
|
|
|
|
import DocumentPicker from 'react-native-document-picker';
|
|
|
|
import RNFS from 'react-native-fs';
|
2019-09-21 19:37:37 +02:00
|
|
|
const LocalQRCode = require('@remobile/react-native-qrcode-local-image');
|
2018-01-30 23:42:38 +01:00
|
|
|
|
2020-01-01 04:31:04 +01:00
|
|
|
const ScanQRCode = ({
|
|
|
|
onBarScanned = useNavigationParam('onBarScanned'),
|
|
|
|
cameraPreviewIsPaused = false,
|
|
|
|
showCloseButton = true,
|
|
|
|
showFileImportButton = useNavigationParam('showFileImportButton') || false,
|
|
|
|
launchedBy = useNavigationParam('launchedBy'),
|
|
|
|
}) => {
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const { navigate } = useNavigation();
|
|
|
|
|
|
|
|
const onBarCodeRead = ret => {
|
|
|
|
if (!isLoading && !cameraPreviewIsPaused) {
|
|
|
|
setIsLoading(true);
|
|
|
|
try {
|
|
|
|
if (showCloseButton && launchedBy) {
|
|
|
|
navigate(launchedBy);
|
|
|
|
}
|
|
|
|
if (ret.additionalProperties) {
|
|
|
|
onBarScanned(ret.data, ret.additionalProperties);
|
|
|
|
} else {
|
|
|
|
onBarScanned(ret.data);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setIsLoading(false);
|
2018-05-12 22:27:34 +02:00
|
|
|
};
|
|
|
|
|
2020-01-01 04:31:04 +01:00
|
|
|
const showFilePicker = async () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
try {
|
|
|
|
const res = await DocumentPicker.pick();
|
|
|
|
const file = await RNFS.readFile(res.uri);
|
|
|
|
const fileParsed = JSON.parse(file);
|
|
|
|
if (fileParsed.keystore.xpub) {
|
|
|
|
onBarCodeRead({ data: fileParsed.keystore.xpub, additionalProperties: { masterFingerprint: fileParsed.keystore.ckcc_xfp } });
|
|
|
|
} else {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (!DocumentPicker.isCancel(err)) {
|
|
|
|
alert('The selected file does not contain a wallet that can be imported.');
|
|
|
|
}
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
setIsLoading(false);
|
|
|
|
};
|
2018-01-30 23:42:38 +01:00
|
|
|
|
2020-01-01 04:31:04 +01:00
|
|
|
useEffect(() => {}, [cameraPreviewIsPaused]);
|
2018-01-30 23:42:38 +01:00
|
|
|
|
2020-01-01 04:31:04 +01:00
|
|
|
return (
|
|
|
|
<View style={{ flex: 1, backgroundColor: '#000000' }}>
|
|
|
|
{!cameraPreviewIsPaused && !isLoading && (
|
2019-05-02 22:33:03 +02:00
|
|
|
<RNCamera
|
|
|
|
captureAudio={false}
|
|
|
|
androidCameraPermissionOptions={{
|
|
|
|
title: 'Permission to use camera',
|
|
|
|
message: 'We need your permission to use your camera',
|
|
|
|
buttonPositive: 'OK',
|
|
|
|
buttonNegative: 'Cancel',
|
|
|
|
}}
|
2020-01-01 04:31:04 +01:00
|
|
|
style={{ flex: 1, justifyContent: 'space-between', backgroundColor: '#000000' }}
|
|
|
|
onBarCodeRead={onBarCodeRead}
|
2019-05-02 22:33:03 +02:00
|
|
|
barCodeTypes={[RNCamera.Constants.BarCodeType.qr]}
|
|
|
|
/>
|
2020-01-01 04:31:04 +01:00
|
|
|
)}
|
|
|
|
{showCloseButton && (
|
2019-05-02 22:33:03 +02:00
|
|
|
<TouchableOpacity
|
|
|
|
style={{
|
|
|
|
width: 40,
|
|
|
|
height: 40,
|
2019-09-17 19:46:23 +02:00
|
|
|
backgroundColor: 'rgba(0,0,0,0.4)',
|
2019-05-02 22:33:03 +02:00
|
|
|
justifyContent: 'center',
|
|
|
|
borderRadius: 20,
|
|
|
|
position: 'absolute',
|
2019-09-17 19:46:23 +02:00
|
|
|
right: 16,
|
2019-05-02 22:33:03 +02:00
|
|
|
top: 64,
|
|
|
|
}}
|
2020-01-01 04:31:04 +01:00
|
|
|
onPress={() => navigate(launchedBy)}
|
2019-05-02 22:33:03 +02:00
|
|
|
>
|
2019-09-17 19:46:23 +02:00
|
|
|
<Image style={{ alignSelf: 'center' }} source={require('../../img/close-white.png')} />
|
2019-05-02 22:33:03 +02:00
|
|
|
</TouchableOpacity>
|
2020-01-01 04:31:04 +01:00
|
|
|
)}
|
|
|
|
<TouchableOpacity
|
|
|
|
style={{
|
|
|
|
width: 40,
|
|
|
|
height: 40,
|
|
|
|
backgroundColor: '#FFFFFF',
|
|
|
|
justifyContent: 'center',
|
|
|
|
borderRadius: 20,
|
|
|
|
position: 'absolute',
|
|
|
|
left: 24,
|
|
|
|
bottom: 48,
|
|
|
|
}}
|
|
|
|
onPress={() => {
|
|
|
|
if (!isLoading) {
|
|
|
|
setIsLoading(true);
|
2019-09-21 19:37:37 +02:00
|
|
|
ImagePicker.launchImageLibrary(
|
|
|
|
{
|
|
|
|
title: null,
|
|
|
|
mediaType: 'photo',
|
|
|
|
takePhotoButtonTitle: null,
|
|
|
|
},
|
|
|
|
response => {
|
2019-09-22 01:39:42 +02:00
|
|
|
if (response.uri) {
|
2019-11-26 04:56:17 +01:00
|
|
|
const uri = Platform.OS === 'ios' ? response.uri.toString().replace('file://', '') : response.path.toString();
|
2019-09-22 01:39:42 +02:00
|
|
|
LocalQRCode.decode(uri, (error, result) => {
|
|
|
|
if (!error) {
|
2020-01-01 04:31:04 +01:00
|
|
|
onBarCodeRead({ data: result });
|
2019-09-22 01:39:42 +02:00
|
|
|
} else {
|
|
|
|
alert('The selected image does not contain a QR Code.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-01-01 04:31:04 +01:00
|
|
|
setIsLoading(false);
|
2019-09-21 19:37:37 +02:00
|
|
|
},
|
|
|
|
);
|
2020-01-01 04:31:04 +01:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="image" type="font-awesome" color="#0c2550" />
|
|
|
|
</TouchableOpacity>
|
|
|
|
{showFileImportButton && (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={{
|
|
|
|
width: 40,
|
|
|
|
height: 40,
|
|
|
|
backgroundColor: '#FFFFFF',
|
|
|
|
justifyContent: 'center',
|
|
|
|
borderRadius: 20,
|
|
|
|
position: 'absolute',
|
|
|
|
left: 96,
|
|
|
|
bottom: 48,
|
2019-09-21 19:37:37 +02:00
|
|
|
}}
|
2020-01-01 04:31:04 +01:00
|
|
|
onPress={showFilePicker}
|
2019-09-21 19:37:37 +02:00
|
|
|
>
|
2020-01-01 04:31:04 +01:00
|
|
|
<Icon name="file-import" type="material-community" color="#0c2550" />
|
2019-09-21 19:37:37 +02:00
|
|
|
</TouchableOpacity>
|
2020-01-01 04:31:04 +01:00
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
2018-03-18 03:48:23 +01:00
|
|
|
|
2020-01-01 04:31:04 +01:00
|
|
|
ScanQRCode.navigationOptions = {
|
|
|
|
header: null,
|
|
|
|
};
|
2019-09-21 19:37:37 +02:00
|
|
|
ScanQRCode.propTypes = {
|
2020-01-01 04:31:04 +01:00
|
|
|
launchedBy: PropTypes.string,
|
|
|
|
onBarScanned: PropTypes.func,
|
|
|
|
cameraPreviewIsPaused: PropTypes.bool,
|
|
|
|
showFileImportButton: PropTypes.bool,
|
|
|
|
showCloseButton: PropTypes.bool,
|
2018-03-18 03:48:23 +01:00
|
|
|
};
|
2020-01-01 04:31:04 +01:00
|
|
|
export default ScanQRCode;
|