Refactor upload snackbar code for improved readability and maintainability

This commit is contained in:
Leon Bösche
2026-01-13 16:50:09 +01:00
parent d2c26e6203
commit 233f1dd315

View File

@@ -37,7 +37,8 @@ class _FileExplorerState extends State<FileExplorer> {
final TextEditingController _searchController = TextEditingController();
String _searchQuery = '';
final Map<String, bool> _hovered = {};
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? _uploadSnackBarController;
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>?
_uploadSnackBarController;
String _getParentPath(String path) {
if (path == '/') return '/';
@@ -687,9 +688,11 @@ class _FileExplorerState extends State<FileExplorer> {
if (uploadState is UploadInProgress) {
// Calculate overall progress
final uploads = uploadState.uploads;
final activeUploads = uploads.where((u) => !u.isCompleted && u.error == null).toList();
final activeUploads = uploads
.where((u) => !u.isCompleted && u.error == null)
.toList();
final completedUploads = uploads.where((u) => u.isCompleted).toList();
// Show error if any upload failed
for (final upload in uploads) {
if (upload.error != null && upload.error!.isNotEmpty) {
@@ -698,14 +701,16 @@ class _FileExplorerState extends State<FileExplorer> {
);
}
}
// Show progress snackbar for active uploads
if (activeUploads.isNotEmpty) {
final totalProgress = uploads.fold<double>(0, (sum, u) => sum + u.progress) / uploads.length;
final fileName = activeUploads.length == 1
? activeUploads.first.fileName
final totalProgress =
uploads.fold<double>(0, (sum, u) => sum + u.progress) /
uploads.length;
final fileName = activeUploads.length == 1
? activeUploads.first.fileName
: '${activeUploads.length} files';
// Dismiss previous snackbar and show new one
_uploadSnackBarController?.close();
_uploadSnackBarController = ScaffoldMessenger.of(context).showSnackBar(
@@ -719,7 +724,9 @@ class _FileExplorerState extends State<FileExplorer> {
value: totalProgress,
strokeWidth: 2,
color: AppTheme.accentColor,
backgroundColor: AppTheme.accentColor.withValues(alpha: 0.3),
backgroundColor: AppTheme.accentColor.withValues(
alpha: 0.3,
),
),
),
const SizedBox(width: 12),
@@ -732,17 +739,19 @@ class _FileExplorerState extends State<FileExplorer> {
),
],
),
duration: const Duration(days: 1), // Keep showing until dismissed
backgroundColor: AppTheme.surfaceColor,
duration: const Duration(
days: 1,
), // Keep showing until dismissed
backgroundColor: AppTheme.primaryBackground,
),
);
}
// Dismiss snackbar and refresh when all uploads complete
if (completedUploads.length == uploads.length && uploads.isNotEmpty) {
_uploadSnackBarController?.close();
_uploadSnackBarController = null;
final fbState = context.read<FileBrowserBloc>().state;
String currentPath = '/';
if (fbState is DirectoryLoaded) currentPath = fbState.currentPath;