ADD: Offer additional file save destinations on Android #3356

This commit is contained in:
Marcos Rodriguez Vélez 2021-07-09 22:38:09 -04:00
parent 5a33c90e3f
commit 7b2f27b1e2
3 changed files with 69 additions and 81 deletions

View file

@ -11,6 +11,38 @@ import ActionSheet from '../screen/ActionSheet';
import BlueClipboard from './clipboard'; import BlueClipboard from './clipboard';
const LocalQRCode = require('@remobile/react-native-qrcode-local-image'); 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) { const writeFileAndExport = async function (filename, contents) {
if (Platform.OS === 'ios') { if (Platform.OS === 'ios') {
const filePath = RNFS.TemporaryDirectoryPath + `/${filename}`; const filePath = RNFS.TemporaryDirectoryPath + `/${filename}`;
@ -26,37 +58,37 @@ const writeFileAndExport = async function (filename, contents) {
RNFS.unlink(filePath); RNFS.unlink(filePath);
}); });
} else if (Platform.OS === 'android') { } else if (Platform.OS === 'android') {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { Alert.alert(
title: loc.send.permission_storage_title, loc._.file_save_title,
message: loc.send.permission_storage_message,
buttonNeutral: loc.send.permission_storage_later,
buttonNegative: loc._.cancel,
buttonPositive: loc._.ok,
});
if (granted === PermissionsAndroid.RESULTS.GRANTED) { loc.formatString(loc._.file_save_location, { filePath: filename }),
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',
},
{ text: loc._.cancel, onPress: () => {}, style: 'cancel' }, { 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 },
);
} }
}; };

View file

@ -17,7 +17,11 @@
"success": "Success", "success": "Success",
"wallet_key": "Wallet key", "wallet_key": "Wallet key",
"invalid_animated_qr_code_fragment" : "Invalid animated QRCode fragment. Please try again.", "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": "Discard changes?",
"discard_changes_detail": "You have unsaved changes. Are you sure to discard them and leave the screen?" "discard_changes_detail": "You have unsaved changes. Are you sure to discard them and leave the screen?"
}, },

View file

@ -8,18 +8,15 @@ import {
TextInput, TextInput,
Linking, Linking,
Platform, Platform,
PermissionsAndroid,
Text, Text,
StyleSheet, StyleSheet,
Alert,
findNodeHandle, findNodeHandle,
} from 'react-native'; } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard'; import Clipboard from '@react-native-clipboard/clipboard';
import Share from 'react-native-share';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback'; import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import DocumentPicker from 'react-native-document-picker'; import DocumentPicker from 'react-native-document-picker';
import { useNavigation, useRoute, useTheme } from '@react-navigation/native'; 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 RNFS from 'react-native-fs';
import Biometric from '../../class/biometrics'; import Biometric from '../../class/biometrics';
@ -181,54 +178,9 @@ const PsbtWithHardwareWallet = () => {
); );
}; };
const exportPSBT = async () => { const exportPSBT = () => {
const fileName = `${Date.now()}.psbt`; const fileName = `${Date.now()}.psbt`;
if (Platform.OS === 'ios') { fs.writeFileAndExport(fileName, typeof psbt === 'string' ? psbt : psbt.toBase64());
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' },
]);
}
}
}; };
const openSignedTransaction = async () => { const openSignedTransaction = async () => {