refactor: 下载与镜像设置移入设置页(镜像线路/测速/并发数),下载页保留跳转入口
This commit is contained in:
1 parent
493f22b955
commit
3362c8e165
5 files changed
+196
-80
No files matched your search
@@ -36,7 +36,7 @@ class DownloadTask {
|
||||
/// + 失败重试(指数退避)+ 完成后经 SFTP 自动上传。
|
||||
/// HTTP IO 由 dio 在其内部线程池执行,进度经节流回调到 UI,不阻塞主线程。
|
||||
class DownloadManager extends ChangeNotifier {
|
||||
final int maxConcurrent;
|
||||
int maxConcurrent;
|
||||
final List<DownloadTask> _tasks = [];
|
||||
final Map<String, CancelToken> _cancelTokens = {};
|
||||
final Map<String, bool> _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<DownloadTask> get tasks => List.unmodifiable(_tasks);
|
||||
int get activeCount => _running.length;
|
||||
|
||||
|
||||
@@ -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<DownloadPage> {
|
||||
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<DownloadPage> {
|
||||
return '${(bytes / 1024 / 1024).toStringAsFixed(1)} MB';
|
||||
}
|
||||
|
||||
Future<void> _showMirrorDialog() async {
|
||||
final dp = context.read<DownloadProvider>();
|
||||
final mirror = dp.mirror;
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (_) => StatefulBuilder(
|
||||
builder: (context, setDialogState) => AlertDialog(
|
||||
title: const Text('镜像线路'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
RadioListTile<String>(
|
||||
title: const Text('Modrinth 官方'),
|
||||
value: 'official',
|
||||
groupValue: mirror.currentId,
|
||||
onChanged: (v) async {
|
||||
await mirror.select(v!);
|
||||
setDialogState(() {});
|
||||
},
|
||||
),
|
||||
RadioListTile<String>(
|
||||
title: const Text('MCIM 国内镜像'),
|
||||
value: 'mcim',
|
||||
groupValue: mirror.currentId,
|
||||
onChanged: (v) async {
|
||||
await mirror.select(v!);
|
||||
setDialogState(() {});
|
||||
},
|
||||
),
|
||||
RadioListTile<String>(
|
||||
title: const Text('自定义镜像'),
|
||||
value: 'custom',
|
||||
groupValue: mirror.currentId,
|
||||
onChanged: (v) async {
|
||||
final ctrl = TextEditingController(text: mirror.customUrl);
|
||||
final url = await showDialog<String>(
|
||||
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(() {});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<DownloadSettingsPage> createState() => _DownloadSettingsPageState();
|
||||
}
|
||||
|
||||
class _DownloadSettingsPageState extends State<DownloadSettingsPage> {
|
||||
bool _testing = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
context.read<DownloadProvider>().init();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dp = context.watch<DownloadProvider>();
|
||||
final mirror = dp.mirror;
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('下载与镜像设置')),
|
||||
body: ListView(
|
||||
children: [
|
||||
const SectionHeader('镜像线路'),
|
||||
RadioListTile<String>(
|
||||
title: const Text('MCIM 国内镜像(推荐)'),
|
||||
subtitle: const Text('modrinth.mcimirror.top,无需科学上网'),
|
||||
value: 'mcim',
|
||||
groupValue: mirror.currentId,
|
||||
onChanged: (v) async {
|
||||
await mirror.select(v!);
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
RadioListTile<String>(
|
||||
title: const Text('Modrinth 官方'),
|
||||
subtitle: const Text('直连官方源,适合海外网络'),
|
||||
value: 'official',
|
||||
groupValue: mirror.currentId,
|
||||
onChanged: (v) async {
|
||||
await mirror.select(v!);
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
RadioListTile<String>(
|
||||
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<int>(
|
||||
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<void> _editCustomUrl() async {
|
||||
final dp = context.read<DownloadProvider>();
|
||||
final ctrl = TextEditingController(text: dp.mirror.customUrl);
|
||||
final url = await showDialog<String>(
|
||||
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)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<SettingsPage> {
|
||||
],
|
||||
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),
|
||||
|
||||
@@ -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<void> init() async {
|
||||
await mirror.load();
|
||||
// 读取持久化的并发数
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final c = prefs.getInt('download_concurrency');
|
||||
if (c != null) manager.setConcurrency(c);
|
||||
}
|
||||
|
||||
/// 设置并发数并持久化。
|
||||
Future<void> 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<List<ModrinthSearchResult>> search(
|
||||
String query, {String? mcVersion, String? loader, String projectType = 'mod', int limit = 20}) {
|
||||
|
||||
Reference in new issue
Block a user