基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。
feat(ui): redesign the download management page
Predidit2f94c75e0b46
diff --git a/lib/pages/download/download_page.dart b/lib/pages/download/download_page.dartindex 9d4f3061..e8ad4633 100644--- a/lib/pages/download/download_page.dart+++ b/lib/pages/download/download_page.dart@@ -6,6 +6,7 @@ import 'package:kazumi/bean/dialog/dialog_helper.dart';import 'package:kazumi/modules/download/download_module.dart';import 'package:kazumi/modules/bangumi/bangumi_item.dart';import 'package:kazumi/pages/download/download_controller.dart';+import 'package:kazumi/pages/download/download_widgets.dart';import 'package:kazumi/pages/video/video_playback_args.dart';import 'package:kazumi/utils/format.dart';@@ -24,47 +25,46 @@ class DownloadPage extends StatefulWidget {class _DownloadPageState extends State<DownloadPage> {DownloadController get downloadController => widget.controller;+ /// Expansion state per record key. Kept outside the record snapshots+ /// because the controller replaces them on every refresh tick.+ final Map<String, bool> _expanded = {};+@overridevoid initState() {super.initState();downloadController.refreshRecords();}+ bool _isExpanded(String recordKey, DownloadRecord record) {+ return _expanded.putIfAbsent(+ recordKey,+ () => record.episodes.values+ .any((e) => e.status != DownloadStatus.completed),+ );+ }+@overrideWidget build(BuildContext context) {return Scaffold(appBar: const SysAppBar(title: Text('下载管理')),body: Observer(builder: (context) {final recordKeys = downloadController.recordKeys.toList();+ // Drop expansion state for deleted records, so a re-downloaded+ // bangumi gets a fresh default instead of a stale cached one.+ _expanded.removeWhere((key, _) => !recordKeys.contains(key));if (recordKeys.isEmpty) {- return Center(- child: Column(- mainAxisSize: MainAxisSize.min,- children: [- Icon(- Icons.download_rounded,- size: 72,- color: Theme.of(context)- .colorScheme- .outline- .withValues(alpha: 0.5),- ),- const SizedBox(height: 16),- Text(- '暂无离线下载',- style: Theme.of(context).textTheme.titleMedium?.copyWith(- color: Theme.of(context).colorScheme.outline,- ),- ),- ],- ),- );+ return const Center(child: DownloadEmptyState());}return ListView.builder(- padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),+ padding: const EdgeInsets.all(16),itemCount: recordKeys.length,itemBuilder: (context, index) {- return _buildRecordCard(recordKeys[index]);+ return Center(+ child: ConstrainedBox(+ constraints: const BoxConstraints(maxWidth: 1000),+ child: _buildRecordCard(recordKeys[index]),+ ),+ );},);}),@@ -78,165 +78,57 @@ class _DownloadPageState extends State<DownloadPage> {return const SizedBox.shrink();}- final episodes = record.episodes.values.toList()- ..sort((a, b) => a.episodeNumber.compareTo(b.episodeNumber));- final completedCount = downloadController.completedCount(record);+ var totalSpeed = 0.0;+ for (final e in record.episodes.values) {+ if (e.status == DownloadStatus.downloading) {+ totalSpeed += downloadController.getSpeed(+ record.bangumiId,+ record.pluginName,+ e.episodeNumber,+ );+ }+ }+ final expanded = _isExpanded(recordKey, record);- return Card(- margin: const EdgeInsets.only(bottom: 12),- clipBehavior: Clip.antiAlias,- child: Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- ListTile(- leading: ClipRRect(- borderRadius: BorderRadius.circular(4),- child: Image.network(- record.bangumiCover,- width: 48,- height: 64,- fit: BoxFit.cover,- errorBuilder: (_, __, ___) => Container(- width: 48,- height: 64,- color:- Theme.of(context).colorScheme.surfaceContainerHighest,- child: const Icon(Icons.movie_outlined),- ),- ),- ),- title: Text(- record.bangumiName,- maxLines: 1,- overflow: TextOverflow.ellipsis,- ),- subtitle: Text(- '来源: ${record.pluginName} · $completedCount/${episodes.length} 已完成',- style: TextStyle(- fontSize: 12,- color: Theme.of(context).colorScheme.outline,- ),- ),- trailing: PopupMenuButton<String>(- onSelected: (value) {- if (value == 'delete') {- _confirmDeleteRecord(record);- } else if (value == 'resume_all') {- downloadController.resumeAllDownloads(- record.bangumiId,- record.pluginName,- );- KazumiDialog.showToast(message: '已开始恢复下载');- }- },- itemBuilder: (context) => [- const PopupMenuItem(- value: 'resume_all',- child: Text('开始全部'),- ),- const PopupMenuItem(- value: 'delete',- child: Text('删除全部'),- ),- ],- ),- ),- ...episodes.map((ep) => _buildEpisodeTile(record, ep)),- ],- ),+ return DownloadRecordCard(+ record: record,+ expanded: expanded,+ onToggle: () {+ setState(() => _expanded[recordKey] = !expanded);+ },+ onResumeAll: () {+ downloadController.resumeAllDownloads(+ record.bangumiId,+ record.pluginName,+ );+ KazumiDialog.showToast(message: '已开始恢复下载');+ },+ onDeleteAll: () => _confirmDeleteRecord(record),+ totalSpeed: totalSpeed,+ episodeTileBuilder: () {+ final episodes = record.episodes.values.toList()+ ..sort((a, b) => a.episodeNumber.compareTo(b.episodeNumber));+ return episodes.map((ep) => _buildEpisodeTile(record, ep)).toList();+ },);});}Widget _buildEpisodeTile(DownloadRecord record, DownloadEpisode episode) {- final statusIcon = _getStatusIcon(episode);- final statusText = _getStatusText(record, episode);-- return Padding(- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),- child: Row(- children: [- statusIcon,- const SizedBox(width: 8),- Expanded(- child: Column(- crossAxisAlignment: CrossAxisAlignment.start,- children: [- Text(- episode.episodeName.isNotEmpty- ? episode.episodeName- : '第${episode.episodeNumber}集',- maxLines: 1,- overflow: TextOverflow.ellipsis,- style: const TextStyle(fontSize: 14),- ),- const SizedBox(height: 2),- Text(- statusText,- style: TextStyle(- fontSize: 12,- color: episode.status == DownloadStatus.failed- ? Theme.of(context).colorScheme.error- : Theme.of(context).colorScheme.outline,- ),- ),- ],- ),- ),- if (episode.status == DownloadStatus.downloading) ...[- SizedBox(- width: 60,- child: LinearProgressIndicator(- value: episode.progressPercent,- minHeight: 3,- ),- ),- const SizedBox(width: 8),- ],- ..._getActionButtons(record, episode),- ],- ),+ return DownloadEpisodeTile(+ episode: episode,+ statusText: _getStatusText(record, episode),+ actions: _getActionButtons(record, episode),+ onPlay: episode.status == DownloadStatus.completed+ ? () => _playEpisode(record, episode)+ : null,);}- Widget _getStatusIcon(DownloadEpisode episode) {- switch (episode.status) {- case DownloadStatus.completed:- return Icon(Icons.offline_pin,- size: 20, color: Theme.of(context).colorScheme.primary);- case DownloadStatus.downloading:- return SizedBox(- width: 20,- height: 20,- child: CircularProgressIndicator(- value: episode.progressPercent,- strokeWidth: 2,- ),- );- case DownloadStatus.failed:- return Icon(Icons.error_outline,- size: 20, color: Theme.of(context).colorScheme.error);- case DownloadStatus.paused:- return Icon(Icons.pause_circle_outline,- size: 20, color: Theme.of(context).colorScheme.outline);- case DownloadStatus.pending:- return Icon(Icons.hourglass_empty,- size: 20, color: Theme.of(context).colorScheme.outline);- case DownloadStatus.resolving:- return const SizedBox(- width: 20,- height: 20,- child: CircularProgressIndicator(strokeWidth: 2),- );- default:- return const SizedBox(width: 20, height: 20);- }- }-String _getStatusText(DownloadRecord record, DownloadEpisode episode) {switch (episode.status) {case DownloadStatus.completed:- return '已完成 ${formatBytes(episode.totalBytes)}';+ return '已完成 · ${formatBytes(episode.totalBytes)}';case DownloadStatus.downloading:final speed = downloadController.getSpeed(record.bangumiId,@@ -244,16 +136,16 @@ class _DownloadPageState extends State<DownloadPage> {episode.episodeNumber,);final speedText = speed > 0 ? ' · ${formatSpeed(speed)}' : '';- return '${(episode.progressPercent * 100).toStringAsFixed(0)}% '- '${episode.downloadedSegments}/${episode.totalSegments}$speedText';+ return '${(episode.progressPercent * 100).toStringAsFixed(0)}% · '+ '${episode.downloadedSegments}/${episode.totalSegments} 分片$speedText';case DownloadStatus.failed:return episode.errorMessage.isNotEmpty ? episode.errorMessage : '下载失败';case DownloadStatus.paused:- return '已暂停 ${(episode.progressPercent * 100).toStringAsFixed(0)}%';+ return '已暂停 · ${(episode.progressPercent * 100).toStringAsFixed(0)}%';case DownloadStatus.pending:- return '等待中';+ return '排队中';case DownloadStatus.resolving:- return '解析视频源中';+ return '正在解析视频源';default:return '';}@@ -261,13 +153,14 @@ class _DownloadPageState extends State<DownloadPage> {List<Widget> _getActionButtons(DownloadRecord record, DownloadEpisode episode) {+ final colorScheme = Theme.of(context).colorScheme;final buttons = <Widget>[];switch (episode.status) {case DownloadStatus.completed:buttons.add(IconButton(icon: Icon(Icons.play_circle_outline,- size: 20, color: Theme.of(context).colorScheme.primary),+ size: 20, color: colorScheme.primary),onPressed: () => _playEpisode(record, episode),tooltip: '播放',visualDensity: VisualDensity.compact,@@ -275,7 +168,7 @@ class _DownloadPageState extends State<DownloadPage> {break;case DownloadStatus.downloading:buttons.add(IconButton(- icon: const Icon(Icons.pause, size: 20),+ icon: const Icon(Icons.pause_rounded, size: 20),onPressed: () => downloadController.pauseDownload(record.bangumiId,record.pluginName,@@ -287,7 +180,7 @@ class _DownloadPageState extends State<DownloadPage> {break;case DownloadStatus.paused:buttons.add(IconButton(- icon: const Icon(Icons.play_arrow, size: 20),+ icon: const Icon(Icons.play_arrow_rounded, size: 20),onPressed: () => downloadController.retryDownload(bangumiId: record.bangumiId,pluginName: record.pluginName,@@ -299,7 +192,7 @@ class _DownloadPageState extends State<DownloadPage> {break;case DownloadStatus.failed:buttons.add(IconButton(- icon: const Icon(Icons.refresh, size: 20),+ icon: const Icon(Icons.refresh_rounded, size: 20),onPressed: () => downloadController.retryDownload(bangumiId: record.bangumiId,pluginName: record.pluginName,@@ -312,7 +205,7 @@ class _DownloadPageState extends State<DownloadPage> {case DownloadStatus.pending:buttons.add(IconButton(icon: Icon(Icons.priority_high,- size: 20, color: Theme.of(context).colorScheme.primary),+ size: 20, color: colorScheme.primary),onPressed: () {downloadController.priorityDownload(bangumiId: record.bangumiId,@@ -330,7 +223,8 @@ class _DownloadPageState extends State<DownloadPage> {}buttons.add(IconButton(- icon: const Icon(Icons.delete_outline, size: 20),+ icon: Icon(Icons.delete_outline,+ size: 20, color: colorScheme.onSurfaceVariant),onPressed: () => _confirmDeleteEpisode(record, episode),tooltip: '删除',visualDensity: VisualDensity.compact,@@ -408,7 +302,10 @@ class _DownloadPageState extends State<DownloadPage> {);KazumiDialog.dismiss();},- child: const Text('删除'),+ child: Text(+ '删除',+ style: TextStyle(color: Theme.of(context).colorScheme.error),+ ),),],),@@ -436,7 +333,10 @@ class _DownloadPageState extends State<DownloadPage> {);KazumiDialog.dismiss();},- child: const Text('删除'),+ child: Text(+ '删除',+ style: TextStyle(color: Theme.of(context).colorScheme.error),+ ),),],),diff --git a/lib/pages/download/download_widgets.dart b/lib/pages/download/download_widgets.dartnew file mode 100644index 00000000..edc90bdc--- /dev/null+++ b/lib/pages/download/download_widgets.dart@@ -0,0 +1,396 @@+import 'package:flutter/material.dart';+import 'package:kazumi/bean/card/network_img_layer.dart';+import 'package:kazumi/bean/card/rule_card.dart';+import 'package:kazumi/modules/download/download_module.dart';+import 'package:kazumi/utils/format.dart';++const Duration _kExpandDuration = Duration(milliseconds: 250);+const Curve _kExpandCurve = Curves.easeInOutCubic;++/// Rounded tonal card for one bangumi download record: cover, title,+/// source tag, aggregate progress and a collapsible episode list.+class DownloadRecordCard extends StatelessWidget {+ const DownloadRecordCard({+ super.key,+ required this.record,+ required this.expanded,+ required this.onToggle,+ required this.onResumeAll,+ required this.onDeleteAll,+ required this.totalSpeed,+ required this.episodeTileBuilder,+ });++ final DownloadRecord record;+ final bool expanded;+ final VoidCallback onToggle;+ final VoidCallback onResumeAll;+ final VoidCallback onDeleteAll;+ final double totalSpeed;++ /// Builds the episode rows; only invoked while [expanded], so collapsed+ /// cards skip sorting and tile construction entirely.+ final List<Widget> Function() episodeTileBuilder;++ @override+ Widget build(BuildContext context) {+ final colorScheme = Theme.of(context).colorScheme;+ final textTheme = Theme.of(context).textTheme;+ final episodes = record.episodes.values;+ final totalCount = episodes.length;+ final completedCount =+ episodes.where((e) => e.status == DownloadStatus.completed).length;+ final activeCount = episodes+ .where((e) =>+ e.status == DownloadStatus.downloading ||+ e.status == DownloadStatus.resolving ||+ e.status == DownloadStatus.pending)+ .length;+ final aggregateProgress = totalCount == 0+ ? 0.0+ : episodes.fold<double>(+ 0,+ (sum, e) =>+ sum ++ (e.status == DownloadStatus.completed+ ? 1.0+ : e.progressPercent),+ ) /+ totalCount;+ final allCompleted = completedCount >= totalCount;++ var meta = '$completedCount/$totalCount 已完成';+ if (activeCount > 0) {+ meta += ' · $activeCount 项进行中';+ if (totalSpeed > 0) {+ meta += ' · ${formatSpeed(totalSpeed)}';+ }+ }++ return Card(+ elevation: 0,+ margin: const EdgeInsets.only(bottom: 12),+ color: colorScheme.surfaceContainerLow,+ shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),+ clipBehavior: Clip.antiAlias,+ child: Column(+ crossAxisAlignment: CrossAxisAlignment.stretch,+ children: [+ InkWell(+ onTap: onToggle,+ child: Padding(+ padding: const EdgeInsets.fromLTRB(16, 16, 8, 16),+ child: Row(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ NetworkImgLayer(+ src: record.bangumiCover,+ width: 56,+ height: 75,+ ),+ const SizedBox(width: 16),+ Expanded(+ child: Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ Text(+ record.bangumiName,+ style: textTheme.titleMedium+ ?.copyWith(fontWeight: FontWeight.w700),+ maxLines: 2,+ overflow: TextOverflow.ellipsis,+ ),+ const SizedBox(height: 6),+ Wrap(+ spacing: 6,+ runSpacing: 4,+ crossAxisAlignment: WrapCrossAlignment.center,+ children: [+ RuleTag(+ label: record.pluginName,+ background: colorScheme.secondaryContainer,+ foreground: colorScheme.onSecondaryContainer,+ ),+ Text(+ meta,+ style: textTheme.bodySmall?.copyWith(+ color: colorScheme.onSurfaceVariant,+ ),+ ),+ ],+ ),+ ],+ ),+ ),+ PopupMenuButton<String>(+ icon: Icon(+ Icons.more_vert,+ color: colorScheme.onSurfaceVariant,+ ),+ tooltip: '更多操作',+ onSelected: (value) {+ if (value == 'resume_all') {+ onResumeAll();+ } else if (value == 'delete') {+ onDeleteAll();+ }+ },+ itemBuilder: (context) => [+ const PopupMenuItem(+ value: 'resume_all',+ child: Text('全部开始'),+ ),+ PopupMenuItem(+ value: 'delete',+ child: Text(+ '全部删除',+ style: TextStyle(color: colorScheme.error),+ ),+ ),+ ],+ ),+ Padding(+ padding: const EdgeInsets.all(8),+ child: AnimatedRotation(+ turns: expanded ? 0.5 : 0,+ duration: _kExpandDuration,+ curve: _kExpandCurve,+ child: Icon(+ Icons.expand_more,+ color: colorScheme.onSurfaceVariant,+ ),+ ),+ ),+ ],+ ),+ ),+ ),+ if (!allCompleted)+ Padding(+ padding: EdgeInsets.fromLTRB(16, 0, 16, expanded ? 4 : 16),+ child: ClipRRect(+ borderRadius: BorderRadius.circular(2),+ child: LinearProgressIndicator(+ value: aggregateProgress,+ minHeight: 4,+ backgroundColor: colorScheme.surfaceContainerHighest,+ ),+ ),+ ),+ AnimatedSize(+ duration: _kExpandDuration,+ curve: _kExpandCurve,+ alignment: Alignment.topCenter,+ child: expanded+ ? Padding(+ padding: const EdgeInsets.fromLTRB(12, 0, 12, 8),+ child: Column(children: episodeTileBuilder()),+ )+ : const SizedBox(width: double.infinity),+ ),+ ],+ ),+ );+ }+}++/// One episode row inside [DownloadRecordCard]: tonal status badge,+/// episode name, status line, optional full-width progress bar and actions.+class DownloadEpisodeTile extends StatelessWidget {+ const DownloadEpisodeTile({+ super.key,+ required this.episode,+ required this.statusText,+ this.actions = const [],+ this.onPlay,+ });++ final DownloadEpisode episode;+ final String statusText;+ final List<Widget> actions;++ /// Non-null only for completed episodes; makes the whole row tappable.+ final VoidCallback? onPlay;++ @override+ Widget build(BuildContext context) {+ final colorScheme = Theme.of(context).colorScheme;+ final textTheme = Theme.of(context).textTheme;+ final isError = episode.status == DownloadStatus.failed;+ final showProgress = episode.status == DownloadStatus.downloading ||+ (episode.status == DownloadStatus.paused &&+ episode.progressPercent > 0);++ return InkWell(+ borderRadius: BorderRadius.circular(12),+ onTap: onPlay,+ child: Padding(+ padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),+ child: Row(+ children: [+ _EpisodeStatusBadge(episode: episode),+ const SizedBox(width: 12),+ Expanded(+ child: Column(+ crossAxisAlignment: CrossAxisAlignment.start,+ children: [+ Text(+ episode.episodeName.isNotEmpty+ ? episode.episodeName+ : '第${episode.episodeNumber}集',+ style: textTheme.bodyMedium,+ maxLines: 1,+ overflow: TextOverflow.ellipsis,+ ),+ const SizedBox(height: 2),+ Text(+ statusText,+ style: textTheme.bodySmall?.copyWith(+ color: isError+ ? colorScheme.error+ : colorScheme.onSurfaceVariant,+ ),+ maxLines: 1,+ overflow: TextOverflow.ellipsis,+ ),+ if (showProgress) ...[+ const SizedBox(height: 6),+ ClipRRect(+ borderRadius: BorderRadius.circular(2),+ child: LinearProgressIndicator(+ value: episode.progressPercent,+ minHeight: 4,+ backgroundColor: colorScheme.surfaceContainerHighest,+ ),+ ),+ ],+ ],+ ),+ ),+ const SizedBox(width: 8),+ ...actions,+ ],+ ),+ ),+ );+ }+}++/// Circular tonal badge showing the episode download status.+class _EpisodeStatusBadge extends StatelessWidget {+ const _EpisodeStatusBadge({required this.episode});++ final DownloadEpisode episode;++ @override+ Widget build(BuildContext context) {+ final colorScheme = Theme.of(context).colorScheme;++ Color background;+ Widget child;+ switch (episode.status) {+ case DownloadStatus.completed:+ background = colorScheme.secondaryContainer;+ child = Icon(+ Icons.check_rounded,+ size: 18,+ color: colorScheme.onSecondaryContainer,+ );+ break;+ case DownloadStatus.downloading:+ background = Colors.transparent;+ child = SizedBox(+ width: 22,+ height: 22,+ child: CircularProgressIndicator(+ value: episode.progressPercent,+ strokeWidth: 2.5,+ backgroundColor: colorScheme.surfaceContainerHighest,+ ),+ );+ break;+ case DownloadStatus.resolving:+ background = Colors.transparent;+ child = const SizedBox(+ width: 22,+ height: 22,+ child: CircularProgressIndicator(strokeWidth: 2.5),+ );+ break;+ case DownloadStatus.failed:+ background = colorScheme.errorContainer;+ child = Icon(+ Icons.error_outline_rounded,+ size: 18,+ color: colorScheme.onErrorContainer,+ );+ break;+ case DownloadStatus.paused:+ background = colorScheme.surfaceContainerHighest;+ child = Icon(+ Icons.pause_rounded,+ size: 18,+ color: colorScheme.onSurfaceVariant,+ );+ break;+ case DownloadStatus.pending:+ default:+ background = colorScheme.surfaceContainerHighest;+ child = Icon(+ Icons.schedule_rounded,+ size: 18,+ color: colorScheme.onSurfaceVariant,+ );+ break;+ }++ return Container(+ width: 36,+ height: 36,+ decoration: BoxDecoration(color: background, shape: BoxShape.circle),+ child: Center(child: child),+ );+ }+}++/// Empty state for the download page, following the onboarding badge idiom.+class DownloadEmptyState extends StatelessWidget {+ const DownloadEmptyState({super.key});++ @override+ Widget build(BuildContext context) {+ final colorScheme = Theme.of(context).colorScheme;+ final textTheme = Theme.of(context).textTheme;++ return Column(+ mainAxisSize: MainAxisSize.min,+ children: [+ Container(+ width: 64,+ height: 64,+ decoration: BoxDecoration(+ color: colorScheme.secondaryContainer,+ shape: BoxShape.circle,+ ),+ child: Icon(+ Icons.download_rounded,+ size: 32,+ color: colorScheme.onSecondaryContainer,+ ),+ ),+ const SizedBox(height: 20),+ Text(+ '暂无下载内容',+ style: textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w700),+ ),+ const SizedBox(height: 8),+ Text(+ '缓存的番剧会显示在这里,随时离线观看',+ style: textTheme.bodySmall?.copyWith(+ color: colorScheme.onSurfaceVariant,+ ),+ ),+ ],+ );+ }+}