This commit is contained in:
lollipopkit
2023-09-23 10:54:42 +08:00
parent f2981c5b15
commit 5a9fd74470
22 changed files with 117 additions and 136 deletions

View File

@@ -64,7 +64,7 @@ class BackupPage extends StatelessWidget {
Icons.save,
() async {
await Backup.backup();
await shareFiles(context, [await Paths.bak]);
await shareFiles([await Paths.bak]);
},
)
],

View File

@@ -44,7 +44,7 @@ class _DockerManagePageState extends State<DockerManagePage> {
@override
void initState() {
super.initState();
final client = widget.spi.findServer?.client;
final client = widget.spi.server?.client;
if (client == null) {
return;
}

View File

@@ -136,10 +136,9 @@ class _FullScreenPageState extends State<FullScreenPage> with AfterLayoutMixin {
}
return PageView.builder(
controller: _pageController,
itemCount: pro.servers.length,
itemCount: pro.serverOrder.length,
itemBuilder: (_, idx) {
final id = pro.serverOrder[idx];
final s = pro.servers[id];
final s = pro.pick(id: pro.serverOrder[idx]);
if (s == null) {
return Center(child: Text(l10n.noClient));
}

View File

@@ -166,7 +166,7 @@ class _PingPageState extends State<PingPage>
return;
}
if (Providers.server.servers.isEmpty) {
if (Providers.server.serverOrder.isEmpty) {
context.showSnackBar(l10n.pingNoServer);
return;
}
@@ -177,7 +177,7 @@ class _PingPageState extends State<PingPage>
return;
}
await Future.wait(Providers.server.servers.values.map((e) async {
await Future.wait(Providers.server.servers.map((e) async {
if (e.client == null) {
return;
}
@@ -197,7 +197,7 @@ class _PingPageState extends State<PingPage>
@override
Future<FutureOr<void>> afterFirstLayout(BuildContext context) async {
if (Providers.server.servers.isEmpty) {
if (Providers.server.serverOrder.isEmpty) {
await Providers.server.loadLocalData();
await Providers.server.refreshData();
}

View File

@@ -8,7 +8,6 @@ import 'package:toolbox/core/extension/context/locale.dart';
import 'package:toolbox/core/extension/context/snackbar.dart';
import 'package:toolbox/core/extension/uint8list.dart';
import 'package:toolbox/core/utils/misc.dart';
import 'package:toolbox/data/res/provider.dart';
import 'package:toolbox/data/res/store.dart';
import '../../data/model/app/shell_func.dart';
@@ -45,7 +44,7 @@ class _ProcessPageState extends State<ProcessPage> {
@override
void initState() {
super.initState();
_client = Providers.server.servers[widget.spi.id]?.client;
_client = widget.spi.server?.client;
final duration =
Duration(seconds: Stores.setting.serverStatusUpdateInterval.fetch());
_timer = Timer.periodic(duration, (_) => _refresh());
@@ -59,7 +58,7 @@ class _ProcessPageState extends State<ProcessPage> {
Future<void> _refresh() async {
if (mounted) {
final result = await _client?.run(AppShellFuncType.process.exec).string;
final result = await _client?.run(ShellFunc.process.exec).string;
if (result == null || result.isEmpty) {
context.showSnackBar(l10n.noResult);
return;

View File

@@ -68,7 +68,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
@override
Widget build(BuildContext context) {
return Consumer<ServerProvider>(builder: (_, provider, __) {
final s = provider.servers[widget.spi.id];
final s = widget.spi.server;
if (s == null) {
return Scaffold(
body: Center(

View File

@@ -127,7 +127,7 @@ class _ServerPageState extends State<ServerPage>
if (index == count - 1) return UIs.height77;
if (buildTags) index--;
return _buildEachServerCard(provider.servers[filtered[index]]);
return _buildEachServerCard(provider.pick(id: filtered[index]));
},
);
}
@@ -246,14 +246,14 @@ class _ServerPageState extends State<ServerPage>
children: [
IconButton(
onPressed: () => srv.client?.execWithPwd(
AppShellFuncType.shutdown.cmd,
ShellFunc.shutdown.cmd,
context: context,
),
icon: const Icon(Icons.power_off),
),
IconButton(
onPressed: () => srv.client?.execWithPwd(
AppShellFuncType.reboot.cmd,
ShellFunc.reboot.cmd,
context: context,
),
icon: const Icon(Icons.refresh),
@@ -456,16 +456,16 @@ class _ServerPageState extends State<ServerPage>
@override
Future<void> afterFirstLayout(BuildContext context) async {
await GetIt.I.allReady();
if (Providers.server.servers.isEmpty) {
if (Providers.server.serverOrder.isEmpty) {
await Providers.server.loadLocalData();
}
Providers.server.startAutoRefresh();
}
List<String> _filterServers(ServerProvider pro) => pro.serverOrder
.where((e) => pro.servers.containsKey(e))
.where((e) => pro.serverOrder.contains(e))
.where((e) =>
_tag == null || (pro.servers[e]?.spi.tags?.contains(_tag) ?? false))
_tag == null || (pro.pick(id: e)?.spi.tags?.contains(_tag) ?? false))
.toList();
String _getTopRightStr(

View File

@@ -310,10 +310,13 @@ class _SettingPageState extends State<SettingPage> {
Widget _buildAppColor() {
return ListTile(
trailing: ClipOval(
child: Container(
color: primaryColor,
height: 27,
width: 27,
child: ValueBuilder(
listenable: _selectedColorValue,
build: () => Container(
color: primaryColor,
height: 27,
width: 27,
),
),
),
title: Text(l10n.primaryColorSeed),
@@ -368,9 +371,11 @@ class _SettingPageState extends State<SettingPage> {
context.showSnackBar(l10n.failed);
return;
}
// Change [primaryColor] first, then change [_selectedColorValue],
// So the [ValueBuilder] will be triggered with the new value
primaryColor = color;
_selectedColorValue.value = color.value;
_setting.primaryColor.put(_selectedColorValue.value);
primaryColor = color;
context.pop();
RebuildNodes.app.rebuild();
}

View File

@@ -47,7 +47,7 @@ class _ServerOrderPageState extends State<ServerOrderPage> {
}
Widget _buildItem(int index, String id) {
final spi = Providers.server.servers[id]?.spi;
final spi = Providers.server.pick(id: id)?.spi;
if (spi == null) {
return const SizedBox();
}

View File

@@ -130,7 +130,7 @@ class _SnippetListPageState extends State<SnippetListPage> {
final servers = await showDialog<List<Server>>(
context: context,
builder: (_) => TagPicker<Server>(
items: Providers.server.servers.values.toList(),
items: Providers.server.servers.toList(),
tags: Providers.server.tags.toSet(),
),
);

View File

@@ -289,7 +289,7 @@ class _LocalStoragePageState extends State<LocalStoragePage> {
],
);
final id = ids[idx];
final spi = Providers.server.servers[id]?.spi;
final spi = Providers.server.pick(id: id)?.spi;
if (spi == null) {
return;
}
@@ -313,7 +313,7 @@ class _LocalStoragePageState extends State<LocalStoragePage> {
leading: const Icon(Icons.open_in_new),
title: Text(l10n.open),
onTap: () {
shareFiles(context, [file.absolute.path]);
shareFiles([file.absolute.path]);
},
),
],

View File

@@ -33,12 +33,12 @@ import '../../widget/two_line_text.dart';
class SftpPage extends StatefulWidget {
final ServerPrivateInfo spi;
final String? initPath;
final bool selectPath;
final bool isSelect;
const SftpPage({
Key? key,
required this.spi,
required this.selectPath,
required this.isSelect,
this.initPath,
}) : super(key: key);
@@ -47,20 +47,8 @@ class SftpPage extends StatefulWidget {
}
class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
final SftpBrowserStatus _status = SftpBrowserStatus();
SSHClient? _client;
@override
void didChangeDependencies() {
super.didChangeDependencies();
}
@override
void initState() {
super.initState();
_client = Providers.server.servers[widget.spi.id]?.client;
}
final _status = SftpBrowserStatus();
late final _client = widget.spi.server?.client;
@override
Widget build(BuildContext context) {
@@ -69,9 +57,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
leading: IconButton(
icon: const BackButtonIcon(),
onPressed: () {
if (_status.path != null) {
_status.path!.update('/');
}
_status.path?.update('/');
context.pop();
},
),
@@ -102,7 +88,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
}
Widget _buildBottom() {
final children = widget.selectPath
final children = widget.isSelect
? [
IconButton(
onPressed: () => context.pop(_status.path?.path),
@@ -195,13 +181,15 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
leading: const Icon(Icons.folder),
title: Text(l10n.createFolder),
onTap: () => _mkdir(context)),
leading: const Icon(Icons.folder),
title: Text(l10n.createFolder),
onTap: _mkdir,
),
ListTile(
leading: const Icon(Icons.insert_drive_file),
title: Text(l10n.createFile),
onTap: () => _newFile(context)),
leading: const Icon(Icons.insert_drive_file),
title: Text(l10n.createFile),
onTap: _newFile,
),
],
),
)),
@@ -242,7 +230,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
}
_status.path?.update(p);
final suc = await _listDir(path: p);
final suc = await _listDir();
if (suc && Stores.setting.recordHistory.fetch()) {
Stores.history.sftpPath.add(p);
}
@@ -285,7 +273,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
Widget _buildItem(SftpName file) {
final isDir = file.attr.isDirectory;
final trailing = Text(
'${getTime(file.attr.modifyTime)}\n${file.attr.mode?.str ?? ''}',
'${_getTime(file.attr.modifyTime)}\n${file.attr.mode?.str ?? ''}',
style: UIs.textGrey,
textAlign: TextAlign.right,
);
@@ -302,26 +290,26 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
onTap: () {
if (isDir) {
_status.path?.update(file.filename);
_listDir(path: _status.path?.path);
_listDir();
} else {
_onItemPress(context, file, true);
_onItemPress(file, true);
}
},
onLongPress: () => _onItemPress(context, file, !isDir),
onLongPress: () => _onItemPress(file, !isDir),
));
}
void _onItemPress(BuildContext context, SftpName file, bool notDir) {
void _onItemPress(SftpName file, bool notDir) {
final children = [
ListTile(
leading: const Icon(Icons.delete),
title: Text(l10n.delete),
onTap: () => _delete(context, file),
onTap: () => _delete(file),
),
ListTile(
leading: const Icon(Icons.abc),
title: Text(l10n.rename),
onTap: () => _rename(context, file),
onTap: () => _rename(file),
),
];
if (notDir) {
@@ -329,19 +317,19 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
ListTile(
leading: const Icon(Icons.edit),
title: Text(l10n.edit),
onTap: () => _edit(context, file),
onTap: () => _edit(file),
),
ListTile(
leading: const Icon(Icons.download),
title: Text(l10n.download),
onTap: () => _download(context, file),
onTap: () => _download(file),
),
// Only show decompress option when the file is a compressed file
if (_canDecompress(file.filename))
ListTile(
leading: const Icon(Icons.folder_zip),
title: Text(l10n.decompress),
onTap: () => _decompress(context, file),
onTap: () => _decompress(file),
),
]);
}
@@ -353,7 +341,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
);
}
Future<void> _edit(BuildContext context, SftpName name) async {
Future<void> _edit(SftpName name) async {
final size = name.attr.size;
if (size == null || size > Miscs.editorMaxSize) {
context.showSnackBar(l10n.fileTooLarge(
@@ -387,7 +375,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
}
}
void _download(BuildContext context, SftpName name) {
void _download(SftpName name) {
context.showRoundDialog(
title: Text(l10n.attention),
child: Text('${l10n.dl2Local(name.filename)}\n${l10n.keepForeground}'),
@@ -418,7 +406,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
);
}
void _delete(BuildContext context, SftpName file) {
void _delete(SftpName file) {
context.pop();
final isDir = file.attr.isDirectory;
final useRmrf = Stores.setting.sftpRmrfDir.fetch();
@@ -469,7 +457,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
);
}
void _mkdir(BuildContext context) {
void _mkdir() {
context.pop();
final textController = TextEditingController();
context.showRoundDialog(
@@ -510,7 +498,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
);
}
void _newFile(BuildContext context) {
void _newFile() {
context.pop();
final textController = TextEditingController();
context.showRoundDialog(
@@ -550,7 +538,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
);
}
void _rename(BuildContext context, SftpName file) {
void _rename(SftpName file) {
context.pop();
final textController = TextEditingController();
context.showRoundDialog(
@@ -588,7 +576,7 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
);
}
Future<void> _decompress(BuildContext context, SftpName name) async {
Future<void> _decompress(SftpName name) async {
context.pop();
final absPath = _getRemotePath(name);
final cmd = _getDecompressCmd(absPath);
@@ -621,15 +609,18 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
}
/// Only return true if the path is changed
Future<bool> _listDir({String? path, SSHClient? client}) async {
Future<bool> _listDir() async {
context.showLoadingDialog();
if (client != null) {
final sftpc = await client.sftp();
if (_status.client == null) {
final sftpc = await _client?.sftp();
_status.client = sftpc;
}
try {
final listPath = path ?? _status.path?.path ?? '/';
final fs = await _status.client!.listdir(listPath);
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
@@ -676,16 +667,15 @@ class _SftpPageState extends State<SftpPage> with AfterLayoutMixin {
}
Future<void> _backward() async {
if (_status.path!.undo()) {
if (_status.path?.undo() ?? false) {
await _listDir();
}
}
@override
FutureOr<void> afterFirstLayout(BuildContext context) {
final p_ = widget.initPath ?? '/';
_status.path = AbsolutePath(p_);
_listDir(path: p_, client: _client);
_status.path = AbsolutePath(widget.initPath ?? '/');
_listDir();
}
}
@@ -754,3 +744,10 @@ const _extCmdMap = {
'obscpio': 'cpio -idmvF FILE',
'zpaq': 'zpaq x FILE',
};
/// Return fmt: 2021-01-01 00:00:00
String _getTime(int? unixMill) {
return DateTime.fromMillisecondsSinceEpoch((unixMill ?? 0) * 1000)
.toString()
.replaceFirst('.000', '');
}

View File

@@ -72,7 +72,7 @@ class _SftpMissionPageState extends State<SftpMissionPage> {
},
icon: const Icon(Icons.file_open)),
IconButton(
onPressed: () => shareFiles(context, [status.req.localPath]),
onPressed: () => shareFiles([status.req.localPath]),
icon: const Icon(Icons.open_in_new),
)
],