feat(download): auto-categorize downloads into folders by file extension (#1572)

This commit is contained in:
WuFeng authored and GitHub committed 2026-10-23 20:38:25 +08:00
1 parent d125d4ab75
commit 9aae13453a
55 files changed
+4403 -217

No files matched your search

@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart' show Icons;
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:path/path.dart' as path;
import 'package:go_router/go_router.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart' as shad;
import 'package:url_launcher/url_launcher.dart';
@@ -88,6 +89,7 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
DownloaderConfig? _config;
StartConfig? _startConfig;
String? _loadedSignature;
String? _appliedDownloadDir;
String? _loadedStartSignature;
Timer? _textSaveTimer;
bool _syncingControllers = false;
@@ -470,13 +472,21 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
SettingsItem(
title: context.l10n.downloadCategories,
child: DownloadCategoriesControl(
categories: config.extra.downloadCategories,
categories: config.categories,
displayName: _categoryName,
onAdd: () => unawaited(_editDownloadCategory()),
onEdit: (category) => unawaited(_editDownloadCategory(category)),
onDelete: (category) => unawaited(_deleteDownloadCategory(category)),
),
),
SettingsItem(
title: context.l10n.autoCategorizeByExtension,
subtitle: context.l10n.autoCategorizeByExtensionDescription,
child: shad.Switch(
value: config.autoCategorize,
onChanged: (value) => _mutateConfig((next) => next.autoCategorize = value),
),
),
SettingsItem(
title: context.l10n.maxRunning,
subtitle: context.l10n.maxRunningDescription,
@@ -996,6 +1006,7 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
_config = DownloaderConfig.fromJson(config.toJson());
_syncingControllers = true;
_downloadDirController.text = _config!.downloadDir;
_appliedDownloadDir = _config!.downloadDir;
_maxRunningController.text = _config!.maxRunning.clamp(1, 256).toString();
_httpUserAgentController.text = _config!.protocolConfig.http.userAgent;
_httpConnectionsController.text = _config!.protocolConfig.http.connections.clamp(1, 256).toString();
@@ -1087,7 +1098,13 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
}
void _applyTextControllers(DownloaderConfig config) {
config.downloadDir = _downloadDirController.text.trim();
final downloadDir = _downloadDirController.text.trim();
final previousDownloadDir = _appliedDownloadDir;
_appliedDownloadDir = downloadDir;
if (previousDownloadDir != null && !path.equals(previousDownloadDir, downloadDir)) {
rebaseBuiltinCategoryPaths(config, oldDir: previousDownloadDir, newDir: downloadDir);
}
config.downloadDir = downloadDir;
config.maxRunning = _boundedInt(_maxRunningController, fallback: config.maxRunning, min: 1, max: 256);
config.protocolConfig.http.userAgent = _httpUserAgentController.text.trim();
config.protocolConfig.http.connections = _boundedInt(
@@ -1357,13 +1374,15 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
category: category,
initialName: category == null ? '' : _categoryName(category),
initialPath: _downloadDirController.text.trim(),
categories: _config?.categories ?? const [],
displayName: _categoryName,
);
if (draft == null || !mounted) return;
_mutateConfig((config) {
final categories = List<DownloadCategory>.from(config.extra.downloadCategories);
final categories = List<DownloadCategory>.from(config.categories);
if (category == null) {
categories.add(DownloadCategory(name: draft.name, path: draft.path));
categories.add(DownloadCategory(name: draft.name, path: draft.path, extensions: draft.extensions));
} else {
final index = categories.indexOf(category);
if (index < 0) return;
@@ -1371,10 +1390,11 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
category
..name = draft.name
..path = draft.path
..extensions = draft.extensions
..isDeleted = false;
if (nameChanged) category.nameKey = null;
}
config.extra.downloadCategories = categories;
config.categories = categories;
});
}
@@ -1386,7 +1406,7 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
category.isDeleted = true;
return;
}
config.extra.downloadCategories = List<DownloadCategory>.from(config.extra.downloadCategories)..remove(category);
config.categories = List<DownloadCategory>.from(config.categories)..remove(category);
});
}