migrate: fl_lib

This commit is contained in:
lollipopkit🏳️‍⚧️
2024-08-01 13:44:32 +08:00
parent 969643d3df
commit 73248011a1
11 changed files with 119 additions and 123 deletions

View File

@@ -222,10 +222,10 @@ class BackupPage extends StatelessWidget {
if (text == null) return;
try {
final backup = await context.showLoadingDialog(
final (backup, err) = await context.showLoadingDialog(
fn: () => Computer.shared.start(Backup.fromJsonString, text.trim()),
);
if (backup == null) return;
if (err != null || backup == null) return;
if (backupFormatVersion != backup.version) {
context.showSnackBar(l10n.backupVersionNotMatch);
return;
@@ -364,11 +364,11 @@ class BackupPage extends StatelessWidget {
}
try {
final backup = await context.showLoadingDialog(
final (backup, err) = await context.showLoadingDialog(
fn: () => Computer.shared.start(Backup.fromJsonString, text.trim()),
);
if (err != null || backup == null) return;
if (backup == null) return;
if (backupFormatVersion != backup.version) {
context.showSnackBar(l10n.backupVersionNotMatch);
return;
@@ -404,13 +404,13 @@ class BackupPage extends StatelessWidget {
if (text == null) return;
try {
final spis = await context.showLoadingDialog(
final (spis, err) = 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()),
);
if (spis == null) return;
if (err != null || spis == null) return;
final sure = await context.showRoundDialog<bool>(
title: l10n.import,
child: Text(l10n.askContinue('${spis.length} ${l10n.server}')),
@@ -422,7 +422,7 @@ class BackupPage extends StatelessWidget {
],
);
if (sure == true) {
final suc = await context.showLoadingDialog(
final (suc, err) = await context.showLoadingDialog(
fn: () async {
for (var spi in spis) {
Stores.server.put(spi);
@@ -430,7 +430,7 @@ class BackupPage extends StatelessWidget {
return true;
},
);
if (suc != true) return;
if (err != null || suc != true) return;
context.showSnackBar(l10n.success);
}
} catch (e, s) {

View File

@@ -391,11 +391,15 @@ class _ContainerPageState extends State<ContainerPage> {
onPressed: () async {
context.pop();
final result = await context.showLoadingDialog(
final (result, err) = await context.showLoadingDialog(
fn: () => _container.run(cmd),
);
if (result != null) {
context.showSnackBar(result.message ?? l10n.unknownError);
if (err != null || result != null) {
final e = result?.message ?? err?.toString();
context.showRoundDialog(
title: l10n.error,
child: Text(e ?? l10n.unknownError),
);
}
},
child: Text(l10n.run),
@@ -506,13 +510,14 @@ class _ContainerPageState extends State<ContainerPage> {
onPressed: () async {
context.pop();
final result = await context.showLoadingDialog(
final (result, err) = await context.showLoadingDialog(
fn: () => _container.delete(id, force),
);
if (result != null) {
if (err != null || result != null) {
final e = result?.message ?? err?.toString();
context.showRoundDialog(
title: l10n.error,
child: Text(result.message ?? l10n.unknownError),
child: Text(e ?? l10n.unknownError),
);
}
},
@@ -522,35 +527,38 @@ class _ContainerPageState extends State<ContainerPage> {
);
break;
case ContainerMenu.start:
final result = await context.showLoadingDialog(
final (result, err) = await context.showLoadingDialog(
fn: () => _container.start(id),
);
if (result != null) {
if (err != null || result != null) {
final e = result?.message ?? err?.toString();
context.showRoundDialog(
title: l10n.error,
child: Text(result.message ?? l10n.unknownError),
child: Text(e ?? l10n.unknownError),
);
}
break;
case ContainerMenu.stop:
final result = await context.showLoadingDialog(
final (result, err) = await context.showLoadingDialog(
fn: () => _container.stop(id),
);
if (result != null) {
if (err != null || result != null) {
final e = result?.message ?? err?.toString();
context.showRoundDialog(
title: l10n.error,
child: Text(result.message ?? l10n.unknownError),
child: Text(e ?? l10n.unknownError),
);
}
break;
case ContainerMenu.restart:
final result = await context.showLoadingDialog(
final (result, err) = await context.showLoadingDialog(
fn: () => _container.restart(id),
);
if (result != null) {
if (err != null || result != null) {
final e = result?.message ?? err?.toString();
context.showRoundDialog(
title: l10n.error,
child: Text(result.message ?? l10n.unknownError),
child: Text(e ?? l10n.unknownError),
);
}
break;

View File

@@ -145,10 +145,13 @@ class _EditorPageState extends State<EditorPage> {
// If path is not null, then it's a file editor
// save the text and return true to pop the page
if (widget.path != null) {
final res = await context.showLoadingDialog(
final (res, _) = await context.showLoadingDialog(
fn: () => File(widget.path!).writeAsString(_controller.text),
);
if (res == null) return;
if (res == null) {
context.showSnackBar(l10n.failed);
return;
}
context.pop(true);
return;
}

View File

@@ -430,12 +430,13 @@ final class _PvePageState extends State<PvePage> {
);
if (sure != true) return;
final suc =
await context.showLoadingDialog(fn: () => func(item.node, item.id));
final (suc, err) = await context.showLoadingDialog(
fn: () => func(item.node, item.id),
);
if (suc == true) {
context.showSnackBar(l10n.success);
} else {
context.showSnackBar(l10n.failed);
context.showSnackBar(err?.toString() ?? l10n.failed);
}
}

View File

@@ -114,12 +114,12 @@ class _IOSSettingsPageState extends State<IOSSettingsPage> {
final result = await AppRoutes.kvEditor(data: urls).go(context);
if (result == null || result is! Map<String, String>) return;
final suc = await context.showLoadingDialog(fn: () async {
final (suc, err) = await context.showLoadingDialog(fn: () async {
await wc.updateApplicationContext({'urls': result});
return true;
});
if (suc == true) {
context.showSnackBar(l10n.success);
context.showSnackBar(err?.toString() ?? l10n.success);
}
}
}

View File

@@ -471,8 +471,10 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
SftpReqType.download,
);
Pros.sftp.add(req, completer: completer);
final suc = await context.showLoadingDialog(fn: () => completer.future);
if (suc == null) return;
final (suc, err) = await context.showLoadingDialog(
fn: () => completer.future,
);
if (suc == null || err != null) return;
final result = await AppRoutes.editor(path: localPath).go<bool>(context);
if (result != null && result) {
@@ -564,7 +566,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
onPressed: () async {
context.pop();
final suc = await context.showLoadingDialog(
final (suc, err) = await context.showLoadingDialog(
fn: () async {
final remotePath = _getRemotePath(file);
if (useRmr) {
@@ -577,7 +579,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
return true;
},
);
if (suc == null) return;
if (suc == null || err != null) return;
_listDir();
},
@@ -606,14 +608,14 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
}
context.pop();
final suc = await context.showLoadingDialog(
final (suc, err) = await context.showLoadingDialog(
fn: () async {
final dir = '${_status.path!.path}/${textController.text}';
await _status.client!.mkdir(dir);
return true;
},
);
if (suc == null) return;
if (suc == null || err != null) return;
_listDir();
}
@@ -661,14 +663,14 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
}
context.pop();
final suc = await context.showLoadingDialog(
final (suc, err) = await context.showLoadingDialog(
fn: () async {
final path = '${_status.path!.path}/${textController.text}';
await _client!.run('touch "$path"');
return true;
},
);
if (suc == null) return;
if (suc == null || err != null) return;
_listDir();
}
@@ -712,14 +714,14 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
}
context.pop();
final suc = await context.showLoadingDialog(
final (suc, err) = await context.showLoadingDialog(
fn: () async {
final newName = textController.text;
await _status.client?.rename(file.filename, newName);
return true;
},
);
if (suc == null) return;
if (suc == null || err != null) return;
_listDir();
}
@@ -767,10 +769,10 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
);
if (confirm != true) return;
final suc = await context.showLoadingDialog(
final (suc, err) = await context.showLoadingDialog(
fn: () => _client?.run(cmd) ?? Future.value(false),
);
if (suc == null) return;
if (suc == null || err != null) return;
_listDir();
}
@@ -786,65 +788,47 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
/// Only return true if the path is changed
Future<bool?> _listDir() async {
return context.showLoadingDialog(
final (ret, err) = await context.showLoadingDialog(
fn: () async {
_status.client ??= await _client?.sftp();
try {
final listPath = _status.path?.path ?? '/';
final fs = await _status.client?.listdir(listPath);
if (fs == null) {
return false;
}
fs.sort((a, b) => a.filename.compareTo(b.filename));
/// Issue #97
/// In order to compatible with the Synology NAS
/// which not has '.' and '..' in listdir
if (fs.isNotEmpty && fs.firstOrNull?.filename == '.') {
fs.removeAt(0);
}
/// Issue #96
/// Due to [WillPopScope] added in this page
/// There is no need to keep '..' folder in listdir
/// So remove it
if (fs.isNotEmpty && fs.firstOrNull?.filename == '..') {
fs.removeAt(0);
}
if (mounted) {
setState(() {
_status.files = fs;
});
// Only update history when success
if (Stores.setting.sftpOpenLastPath.fetch()) {
Stores.history.sftpLastPath.put(widget.spi.id, listPath);
}
return true;
}
return false;
} catch (e, trace) {
Loggers.app.warning('List dir failed', e, trace);
await _backward();
Future.delayed(
const Duration(milliseconds: 177),
() => context.showRoundDialog(
title: l10n.error,
child: Text(e.toString()),
actions: [
TextButton(
onPressed: () => context.pop(),
child: Text(l10n.ok),
)
],
),
);
final listPath = _status.path?.path ?? '/';
final fs = await _status.client?.listdir(listPath);
if (fs == null) {
return false;
}
fs.sort((a, b) => a.filename.compareTo(b.filename));
/// Issue #97
/// In order to compatible with the Synology NAS
/// which not has '.' and '..' in listdir
if (fs.isNotEmpty && fs.firstOrNull?.filename == '.') {
fs.removeAt(0);
}
/// Issue #96
/// Due to [WillPopScope] added in this page
/// There is no need to keep '..' folder in listdir
/// So remove it
if (fs.isNotEmpty && fs.firstOrNull?.filename == '..') {
fs.removeAt(0);
}
if (mounted) {
setState(() {
_status.files = fs;
});
// Only update history when success
if (Stores.setting.sftpOpenLastPath.fetch()) {
Stores.history.sftpLastPath.put(widget.spi.id, listPath);
}
return true;
}
return false;
},
barrierDismiss: true,
);
return ret ?? err == null;
}
Future<void> _backward() async {