onBackup method
Implementation
onBackup(BuildContext context) async {
try {
var aesEncryptedKeys = await BackUpKeyService.getEncryptedKeys(atsign);
if (aesEncryptedKeys.isEmpty) {
return false;
}
String tempFilePath = await _generateFile(aesEncryptedKeys);
if (Platform.isAndroid && context.mounted) {
if (Navigator.of(context).canPop()) {
Navigator.of(context).pop();
}
await showDialog(
context: context,
useRootNavigator: false,
builder: (context) {
return AlertDialog(
contentPadding: EdgeInsets.zero,
content: Container(
color: Colors.white,
width: double.infinity,
margin: const EdgeInsets.symmetric(horizontal: 32),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
InkWell(
onTap: () async {
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
String? dir = await getDownloadPath();
if (dir != null) {
String newPath = "$dir/$atsign${Strings.backupKeyName}";
debugPrint(newPath);
try {
if (await File(newPath).exists()) {
if (context.mounted) {
Navigator.of(context).pop();
showSnackBar(context: context, content: "File exists!");
}
} else {
final encryptedKeysFile = await File(newPath).create();
var keyString = jsonEncode(aesEncryptedKeys);
encryptedKeysFile.writeAsStringSync(keyString);
if (context.mounted) {
Navigator.of(context).pop();
showSnackBar(
context: context,
content: 'File saved successfully',
);
}
}
} catch (e) {
debugPrint("$e");
}
}
},
child: Container(
height: 52,
width: double.infinity,
alignment: Alignment.centerLeft,
child: const Text("Download"),
),
),
const Divider(height: 1),
InkWell(
onTap: () {
Navigator.of(context).pop();
shareFile(
context: context,
path: tempFilePath,
);
},
child: Container(
height: 52,
width: double.infinity,
alignment: Alignment.centerLeft,
child: const Text("Share"),
),
),
],
),
),
);
},
);
} else if (Platform.isIOS) {
if (context.mounted) {
var size = MediaQuery.of(context).size;
await Share.shareXFiles(
[XFile(tempFilePath)],
sharePositionOrigin: Rect.fromLTWH(0, 0, size.width, size.height / 2),
).then((ShareResult shareResult) {
if (shareResult.status == ShareResultStatus.success && context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('File saved successfully')));
}
});
}
} else {
final path = await getSaveLocation(suggestedName: '$atsign${Strings.backupKeyName}');
if (path == null) return;
final file = XFile(tempFilePath);
await file.saveTo(path.path);
if (context.mounted) {
showSnackBar(
context: context,
content: 'File saved successfully',
);
}
}
} on Exception catch (ex) {
_logger.severe('BackingUp keys throws $ex exception');
} on Error catch (err) {
_logger.severe('BackingUp keys throws $err error');
}
}