REF: Reuse fs

This commit is contained in:
marcosrdz 2020-12-10 22:28:54 -05:00
parent 4ad7745832
commit 79d993ac6b
3 changed files with 59 additions and 103 deletions

View File

@ -5,6 +5,8 @@ import Share from 'react-native-share';
import loc from '../loc';
import DocumentPicker from 'react-native-document-picker';
import isCatalyst from 'react-native-is-catalyst';
import ImagePicker from 'react-native-image-picker';
import { presentCameraNotAuthorizedAlert } from './class/camera';
const LocalQRCode = require('@remobile/react-native-qrcode-local-image');
const writeFileAndExport = async function (filename, contents) {
@ -92,6 +94,56 @@ const _readPsbtFileIntoBase64 = async function (uri) {
}
};
const showImagePickerAndReadImage = () => {
return new Promise((resolve, reject) =>
ImagePicker.launchImageLibrary(
{
title: null,
mediaType: 'photo',
takePhotoButtonTitle: null,
},
response => {
if (response.uri) {
const uri = Platform.OS === 'ios' ? response.uri.toString().replace('file://', '') : response.path.toString();
LocalQRCode.decode(uri, (error, result) => {
if (!error) {
resolve(result);
} else {
reject(new Error(loc.send.qr_error_no_qrcode));
}
});
}
},
),
);
};
const takePhotoWithImagePickerAndReadPhoto = () => {
return new Promise((resolve, reject) =>
ImagePicker.launchCamera(
{
title: null,
mediaType: 'photo',
takePhotoButtonTitle: null,
},
response => {
if (response.uri) {
const uri = Platform.OS === 'ios' ? response.uri.toString().replace('file://', '') : response.path.toString();
LocalQRCode.decode(uri, (error, result) => {
if (!error) {
resolve(result);
} else {
reject(new Error(loc.send.qr_error_no_qrcode));
}
});
} else if (response.error) {
presentCameraNotAuthorizedAlert(response.error);
}
},
),
);
};
const showFilePickerAndReadFile = async function () {
try {
const res = await DocumentPicker.pick({
@ -141,3 +193,5 @@ const showFilePickerAndReadFile = async function () {
module.exports.writeFileAndExport = writeFileAndExport;
module.exports.openSignedTransaction = openSignedTransaction;
module.exports.showFilePickerAndReadFile = showFilePickerAndReadFile;
module.exports.showImagePickerAndReadImage = showImagePickerAndReadImage;
module.exports.takePhotoWithImagePickerAndReadPhoto = takePhotoWithImagePickerAndReadPhoto;

View File

@ -1,19 +1,16 @@
/* global alert */
import React, { useState } from 'react';
import { ActivityIndicator, Platform, ScrollView, StyleSheet, View } from 'react-native';
import { ActivityIndicator, ScrollView, StyleSheet, View } from 'react-native';
import { BlueNavigationStyle, BlueSpacing20, SafeBlueArea } from '../../BlueComponents';
import { DynamicQRCode } from '../../components/DynamicQRCode';
import { SquareButton } from '../../components/SquareButton';
import { getSystemName } from 'react-native-device-info';
import loc from '../../loc';
import ImagePicker from 'react-native-image-picker';
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
import { presentCameraNotAuthorizedAlert } from '../../class/camera';
import ActionSheet from '../ActionSheet';
const bitcoin = require('bitcoinjs-lib');
const fs = require('../../blue_modules/fs');
const LocalQRCode = require('@remobile/react-native-qrcode-local-image');
const isDesktop = getSystemName() === 'Mac OS X';
const PsbtMultisigQRCode = () => {
@ -50,60 +47,14 @@ const PsbtMultisigQRCode = () => {
}
};
const choosePhoto = () => {
ImagePicker.launchImageLibrary(
{
title: null,
mediaType: 'photo',
takePhotoButtonTitle: null,
},
response => {
if (response.uri) {
const uri = Platform.OS === 'ios' ? response.uri.toString().replace('file://', '') : response.path.toString();
LocalQRCode.decode(uri, (error, result) => {
if (!error) {
onBarScanned(result);
} else {
alert(loc.send.qr_error_no_qrcode);
}
});
}
},
);
};
const takePhoto = () => {
ImagePicker.launchCamera(
{
title: null,
mediaType: 'photo',
takePhotoButtonTitle: null,
},
response => {
if (response.uri) {
const uri = Platform.OS === 'ios' ? response.uri.toString().replace('file://', '') : response.path.toString();
LocalQRCode.decode(uri, (error, result) => {
if (!error) {
onBarScanned(result);
} else {
alert(loc.send.qr_error_no_qrcode);
}
});
} else if (response.error) {
presentCameraNotAuthorizedAlert(response.error);
}
},
);
};
const showActionSheet = () => {
const options = [loc._.cancel, loc.wallets.take_photo, loc.wallets.list_long_choose, loc.wallets.import_file];
ActionSheet.showActionSheetWithOptions({ options, cancelButtonIndex: 0 }, async buttonIndex => {
if (buttonIndex === 1) {
takePhoto();
fs.takePhotoWithImagePickerAndReadPhoto.then(onBarScanned);
} else if (buttonIndex === 2) {
choosePhoto();
fs.showImagePickerAndReadImage(onBarScanned).catch(error => alert(error.message));
} else if (buttonIndex === 3) {
const { data } = await fs.showFilePickerAndReadFile();
if (data) {

View File

@ -29,8 +29,6 @@ import { HDSegwitBech32Wallet, MultisigCosigner, MultisigHDWallet } from '../../
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
import loc from '../../loc';
import { getSystemName } from 'react-native-device-info';
import ImagePicker from 'react-native-image-picker';
import ScanQRCode from '../send/ScanQRCode';
import QRCode from 'react-native-qrcode-svg';
import { SquareButton } from '../../components/SquareButton';
import BottomModal from '../../components/BottomModal';
@ -49,7 +47,6 @@ const prompt = require('../../blue_modules/prompt');
const A = require('../../blue_modules/analytics');
const fs = require('../../blue_modules/fs');
const isDesktop = getSystemName() === 'Mac OS X';
const LocalQRCode = require('@remobile/react-native-qrcode-local-image');
const staticCache = {};
const WalletsAddMultisigStep2 = () => {
@ -407,60 +404,14 @@ const WalletsAddMultisigStep2 = () => {
}
};
const takePhoto = () => {
ImagePicker.launchCamera(
{
title: null,
mediaType: 'photo',
takePhotoButtonTitle: null,
},
response => {
if (response.uri) {
const uri = Platform.OS === 'ios' ? response.uri.toString().replace('file://', '') : response.path.toString();
LocalQRCode.decode(uri, (error, result) => {
if (!error) {
onBarScanned(result);
} else {
alert(loc.send.qr_error_no_qrcode);
}
});
} else if (response.error) {
ScanQRCode.presentCameraNotAuthorizedAlert(response.error);
}
},
);
};
const choosePhoto = () => {
ImagePicker.launchImageLibrary(
{
title: null,
mediaType: 'photo',
takePhotoButtonTitle: null,
},
response => {
if (response.uri) {
const uri = Platform.OS === 'ios' ? response.uri.toString().replace('file://', '') : response.path.toString();
LocalQRCode.decode(uri, (error, result) => {
if (!error) {
onBarScanned(result);
} else {
alert(loc.send.qr_error_no_qrcode);
}
});
}
},
);
};
const showActionSheet = () => {
const options = [loc._.cancel, loc.wallets.take_photo, loc.wallets.list_long_choose, loc.wallets.import_file];
ActionSheet.showActionSheetWithOptions({ options, cancelButtonIndex: 0 }, async buttonIndex => {
if (buttonIndex === 1) {
takePhoto();
fs.takePhotoWithImagePickerAndReadPhoto.then(onBarScanned);
} else if (buttonIndex === 2) {
choosePhoto();
fs.showImagePickerAndReadImage(onBarScanned).catch(error => alert(error.message));
} else if (buttonIndex === 3) {
const { data } = await fs.showFilePickerAndReadFile();
if (data) {