From 045d0694e7da58e0426e121dcacd245a41d7b695 Mon Sep 17 00:00:00 2001 From: RafayAhmad7548 Date: Sun, 10 Aug 2025 07:08:14 +0500 Subject: [PATCH] delete worky --- lib/sftp_explorer.dart | 2 +- lib/sftp_worker.dart | 41 +++++++++++++++ lib/widgets/operation_buttons.dart | 81 ++++++++++++++++-------------- 3 files changed, 84 insertions(+), 40 deletions(-) diff --git a/lib/sftp_explorer.dart b/lib/sftp_explorer.dart index d213c05..6fbe64a 100644 --- a/lib/sftp_explorer.dart +++ b/lib/sftp_explorer.dart @@ -124,7 +124,7 @@ class _SftpExplorerState extends State { return ListTile( leading: Icon(dirEntry.attr.isDirectory ? Icons.folder : Icons.description), title: Text(dirEntry.filename), - trailing: OperationButtons(dirEntry: dirEntry), + trailing: OperationButtons(sftpWorker: widget.sftpWorker, path: path, dirEntries: [dirEntry], listDir: _listDir,), onTap: () { if (dirEntry.attr.isDirectory) { path = '$path${dirEntry.filename}/'; diff --git a/lib/sftp_worker.dart b/lib/sftp_worker.dart index 878c50b..120c6af 100644 --- a/lib/sftp_worker.dart +++ b/lib/sftp_worker.dart @@ -28,6 +28,13 @@ class MkDir extends SftpCommand { MkDir(this.path); } +class Remove extends SftpCommand { + final SftpName dirEntry; + final String path; + + Remove(this.dirEntry, this.path); +} + class SftpWorker { @@ -132,6 +139,33 @@ class SftpWorker { on SftpStatusError catch (e) { sendPort.send((id, RemoteError(e.message, ''))); } + case Remove(:final dirEntry, :final path): + try { + if (dirEntry.attr.isDirectory) { + Future removeRecursively (String path) async { + final dirContents = await sftpClient.listdir(path); + for (SftpName entry in dirContents) { + final fullPath = '$path${entry.filename}'; + if (entry.attr.isDirectory) { + await removeRecursively('$fullPath/'); + await sftpClient.rmdir('$fullPath/'); + } + else { + await sftpClient.remove(fullPath); + } + } + await sftpClient.rmdir(path); + } + await removeRecursively('$path${dirEntry.filename}/'); + } + else { + await sftpClient.remove('$path${dirEntry.filename}'); + } + sendPort.send((id, 0)); + } + on SftpStatusError catch (e) { + sendPort.send((id, RemoteError(e.message, ''))); + } } }); } @@ -190,5 +224,12 @@ class SftpWorker { await completer.future; } + Future remove(SftpName dirEntry, String path) async { + final completer = Completer.sync(); + final id = _idCounter++; + _activeRequests[id] = completer; + _commands.send((id, Remove(dirEntry, path))); + await completer.future; + } } diff --git a/lib/widgets/operation_buttons.dart b/lib/widgets/operation_buttons.dart index d5043d6..9584762 100644 --- a/lib/widgets/operation_buttons.dart +++ b/lib/widgets/operation_buttons.dart @@ -1,13 +1,17 @@ import 'package:dartssh2/dartssh2.dart'; import 'package:flutter/material.dart'; +import 'package:fluxcloud/sftp_worker.dart'; class OperationButtons extends StatelessWidget { const OperationButtons({ super.key, - required this.dirEntry, + required this.sftpWorker, required this.path, required this.dirEntries, required this.listDir, }); - final SftpName dirEntry; + final SftpWorker sftpWorker; + final String path; + final List dirEntries; + final Function listDir; @override Widget build(BuildContext context) { @@ -26,8 +30,10 @@ class OperationButtons extends StatelessWidget { }, icon: Icon(Icons.copy) ), + if (dirEntries.length == 1) IconButton( onPressed: () { + final dirEntry = dirEntries[0]; final newNameController = TextEditingController(text: dirEntry.filename); showDialog( context: context, @@ -45,7 +51,7 @@ class OperationButtons extends StatelessWidget { TextButton( onPressed: () async { // try { - // await widget.sftpWorker.rename('${path}${dirEntry.filename}', '${widget.path}${newNameController.text}'); + // await sftpWorker.rename('${path}${dirEntry.filename}', '${widget.path}${newNameController.text}'); // _listDir(); // } // on SftpStatusError catch (e) { @@ -71,42 +77,39 @@ class OperationButtons extends StatelessWidget { onPressed: () { showDialog( context: context, - builder: (context) => AlertDialog( - title: Text('Delete Permanently?'), - content: Text(dirEntry.attr.isDirectory ? 'The contents of this folder will be deleted as well\nThis action cannot be undone' : 'This action cannot be undone'), - actions: [ - TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')), - TextButton( - onPressed: () async { - // if (dirEntry.attr.isDirectory) { - // Future removeRecursively (String path) async { - // final dirContents = await widget.sftpWorker.listdir(path); - // for (SftpName entry in dirContents) { - // final fullPath = '$path${entry.filename}'; - // if (entry.attr.isDirectory) { - // await removeRecursively('$fullPath/'); - // await widget.sftpWorker.rmdir('$fullPath/'); - // } - // else { - // await widget.sftpWorker.remove(fullPath); - // } - // } - // await widget.sftpWorker.rmdir(path); - // } - // await removeRecursively('${path}${dirEntry.filename}/'); - // } - // else { - // await widget.sftpWorker.remove('${path}${dirEntry.filename}'); - // } - // _listDir(); - // if (context.mounted) { - // Navigator.pop(context); - // } - }, - child: Text('Yes') - ), - ], - ) + builder: (context) { + String warningText = 'This action cannot be undone'; + if (dirEntries.length == 1 && dirEntries[0].attr.isDirectory) { + warningText = 'The contents of this folder will be deleted as well\n$warningText'; + } + else if (dirEntries.length > 1) { + warningText = 'All selected files will be deleted\n$warningText'; + } + return AlertDialog( + title: Text('Delete Permanently?'), + content: Text(warningText), + actions: [ + TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')), + TextButton( + onPressed: () async { + for (final dirEntry in dirEntries) { + try { + await sftpWorker.remove(dirEntry, path); + } + catch (e) { + print(e.toString()); + } + listDir(); + if (context.mounted) { + Navigator.pop(context); + } + } + }, + child: Text('Yes') + ), + ], + ); + } ); }, icon: Icon(Icons.delete)