diff --git a/lib/core/services/download_manager.dart b/lib/core/services/download_manager.dart index ee75a50..5749c3e 100644 --- a/lib/core/services/download_manager.dart +++ b/lib/core/services/download_manager.dart @@ -36,7 +36,7 @@ class DownloadTask { /// + 失败重试(指数退避)+ 完成后经 SFTP 自动上传。 /// HTTP IO 由 dio 在其内部线程池执行,进度经节流回调到 UI,不阻塞主线程。 class DownloadManager extends ChangeNotifier { - final int maxConcurrent; + int maxConcurrent; final List _tasks = []; final Map _cancelTokens = {}; final Map _pauseFlags = {}; @@ -50,6 +50,13 @@ class DownloadManager extends ChangeNotifier { receiveTimeout: const Duration(minutes: 10), )); + /// 修改并发数(设置页可调),立即生效。 + void setConcurrency(int n) { + if (n < 1 || n > 6) return; + maxConcurrent = n; + _pump(); + } + List get tasks => List.unmodifiable(_tasks); int get activeCount => _running.length; diff --git a/lib/features/download/download_page.dart b/lib/features/download/download_page.dart index 9f53b04..51643cb 100644 --- a/lib/features/download/download_page.dart +++ b/lib/features/download/download_page.dart @@ -3,6 +3,7 @@ import 'package:provider/provider.dart'; import '../../core/services/modrinth_api.dart'; import '../../providers/download_provider.dart'; import '../../core/services/download_manager.dart'; +import '../settings/download_settings_page.dart'; /// 下载中心:Modrinth 搜索 + 镜像线路选择 + 下载任务队列(M2)。 class DownloadPage extends StatefulWidget { @@ -67,9 +68,12 @@ class _DownloadPageState extends State { title: const Text('下载中心'), actions: [ IconButton( - icon: const Icon(Icons.speed), - tooltip: '镜像线路:${dp.mirror.currentName}', - onPressed: _showMirrorDialog, + icon: const Icon(Icons.settings_outlined), + tooltip: '下载与镜像设置', + onPressed: () => Navigator.push( + context, + MaterialPageRoute(builder: (_) => const DownloadSettingsPage()), + ), ), ], bottom: const TabBar(tabs: [Tab(text: '搜索下载'), Tab(text: '任务队列')]), @@ -317,80 +321,5 @@ class _DownloadPageState extends State { return '${(bytes / 1024 / 1024).toStringAsFixed(1)} MB'; } - Future _showMirrorDialog() async { - final dp = context.read(); - final mirror = dp.mirror; - await showDialog( - context: context, - builder: (_) => StatefulBuilder( - builder: (context, setDialogState) => AlertDialog( - title: const Text('镜像线路'), - content: Column( - mainAxisSize: MainAxisSize.min, - children: [ - RadioListTile( - title: const Text('Modrinth 官方'), - value: 'official', - groupValue: mirror.currentId, - onChanged: (v) async { - await mirror.select(v!); - setDialogState(() {}); - }, - ), - RadioListTile( - title: const Text('MCIM 国内镜像'), - value: 'mcim', - groupValue: mirror.currentId, - onChanged: (v) async { - await mirror.select(v!); - setDialogState(() {}); - }, - ), - RadioListTile( - title: const Text('自定义镜像'), - value: 'custom', - groupValue: mirror.currentId, - onChanged: (v) async { - final ctrl = TextEditingController(text: mirror.customUrl); - final url = await showDialog( - context: context, - builder: (_) => AlertDialog( - title: const Text('自定义镜像地址'), - content: TextField(controller: ctrl, decoration: const InputDecoration(hintText: 'https://your-mirror.example.com')), - actions: [ - TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')), - FilledButton(onPressed: () => Navigator.pop(context, ctrl.text.trim()), child: const Text('确定')), - ], - ), - ); - if (url != null && url.isNotEmpty) { - await mirror.select('custom', customUrl: url); - } - setDialogState(() {}); - }, - ), - const SizedBox(height: 8), - OutlinedButton.icon( - icon: const Icon(Icons.bolt), - label: const Text('一键测速并选择最快线路'), - onPressed: () async { - await mirror.autoSelect(); - if (context.mounted) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('已选择最快线路:${mirror.currentName}(${mirror.lastSpeedMs}ms)')), - ); - } - setDialogState(() {}); - }, - ), - ], - ), - actions: [ - FilledButton(onPressed: () => Navigator.pop(context), child: const Text('完成')), - ], - ), - ), - ); - if (mounted) setState(() {}); - } + } diff --git a/lib/features/settings/download_settings_page.dart b/lib/features/settings/download_settings_page.dart new file mode 100644 index 0000000..f863588 --- /dev/null +++ b/lib/features/settings/download_settings_page.dart @@ -0,0 +1,152 @@ +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import '../../providers/download_provider.dart'; + +/// 下载与镜像设置页:镜像线路选择、测速自动选线、并发数(原下载页内设置移入此处)。 +class DownloadSettingsPage extends StatefulWidget { + const DownloadSettingsPage({super.key}); + + @override + State createState() => _DownloadSettingsPageState(); +} + +class _DownloadSettingsPageState extends State { + bool _testing = false; + + @override + void initState() { + super.initState(); + context.read().init(); + } + + @override + Widget build(BuildContext context) { + final dp = context.watch(); + final mirror = dp.mirror; + return Scaffold( + appBar: AppBar(title: const Text('下载与镜像设置')), + body: ListView( + children: [ + const SectionHeader('镜像线路'), + RadioListTile( + title: const Text('MCIM 国内镜像(推荐)'), + subtitle: const Text('modrinth.mcimirror.top,无需科学上网'), + value: 'mcim', + groupValue: mirror.currentId, + onChanged: (v) async { + await mirror.select(v!); + setState(() {}); + }, + ), + RadioListTile( + title: const Text('Modrinth 官方'), + subtitle: const Text('直连官方源,适合海外网络'), + value: 'official', + groupValue: mirror.currentId, + onChanged: (v) async { + await mirror.select(v!); + setState(() {}); + }, + ), + RadioListTile( + title: const Text('自定义镜像'), + subtitle: Text(mirror.currentId == 'custom' && mirror.customUrl.isNotEmpty + ? mirror.customUrl + : '使用自建/第三方镜像地址'), + value: 'custom', + groupValue: mirror.currentId, + onChanged: (v) async => {await _editCustomUrl(), setState(() {})}, + ), + ListTile( + leading: _testing + ? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2)) + : const Icon(Icons.speed), + title: const Text('一键测速并选择最快线路'), + subtitle: Text(mirror.lastSpeedMs > 0 + ? '上次测速:${mirror.lastSpeedMs}ms(${mirror.currentName})' + : '自动在官方与镜像之间选择延迟最低的线路'), + onTap: _testing + ? null + : () async { + final messenger = ScaffoldMessenger.of(context); + setState(() => _testing = true); + await mirror.autoSelect(); + if (!mounted) return; + setState(() => _testing = false); + messenger.showSnackBar(SnackBar( + content: Text('已选择最快线路:${mirror.currentName}(${mirror.lastSpeedMs}ms)'), + )); + }, + ), + const Divider(), + const SectionHeader('下载队列'), + ListTile( + leading: const Icon(Icons.playlist_play), + title: const Text('同时下载任务数'), + subtitle: Text('当前:${dp.concurrency} 个(越大越占带宽,建议 1-3)'), + trailing: DropdownButton( + value: dp.concurrency, + underline: const SizedBox.shrink(), + items: const [1, 2, 3, 4, 5, 6] + .map((n) => DropdownMenuItem(value: n, child: Text('$n'))) + .toList(), + onChanged: (n) async { + if (n == null) return; + await dp.setConcurrency(n); + setState(() {}); + }, + ), + ), + const Divider(), + const Padding( + padding: EdgeInsets.all(16), + child: Text( + '说明:下载完成的文件保存在应用目录 downloads/ 下;' + '若服务器配置了 SSH,会自动上传到对应服务器的 mods/plugins 目录。', + style: TextStyle(fontSize: 13), + ), + ), + ], + ), + ); + } + + Future _editCustomUrl() async { + final dp = context.read(); + final ctrl = TextEditingController(text: dp.mirror.customUrl); + final url = await showDialog( + context: context, + builder: (_) => AlertDialog( + title: const Text('自定义镜像地址'), + content: TextField( + controller: ctrl, + decoration: const InputDecoration(hintText: 'https://your-mirror.example.com'), + ), + actions: [ + TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')), + FilledButton(onPressed: () => Navigator.pop(context, ctrl.text.trim()), child: const Text('确定')), + ], + ), + ); + if (url != null && url.isNotEmpty) { + await dp.mirror.select('custom', customUrl: url); + } + } +} + +class SectionHeader extends StatelessWidget { + final String text; + const SectionHeader(this.text, {super.key}); + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.fromLTRB(16, 14, 16, 6), + child: Text(text, + style: Theme.of(context) + .textTheme + .titleSmall + ?.copyWith(color: Theme.of(context).colorScheme.primary)), + ); + } +} diff --git a/lib/features/settings/settings_page.dart b/lib/features/settings/settings_page.dart index 4c145d9..39f9a9d 100644 --- a/lib/features/settings/settings_page.dart +++ b/lib/features/settings/settings_page.dart @@ -12,6 +12,7 @@ import '../../core/widgets/background_widget.dart'; import '../../providers/theme_provider.dart'; import '../../providers/wallpaper_provider.dart'; import 'help_page.dart'; +import 'download_settings_page.dart'; class SettingsPage extends StatefulWidget { const SettingsPage({super.key}); @@ -279,6 +280,19 @@ class _SettingsPageState extends State { ], const Divider(), + // ---------- 下载与镜像 ---------- + ListTile( + leading: const Icon(Icons.download_outlined), + title: const Text('下载与镜像设置'), + subtitle: const Text('镜像线路、测速选线、同时下载任务数'), + trailing: const Icon(Icons.chevron_right), + onTap: () => Navigator.push( + context, + MaterialPageRoute(builder: (_) => const DownloadSettingsPage()), + ), + ), + const Divider(), + // ---------- 使用教程 ---------- ListTile( leading: const Icon(Icons.menu_book_outlined), diff --git a/lib/providers/download_provider.dart b/lib/providers/download_provider.dart index 65231e1..1b863b2 100644 --- a/lib/providers/download_provider.dart +++ b/lib/providers/download_provider.dart @@ -1,5 +1,6 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:path_provider/path_provider.dart'; import '../../core/services/download_manager.dart'; import '../../core/services/modrinth_api.dart'; @@ -19,8 +20,21 @@ class DownloadProvider extends ChangeNotifier { Future init() async { await mirror.load(); + // 读取持久化的并发数 + final prefs = await SharedPreferences.getInstance(); + final c = prefs.getInt('download_concurrency'); + if (c != null) manager.setConcurrency(c); } + /// 设置并发数并持久化。 + Future setConcurrency(int n) async { + manager.setConcurrency(n); + final prefs = await SharedPreferences.getInstance(); + await prefs.setInt('download_concurrency', n); + } + + int get concurrency => manager.maxConcurrent; + /// 搜索 Modrinth。 Future> search( String query, {String? mcVersion, String? loader, String projectType = 'mod', int limit = 20}) {