Skip to content

An open-source cross-platform alternative to AirDrop

feat: add the directory itself when drag&drop

Tien Do Nam8c392b4edd45
diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart
index db4b9f19..0b648694 100644
--- a/lib/pages/home_page.dart
+++ b/lib/pages/home_page.dart
@@ -93,7 +93,7 @@ class _HomePageState extends ConsumerState<HomePage> {
onDragDone: (event) async {
if (event.files.length == 1 && Directory(event.files.first.path).existsSync()) {
// user dropped a directory
- await ref.read(selectedSendingFilesProvider.notifier).addDirectory(event.files.first.path);
+ await ref.read(selectedSendingFilesProvider.notifier).addDirectory(event.files.first.path, includeDirectory: true);
} else {
// user dropped one or more files
await ref.read(selectedSendingFilesProvider.notifier).addFiles(
diff --git a/lib/provider/selection/selected_sending_files_provider.dart b/lib/provider/selection/selected_sending_files_provider.dart
index 1dde768f..007e6759 100644
--- a/lib/provider/selection/selected_sending_files_provider.dart
+++ b/lib/provider/selection/selected_sending_files_provider.dart
@@ -65,12 +65,18 @@ class SelectedSendingFilesNotifier extends Notifier<List<CrossFile>> {
]);
}
- Future<void> addDirectory(String directoryPath) async {
+ /// Adds files inside the directory recursively.
+ /// If [includeDirectory] is true, the directory itself will be added as part of each file path.
+ Future<void> addDirectory(String directoryPath, {required bool includeDirectory}) async {
print('Reading files in $directoryPath');
final newFiles = <CrossFile>[];
+ final directoryName = p.basename(directoryPath);
await for (final entity in Directory(directoryPath).list(recursive: true)) {
if (entity is File) {
- final relative = p.relative(entity.path, from: directoryPath).replaceAll('\\', '/');
+ String relative = p.relative(entity.path, from: directoryPath).replaceAll('\\', '/');
+ if (includeDirectory) {
+ relative = '$directoryName/$relative';
+ }
print('Add file $relative');
final file = CrossFile(
name: relative,
diff --git a/lib/util/native/file_picker.dart b/lib/util/native/file_picker.dart
index 730db478..36471d40 100644
--- a/lib/util/native/file_picker.dart
+++ b/lib/util/native/file_picker.dart
@@ -115,7 +115,7 @@ enum FilePickerOption {
try {
final directoryPath = await FilePicker.platform.getDirectoryPath();
if (directoryPath != null) {
- await ref.read(selectedSendingFilesProvider.notifier).addDirectory(directoryPath);
+ await ref.read(selectedSendingFilesProvider.notifier).addDirectory(directoryPath, includeDirectory: false);
}
} catch (e) {
print(e);