diff --git a/lib/sftp_explorer.dart b/lib/sftp_explorer.dart index f2de2d6..441958f 100644 --- a/lib/sftp_explorer.dart +++ b/lib/sftp_explorer.dart @@ -46,6 +46,7 @@ class _SftpExplorerState extends State { @override Widget build(BuildContext context) { return Scaffold( + // TODO: make appbar same for all directories appBar: AppBar( toolbarHeight: 75, title: Text('Explorer'), @@ -220,23 +221,18 @@ class _SftpExplorerState extends State { TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')), TextButton( onPressed: () async { - // try { - // await widget.sftpWorker.mkdir('${widget.path}${nameController.text}'); - // _listDir(); - // } - // on SftpStatusError catch (e) { - // if (context.mounted) { - // if (e.code == 4) { - // ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Folder Already Exists')); - // } - // else { - // ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, 'Error: ${e.message}')); - // } - // } - // } - // if (context.mounted) { - // Navigator.pop(context); - // } + try { + await widget.sftpWorker.mkdir('${widget.path}${nameController.text}'); + _listDir(); + } + catch (e) { + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar(_buildErrorSnackBar(context, e.toString())); + } + } + if (context.mounted) { + Navigator.pop(context); + } }, child: Text('Ok') ), @@ -260,7 +256,6 @@ class _SftpExplorerState extends State { } try { await for (final progress in widget.sftpWorker.uploadFiles(widget.path, filePaths)) { - print(progress); setState(() => _progress = progress); } } diff --git a/lib/sftp_worker.dart b/lib/sftp_worker.dart index 61966e0..878c50b 100644 --- a/lib/sftp_worker.dart +++ b/lib/sftp_worker.dart @@ -8,11 +8,13 @@ import 'package:path/path.dart'; import 'connection.dart'; sealed class SftpCommand {} + class ListDir extends SftpCommand { final String path; ListDir(this.path); } + class UploadFiles extends SftpCommand { final String path; final List fileNames; @@ -20,6 +22,13 @@ class UploadFiles extends SftpCommand { UploadFiles(this.path, this.fileNames); } +class MkDir extends SftpCommand { + final String path; + + MkDir(this.path); +} + + class SftpWorker { final ReceivePort _responses; @@ -79,7 +88,7 @@ class SftpWorker { static void _sftpCmdHandler(SendPort sendPort, ReceivePort receivePort, SftpClient sftpClient) { receivePort.listen((message) async { - final (int id, dynamic command) = message; + final (int id, SftpCommand command) = message; switch (command) { case ListDir(:final path): try { @@ -115,6 +124,14 @@ class SftpWorker { } sendPort.send((id, 1.0)); } + case MkDir(:final path): + try { + await sftpClient.mkdir(path); + sendPort.send((id, 0)); + } + on SftpStatusError catch (e) { + sendPort.send((id, RemoteError(e.message, ''))); + } } }); } @@ -149,7 +166,7 @@ class SftpWorker { Future> listdir(String path) async { - final completer = Completer.sync(); + final completer = Completer.sync(); final id = _idCounter++; _activeRequests[id] = completer; _commands.send((id, ListDir(path))); @@ -165,5 +182,13 @@ class SftpWorker { return controller.stream; } + Future mkdir(String path) async { + final completer = Completer.sync(); + final id = _idCounter++; + _activeRequests[id] = completer; + _commands.send((id, MkDir(path))); + await completer.future; + } + }