mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
new: bulk import servers (#301)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:computer/computer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_markdown/flutter_markdown.dart';
|
||||
import 'package:toolbox/core/extension/context/common.dart';
|
||||
import 'package:toolbox/core/extension/context/dialog.dart';
|
||||
import 'package:toolbox/core/extension/context/locale.dart';
|
||||
@@ -12,11 +14,15 @@ import 'package:toolbox/core/utils/sync/icloud.dart';
|
||||
import 'package:toolbox/core/utils/platform/base.dart';
|
||||
import 'package:toolbox/core/utils/share.dart';
|
||||
import 'package:toolbox/core/utils/sync/webdav.dart';
|
||||
import 'package:toolbox/core/utils/ui.dart';
|
||||
import 'package:toolbox/data/model/app/backup.dart';
|
||||
import 'package:toolbox/data/model/server/server_private_info.dart';
|
||||
import 'package:toolbox/data/res/color.dart';
|
||||
import 'package:toolbox/data/res/logger.dart';
|
||||
import 'package:toolbox/data/res/path.dart';
|
||||
import 'package:toolbox/data/res/store.dart';
|
||||
import 'package:toolbox/data/res/ui.dart';
|
||||
import 'package:toolbox/data/res/url.dart';
|
||||
import 'package:toolbox/view/widget/appbar.dart';
|
||||
import 'package:toolbox/view/widget/expand_tile.dart';
|
||||
import 'package:toolbox/view/widget/cardx.dart';
|
||||
@@ -48,6 +54,7 @@ class BackupPage extends StatelessWidget {
|
||||
_buildWebdav(context),
|
||||
_buildFile(context),
|
||||
_buildClipboard(context),
|
||||
_buildBulkImportServers(context),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -214,6 +221,26 @@ class BackupPage extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBulkImportServers(BuildContext context) {
|
||||
return CardX(
|
||||
child: ListTile(
|
||||
title: Text(l10n.bulkImportServers),
|
||||
subtitle: MarkdownBody(
|
||||
data: l10n.bulkImportServersTip(Urls.appWiki),
|
||||
styleSheet: MarkdownStyleSheet(
|
||||
p: UIs.textGrey,
|
||||
a: TextStyle(
|
||||
color: primaryColor,
|
||||
)),
|
||||
onTapLink: (text, href, title) {
|
||||
if (href != null) openUrl(href);
|
||||
},
|
||||
),
|
||||
trailing: const Icon(Icons.import_export),
|
||||
onTap: () => _onBulkImportServers(context),
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> _onTapFileRestore(BuildContext context) async {
|
||||
final path = await pickOneFile();
|
||||
if (path == null) return;
|
||||
@@ -419,4 +446,61 @@ class BackupPage extends StatelessWidget {
|
||||
context.showSnackBar(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
void _onBulkImportServers(BuildContext context) async {
|
||||
final path = await pickOneFile();
|
||||
if (path == null) return;
|
||||
|
||||
final file = File(path);
|
||||
if (!await file.exists()) {
|
||||
context.showRoundDialog(
|
||||
title: Text(l10n.error),
|
||||
child: Text(l10n.fileNotExist(path)),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final text = await file.readAsString();
|
||||
if (text.isEmpty) {
|
||||
context.showRoundDialog(
|
||||
title: Text(l10n.error),
|
||||
child: Text(l10n.fieldMustNotEmpty),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final spis = await context.showLoadingDialog(
|
||||
fn: () => Computer.shared.start((val) {
|
||||
final list = json.decode(val) as List;
|
||||
return list.map((e) => ServerPrivateInfo.fromJson(e)).toList();
|
||||
}, text.trim()),
|
||||
);
|
||||
final sure = await context.showRoundDialog<bool>(
|
||||
title: Text(l10n.import),
|
||||
child: Text(l10n.askContinue('${spis.length} ${l10n.server}')),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => context.pop(true),
|
||||
child: Text(l10n.ok),
|
||||
),
|
||||
],
|
||||
);
|
||||
if (sure == true) {
|
||||
await context.showLoadingDialog(
|
||||
fn: () async {
|
||||
for (var spi in spis) {
|
||||
Stores.server.put(spi);
|
||||
}
|
||||
},
|
||||
);
|
||||
context.showSnackBar(l10n.success);
|
||||
}
|
||||
} catch (e) {
|
||||
context.showRoundDialog(
|
||||
title: Text(l10n.error),
|
||||
child: Text(e.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user