diff --git a/lib/core/utils/misc.dart b/lib/core/utils/misc.dart index 3d4fe6e5..ccbb8f57 100644 --- a/lib/core/utils/misc.dart +++ b/lib/core/utils/misc.dart @@ -24,11 +24,16 @@ Future shareFiles(BuildContext context, List filePaths) async { } else { text = '${filePaths.length} ${S.of(context)!.files}'; } - final xfiles = filePaths.map((e) => XFile(e)).toList(); - await Share.shareXFiles(xfiles, text: 'ServerBox -> $text'); + // ignore: deprecated_member_use + await Share.shareFiles(filePaths, text: 'ServerBox -> $text'); return filePaths.isNotEmpty; } +Future shareText(String text) async { + final result = await Share.shareWithResult(text, subject: 'ServerBox backup'); + return result.status == ShareResultStatus.success; +} + void copy(String text) { Clipboard.setData(ClipboardData(text: text)); } diff --git a/lib/view/page/backup.dart b/lib/view/page/backup.dart index 5d28c729..fb253e54 100644 --- a/lib/view/page/backup.dart +++ b/lib/view/page/backup.dart @@ -1,13 +1,14 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; -import 'package:toolbox/core/utils/misc.dart'; +import 'package:toolbox/data/res/path.dart'; import '../../core/extension/colorx.dart'; +import '../../core/utils/misc.dart'; import '../../core/utils/ui.dart'; import '../../data/model/app/backup.dart'; import '../../data/res/color.dart'; @@ -51,8 +52,20 @@ class BackupPage extends StatelessWidget { const SizedBox( height: 107, ), - _buildCard(s.restore, Icons.download, media, - () => _showImportDialog(context, s)), + _buildCard(s.restore, Icons.download, media, () async { + final path = await pickOneFile(); + if (path == null) { + showSnackBar(context, Text(s.notSelected)); + return; + } + final file = File(path); + if (!file.existsSync()) { + showSnackBar(context, Text(s.fileNotExist(path))); + return; + } + final text = await file.readAsString(); + _import(text, context, s); + }), const SizedBox(height: 7), const Divider(), const SizedBox(height: 7), @@ -60,7 +73,23 @@ class BackupPage extends StatelessWidget { s.backup, Icons.file_upload, media, - () => _showExportDialog(context, s), + () async { + final result = _diyEncrtpt( + json.encode( + Backup( + backupFormatVersion, + DateTime.now().toString().split('.').first, + _server.fetch(), + _snippet.fetch(), + _privateKey.fetch(), + _dockerHosts.fetch(), + ), + ), + ); + final path = '${(await docDir).path}/srvbox_bak.json'; + await File(path).writeAsString(result); + await shareFiles(context, [path]); + }, ) ], )), @@ -94,68 +123,6 @@ class BackupPage extends StatelessWidget { ); } - Future _showExportDialog(BuildContext context, S s) async { - final exportFieldController = TextEditingController() - ..text = _diyEncrtpt( - json.encode( - Backup( - backupFormatVersion, - DateTime.now().toString().split('.').first, - _server.fetch(), - _snippet.fetch(), - _privateKey.fetch(), - _dockerHosts.fetch(), - ), - ), - ); - await showRoundDialog( - context, - s.export, - TextField( - decoration: const InputDecoration( - labelText: 'JSON', - ), - maxLines: 7, - controller: exportFieldController, - ), - [ - TextButton( - child: Text(s.copy), - onPressed: () { - Clipboard.setData(ClipboardData(text: exportFieldController.text)); - Navigator.pop(context); - }, - ), - ], - ); - } - - Future _showImportDialog(BuildContext context, S s) async { - final importFieldController = TextEditingController(); - await showRoundDialog( - context, - s.import, - TextField( - decoration: const InputDecoration( - labelText: 'JSON', - ), - maxLines: 3, - controller: importFieldController, - ), - [ - TextButton( - onPressed: () => Navigator.of(context).pop(), - child: Text(s.cancel), - ), - TextButton( - onPressed: () async => - await _import(importFieldController.text.trim(), context, s), - child: const Text('GO'), - ) - ], - ); - } - Future _import(String text, BuildContext context, S s) async { if (text.isEmpty) { showSnackBar(context, Text(s.fieldMustNotEmpty)); diff --git a/pubspec.lock b/pubspec.lock index 4d7a7c31..39c50844 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -746,10 +746,10 @@ packages: dependency: "direct main" description: name: share_plus - sha256: "8c6892037b1824e2d7e8f59d54b3105932899008642e6372e5079c6939b4b625" + sha256: "692261968a494e47323dcc8bc66d8d52e81bc27cb4b808e4e8d7e8079d4cc01a" url: "https://pub.dev" source: hosted - version: "6.3.1" + version: "6.3.2" share_plus_platform_interface: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 829f7fe6..1b659974 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -52,7 +52,7 @@ dependencies: r_upgrade: 0.3.8+2 path_provider: ^2.0.9 easy_isolate: ^1.3.0 - share_plus: ^6.3.1 + share_plus: ^6.3.2 intl: ^0.17.0 share_plus_web: ^3.1.0 # xterm: ^3.4.1