fluxcloud/lib/widgets/operation_buttons.dart

145 lines
5.2 KiB
Dart
Raw Normal View History

import 'package:dartssh2/dartssh2.dart';
import 'package:flutter/material.dart';
2025-08-13 15:50:17 +05:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2025-08-10 07:10:30 +05:00
import 'package:fluxcloud/main.dart';
2025-08-13 15:50:17 +05:00
import 'package:fluxcloud/providers/sftp_state.dart';
2025-08-10 07:08:14 +05:00
import 'package:fluxcloud/sftp_worker.dart';
import 'package:path_provider/path_provider.dart';
2025-08-13 15:50:17 +05:00
class OperationButtons extends ConsumerWidget {
const OperationButtons({
super.key,
2025-08-13 15:50:17 +05:00
required this.sftpWorker, required this.dirEntries,
});
2025-08-10 07:08:14 +05:00
final SftpWorker sftpWorker;
final List<SftpName> dirEntries;
@override
2025-08-13 15:50:17 +05:00
Widget build(BuildContext context, WidgetRef ref) {
final sftpState = ref.watch(sftpNotifierProvider(sftpWorker));
return Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {
},
icon: Icon(Icons.drive_file_move)
),
IconButton(
onPressed: () {
},
icon: Icon(Icons.copy)
),
2025-08-10 07:08:14 +05:00
if (dirEntries.length == 1)
IconButton(
onPressed: () {
2025-08-10 07:08:14 +05:00
final dirEntry = dirEntries[0];
final newNameController = TextEditingController(text: dirEntry.filename);
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Rename'),
content: TextField(
controller: newNameController,
autofocus: true,
decoration: InputDecoration(
labelText: 'Enter new name'
),
),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: Text('Cancel')),
TextButton(
onPressed: () async {
2025-08-10 07:16:13 +05:00
try {
2025-08-13 15:50:17 +05:00
await sftpWorker.rename('${sftpState.path}${dirEntry.filename}', '${sftpState.path}${newNameController.text}');
ref.read(sftpNotifierProvider(sftpWorker).notifier).listDir();
2025-08-10 07:16:13 +05:00
}
on SftpStatusError catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.message));
}
}
if (context.mounted) {
Navigator.pop(context);
}
},
child: Text('Rename')
),
],
)
);
},
icon: Icon(Icons.drive_file_rename_outline)
),
IconButton(
onPressed: () {
showDialog(
context: context,
2025-08-10 07:08:14 +05:00
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 {
2025-08-13 15:50:17 +05:00
await sftpWorker.remove(dirEntry, sftpState.path);
2025-08-10 07:08:14 +05:00
}
catch (e) {
2025-08-10 07:10:30 +05:00
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
}
2025-08-10 07:08:14 +05:00
}
2025-08-13 15:50:17 +05:00
ref.read(sftpNotifierProvider(sftpWorker).notifier).listDir();
2025-08-10 07:08:14 +05:00
if (context.mounted) {
Navigator.pop(context);
}
}
},
child: Text('Yes')
),
],
);
}
);
},
icon: Icon(Icons.delete)
),
IconButton(
onPressed: () async {
final downloadsDir = await getDownloadsDirectory();
if (downloadsDir == null) return;
try {
2025-08-13 15:50:17 +05:00
await for (final progress in sftpWorker.downloadFiles(dirEntries, sftpState.path, downloadsDir.path)) {
ref.read(sftpNotifierProvider(sftpWorker).notifier).setDownloadProgress(progress);
}
2025-08-13 15:50:17 +05:00
ref.read(sftpNotifierProvider(sftpWorker).notifier).setDownloadProgress(null);
}
catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(buildErrorSnackBar(context, e.toString()));
}
}
},
icon: Icon(Icons.download)
)
],
);
}
}