new: clipboard backup (#263)

This commit is contained in:
lollipopkit
2024-02-01 11:57:18 +08:00
parent 3093230400
commit 3d5ce4b863
16 changed files with 127 additions and 28 deletions

View File

@@ -47,6 +47,7 @@ class BackupPage extends StatelessWidget {
if (isMacOS || isIOS) _buildIcloud(context),
_buildWebdav(context),
_buildFile(context),
_buildClipboard(context),
],
);
}
@@ -188,6 +189,31 @@ class BackupPage extends StatelessWidget {
);
}
Widget _buildClipboard(BuildContext context) {
return CardX(
child: ExpandTile(
leading: const Icon(Icons.content_paste),
title: Text(l10n.clipboard),
children: [
ListTile(
title: Text(l10n.backup),
trailing: const Icon(Icons.save),
onTap: () async {
final path = await Backup.backup();
Shares.copy(await File(path).readAsString());
context.showSnackBar(l10n.success);
},
),
ListTile(
trailing: const Icon(Icons.restore),
title: Text(l10n.restore),
onTap: () async => _onTapClipboardRestore(context),
),
],
),
);
}
Future<void> _onTapFileRestore(BuildContext context) async {
final path = await pickOneFile();
if (path == null) return;
@@ -353,4 +379,47 @@ class BackupPage extends StatelessWidget {
Webdav.changeClient(urlCtrl.text, userCtrl.text, pwdCtrl.text);
}
}
void _onTapClipboardRestore(BuildContext context) async {
final text = await Shares.paste();
if (text == null || text.isEmpty) {
context.showSnackBar(l10n.fieldMustNotEmpty);
return;
}
try {
context.showLoadingDialog();
final backup =
await Computer.shared.start(Backup.fromJsonString, text.trim());
if (backupFormatVersion != backup.version) {
context.showSnackBar(l10n.backupVersionNotMatch);
return;
}
await context.showRoundDialog(
title: Text(l10n.restore),
child: Text(l10n.askContinue(
'${l10n.restore} ${l10n.backup}(${backup.date})',
)),
actions: [
TextButton(
onPressed: () => context.pop(),
child: Text(l10n.cancel),
),
TextButton(
onPressed: () async {
await backup.restore(force: true);
context.pop();
},
child: Text(l10n.ok),
),
],
);
} catch (e, trace) {
Loggers.app.warning('Import backup failed', e, trace);
context.showSnackBar(e.toString());
} finally {
context.pop();
}
}
}