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,21 +11,7 @@ import ActionSheet from '../screen/ActionSheet';
import BlueClipboard from './clipboard';
const LocalQRCode = require('@remobile/react-native-qrcode-local-image');
const writeFileAndExport = async function (filename, contents) {
if (Platform.OS === 'ios') {
const filePath = RNFS.TemporaryDirectoryPath + `/${filename}`;
await RNFS.writeFile(filePath, contents);
Share.open({
url: 'file://' + filePath,
saveToFiles: isDesktop,
})
.catch(error => {
console.log(error);
})
.finally(() => {
RNFS.unlink(filePath);
});
} else if (Platform.OS === 'android') {
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,
@ -33,13 +19,11 @@ const writeFileAndExport = async function (filename, contents) {
buttonNegative: loc._.cancel,
buttonPositive: loc._.ok,
});
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Storage Permission: Granted');
const filePath = RNFS.DownloadDirectoryPath + `/${filename}`;
const filePath = destination + `/${filename}`;
try {
await RNFS.writeFile(filePath, contents);
alert(loc.formatString(loc._.file_saved, { filePath: filename }));
alert(loc.formatString(loc._.file_saved, { filePath: filename, destination: destinationLocalizedString }));
} catch (e) {
console.log(e);
alert(e.message);
@ -57,6 +41,54 @@ const writeFileAndExport = async function (filename, contents) {
{ text: loc._.cancel, onPress: () => {}, style: 'cancel' },
]);
}
};
const writeFileAndExport = async function (filename, contents) {
if (Platform.OS === 'ios') {
const filePath = RNFS.TemporaryDirectoryPath + `/${filename}`;
await RNFS.writeFile(filePath, contents);
Share.open({
url: 'file://' + filePath,
saveToFiles: isDesktop,
})
.catch(error => {
console.log(error);
})
.finally(() => {
RNFS.unlink(filePath);
});
} else if (Platform.OS === 'android') {
Alert.alert(
loc._.file_save_title,
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 },
);
}
};

View file

@ -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?"
},

View file

@ -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 () => {