From 7b2f27b1e2124c3d37373ffee24d60f40a1f5b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Rodriguez=20V=C3=A9lez?= Date: Fri, 9 Jul 2021 22:38:09 -0400 Subject: [PATCH] ADD: Offer additional file save destinations on Android #3356 --- blue_modules/fs.js | 90 ++++++++++++++++++--------- loc/en.json | 6 +- screen/send/psbtWithHardwareWallet.js | 54 +--------------- 3 files changed, 69 insertions(+), 81 deletions(-) diff --git a/blue_modules/fs.js b/blue_modules/fs.js index 41d12f14d..5eee3168e 100644 --- a/blue_modules/fs.js +++ b/blue_modules/fs.js @@ -11,6 +11,38 @@ import ActionSheet from '../screen/ActionSheet'; import BlueClipboard from './clipboard'; const LocalQRCode = require('@remobile/react-native-qrcode-local-image'); +const writeFileAndExportToAndroidDestionation = async ({ filename, contents, destinationLocalizedString, destination }) => { + const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { + title: loc.send.permission_storage_title, + message: loc.send.permission_storage_message, + buttonNeutral: loc.send.permission_storage_later, + buttonNegative: loc._.cancel, + buttonPositive: loc._.ok, + }); + if (granted === PermissionsAndroid.RESULTS.GRANTED) { + const filePath = destination + `/${filename}`; + try { + await RNFS.writeFile(filePath, contents); + alert(loc.formatString(loc._.file_saved, { filePath: filename, destination: destinationLocalizedString })); + } catch (e) { + console.log(e); + alert(e.message); + } + } else { + console.log('Storage Permission: Denied'); + Alert.alert(loc.send.permission_storage_title, loc.send.permission_storage_denied_message, [ + { + text: loc.send.open_settings, + onPress: () => { + Linking.openSettings(); + }, + style: 'default', + }, + { text: loc._.cancel, onPress: () => {}, style: 'cancel' }, + ]); + } +}; + const writeFileAndExport = async function (filename, contents) { if (Platform.OS === 'ios') { const filePath = RNFS.TemporaryDirectoryPath + `/${filename}`; @@ -26,37 +58,37 @@ const writeFileAndExport = async function (filename, contents) { RNFS.unlink(filePath); }); } else if (Platform.OS === 'android') { - const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { - title: loc.send.permission_storage_title, - message: loc.send.permission_storage_message, - buttonNeutral: loc.send.permission_storage_later, - buttonNegative: loc._.cancel, - buttonPositive: loc._.ok, - }); + Alert.alert( + loc._.file_save_title, - if (granted === PermissionsAndroid.RESULTS.GRANTED) { - console.log('Storage Permission: Granted'); - const filePath = RNFS.DownloadDirectoryPath + `/${filename}`; - try { - await RNFS.writeFile(filePath, contents); - alert(loc.formatString(loc._.file_saved, { filePath: filename })); - } catch (e) { - console.log(e); - alert(e.message); - } - } else { - console.log('Storage Permission: Denied'); - Alert.alert(loc.send.permission_storage_title, loc.send.permission_storage_denied_message, [ - { - text: loc.send.open_settings, - onPress: () => { - Linking.openSettings(); - }, - style: 'default', - }, + loc.formatString(loc._.file_save_location, { filePath: filename }), + [ { text: loc._.cancel, onPress: () => {}, style: 'cancel' }, - ]); - } + { + text: loc._.downloads_folder, + onPress: () => { + writeFileAndExportToAndroidDestionation({ + filename, + contents, + destinationLocalizedString: loc._.downloads_folder, + destination: RNFS.DownloadDirectoryPath, + }); + }, + }, + { + text: loc._.external_storage, + onPress: async () => { + writeFileAndExportToAndroidDestionation({ + filename, + contents, + destination: RNFS.ExternalStorageDirectoryPath, + destinationLocalizedString: loc._.external_storage, + }); + }, + }, + ], + { cancelable: true }, + ); } }; diff --git a/loc/en.json b/loc/en.json index 45b26207c..2a9adcb9e 100644 --- a/loc/en.json +++ b/loc/en.json @@ -17,7 +17,11 @@ "success": "Success", "wallet_key": "Wallet key", "invalid_animated_qr_code_fragment" : "Invalid animated QRCode fragment. Please try again.", - "file_saved": "File ({filePath}) has been saved in your Downloads folder.", + "file_saved": "File {filePath} has been saved in your {destination}.", + "file_save_title": "Save File", + "file_save_location": "Select where to save {filePath}", + "downloads_folder": "Downloads Folder", + "external_storage": "External Storage", "discard_changes": "Discard changes?", "discard_changes_detail": "You have unsaved changes. Are you sure to discard them and leave the screen?" }, diff --git a/screen/send/psbtWithHardwareWallet.js b/screen/send/psbtWithHardwareWallet.js index e1b454da5..fc0597dca 100644 --- a/screen/send/psbtWithHardwareWallet.js +++ b/screen/send/psbtWithHardwareWallet.js @@ -8,18 +8,15 @@ import { TextInput, Linking, Platform, - PermissionsAndroid, Text, StyleSheet, - Alert, findNodeHandle, } from 'react-native'; import Clipboard from '@react-native-clipboard/clipboard'; -import Share from 'react-native-share'; import ReactNativeHapticFeedback from 'react-native-haptic-feedback'; import DocumentPicker from 'react-native-document-picker'; import { useNavigation, useRoute, useTheme } from '@react-navigation/native'; -import { isDesktop, isMacCatalina } from '../../blue_modules/environment'; +import { isMacCatalina } from '../../blue_modules/environment'; import RNFS from 'react-native-fs'; import Biometric from '../../class/biometrics'; @@ -181,54 +178,9 @@ const PsbtWithHardwareWallet = () => { ); }; - const exportPSBT = async () => { + const exportPSBT = () => { const fileName = `${Date.now()}.psbt`; - if (Platform.OS === 'ios') { - const filePath = RNFS.TemporaryDirectoryPath + `/${fileName}`; - await RNFS.writeFile(filePath, typeof psbt === 'string' ? psbt : psbt.toBase64()); - Share.open({ - url: 'file://' + filePath, - saveToFiles: isDesktop, - }) - .catch(error => { - console.log(error); - }) - .finally(() => { - RNFS.unlink(filePath); - }); - } else if (Platform.OS === 'android') { - const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { - title: loc.send.permission_storage_title, - message: loc.send.permission_storage_message, - buttonNeutral: loc.send.permission_storage_later, - buttonNegative: loc._.cancel, - buttonPositive: loc._.ok, - }); - - if (granted === PermissionsAndroid.RESULTS.GRANTED) { - console.log('Storage Permission: Granted'); - const filePath = RNFS.DownloadDirectoryPath + `/${fileName}`; - try { - await RNFS.writeFile(filePath, typeof psbt === 'string' ? psbt : psbt.toBase64()); - alert(loc.formatString(loc.send.txSaved, { filePath: fileName })); - } catch (e) { - console.log(e); - alert(e.message); - } - } else { - console.log('Storage Permission: Denied'); - Alert.alert(loc.send.permission_storage_title, loc.send.permission_storage_denied_message, [ - { - text: loc.send.open_settings, - onPress: () => { - Linking.openSettings(); - }, - style: 'default', - }, - { text: loc._.cancel, onPress: () => {}, style: 'cancel' }, - ]); - } - } + fs.writeFileAndExport(fileName, typeof psbt === 'string' ? psbt : psbt.toBase64()); }; const openSignedTransaction = async () => {