import 'dart:async'; import 'package:flutter/material.dart'; import '../api_client.dart'; import '../controller.dart'; import '../i18n.dart'; import '../theme.dart'; // --------------------------------------------------------------------------- // 下载页(从原 pages.dart 拆出,公开类名不变) // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // 下载页 // --------------------------------------------------------------------------- class DownloadPageView extends StatefulWidget { const DownloadPageView({ super.key, required this.controller, this.initialSources, this.initialIndex, this.initialJobs, }); final ShellController controller; /// 预置数据(仅用于测试 / 截图,正常运行时为 null,由后端提供)。 final List? initialSources; final SourceIndexInfo? initialIndex; final List? initialJobs; @override State createState() => _DownloadPageViewState(); } class _DownloadPageViewState extends State { List _sources = const []; SourceIndexInfo? _index; List _jobs = const []; String? _selectedSourceId; bool _loading = true; String? _error; Timer? _poll; /// 页面是否可见(被切走后仍保活,但没必要继续请求后端)。 bool _visible = true; /// 上一次轮询时是否有活跃任务,用来判断「刚刚全部结束」。 bool _hadActive = false; @override void initState() { super.initState(); final presetSources = widget.initialSources; if (presetSources != null) { _sources = presetSources; _index = widget.initialIndex; _jobs = widget.initialJobs ?? const []; _selectedSourceId = presetSources.isEmpty ? null : presetSources.first.id; _loading = false; return; } _load(); _schedulePoll(const Duration(seconds: 2)); } @override void didChangeDependencies() { super.didChangeDependencies(); // TickerMode 由外壳的 Offstage 控制:切到别的页时变 false。 _visible = TickerMode.of(context); } @override void dispose() { _poll?.cancel(); super.dispose(); } /// 单次 Timer 链:有活跃任务时 1 秒一次,空闲时降到 5 秒,不可见时 10 秒。 /// /// 以前是 `Timer.periodic(1s)` 且永不停止 —— 只要进过下载页,之后每秒都 /// 在请求 `/api/download/jobs`,哪怕没有任何任务、页面也早就切走了。 void _schedulePoll(Duration delay) { _poll?.cancel(); _poll = Timer(delay, _pollOnce); } Future _pollOnce() async { if (!mounted) return; if (!_visible) { _schedulePoll(const Duration(seconds: 10)); return; } _hadActive = _jobs.any((job) => job.isActive); await _refreshJobs(); if (!mounted) return; final active = _jobs.any((job) => job.isActive); // 活跃 → 空闲:这一轮里下载结束了,首页的模型列表已经过期。 if (_hadActive && !active) widget.controller.modelsChanged(); _hadActive = active; _schedulePoll(Duration(seconds: active ? 1 : 5)); } Future _load() async { setState(() { _loading = true; _error = null; }); try { // 三个请求互不依赖,一起发出去: // 源列表(本地,几乎即时)、任务列表、模型索引(要打远端,最慢) // 以前是 sources → models → jobs 串行等三轮,进页面白等多一秒。 final sourcesFuture = widget.controller.api.sources(); final jobsFuture = widget.controller.api.jobs(); final indexFuture = widget.controller.api.remoteModels( sourceId: _selectedSourceId, ); final sources = await sourcesFuture; final preferred = sources.where((s) => s.enabled).toList(); final selected = _selectedSourceId ?? (preferred.isNotEmpty ? preferred.first.id : (sources.isNotEmpty ? sources.first.id : '')); // 源列表是本地的,先画出来:右侧模型列表还在等远端索引,左侧不必陪着转圈 if (mounted) { setState(() { _sources = sources; _selectedSourceId = selected; }); } final index = await indexFuture; final jobs = await jobsFuture; if (!mounted) return; setState(() { _sources = sources; _selectedSourceId = selected; _index = index; _jobs = jobs; _loading = false; }); } catch (error) { if (!mounted) return; setState(() { _error = error.toString(); _loading = false; }); } } Future _refreshJobs() async { try { final jobs = await widget.controller.api.jobs(); if (!mounted) return; setState(() => _jobs = jobs); } catch (_) { // 轮询失败不打扰用户,下一次再试 } } Future _selectSource(String id) async { setState(() { _selectedSourceId = id; _loading = true; _error = null; }); try { final index = await widget.controller.api.remoteModels(sourceId: id); if (!mounted) return; setState(() { _index = index; _loading = false; }); } catch (error) { if (!mounted) return; setState(() { _error = error.toString(); _loading = false; }); } } Future _download(RemoteModelInfo model) async { final sourceId = _selectedSourceId; if (sourceId == null) return; try { await widget.controller.api.startDownload(sourceId, model.id); await _refreshJobs(); } catch (error) { if (!mounted) return; setState(() => _error = error.toString()); } } Future _jobAction( Future Function(String id) action, String id) async { try { await action(id); await _refreshJobs(); } catch (error) { if (!mounted) return; setState(() => _error = error.toString()); } } @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.fromLTRB( GcSpace.xxl, GcSpace.xxl, GcSpace.xxl, GcSpace.xl), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox(width: 268, child: _sourcePanel()), const SizedBox(width: GcSpace.xl), Expanded(child: _modelPanel()), ], ), ); } Widget _sourcePanel() { return GcCard( padding: const EdgeInsets.all(GcSpace.lg), child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ GcSectionTitle(T('dl.sources')), const SizedBox(height: GcSpace.xs + 2), GcHint(T('dl.subtitle')), const SizedBox(height: GcSpace.md), for (final source in _sources) Padding( padding: const EdgeInsets.only(bottom: GcSpace.xs + 2), child: _sourceTile(source), ), const SizedBox(height: GcSpace.sm), SizedBox( width: double.infinity, child: OutlinedButton.icon( onPressed: _load, icon: const Icon(Icons.refresh_rounded, size: 18), label: Text(T('dl.refresh')), ), ), ], ), ); } Widget _sourceTile(SourceInfo source) { final selected = source.id == _selectedSourceId; return Material( color: selected ? GcColors.accentSoft : Colors.transparent, borderRadius: BorderRadius.circular(GcRadius.control), child: InkWell( borderRadius: BorderRadius.circular(GcRadius.control), hoverColor: GcColors.surfaceAlt, onTap: () => _selectSource(source.id), child: Container( padding: const EdgeInsets.symmetric( horizontal: GcSpace.md, vertical: GcSpace.md - 2, ), decoration: BoxDecoration( borderRadius: BorderRadius.circular(GcRadius.control), border: Border.all( color: selected ? GcColors.accent.withOpacity(0.45) : Colors.transparent, ), ), child: Row( children: [ Icon( selected ? Icons.radio_button_checked_rounded : Icons.radio_button_unchecked_rounded, size: 16, color: selected ? GcColors.accent : GcColors.ghost, ), const SizedBox(width: GcSpace.sm + 2), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Text( source.name, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 13, height: 1.35, fontWeight: FontWeight.w600, color: selected ? GcColors.accent : GcColors.ink, ), ), const SizedBox(height: 1), Text( source.host, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 11.5, height: 1.35, color: GcColors.faint, ), ), ], ), ), if (!source.enabled) const GcPill('off', subtle: true), ], ), ), ), ); } Widget _modelPanel() { final models = _index?.models ?? const []; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ GcPageHeader( title: T('dl.available'), subtitle: _index == null ? null : (_index!.fromCache ? '${T('dl.ready', [ models.length ])} · ${T('dl.fromCache')}' : T('dl.ready', [models.length])), trailing: IconButton( tooltip: T('common.refresh'), onPressed: _load, icon: const Icon(Icons.refresh_rounded, size: 18), ), ), const SizedBox(height: GcSpace.lg), Expanded( child: ListView( padding: const EdgeInsets.only(bottom: GcSpace.sm), children: [ if (_loading) const Padding( padding: EdgeInsets.symmetric(vertical: 48), child: Center( child: SizedBox( width: 22, height: 22, child: CircularProgressIndicator(strokeWidth: 2), ), ), ) else if (_error != null) GcMessage( title: T('common.error'), message: _error, icon: Icons.cloud_off_rounded, color: GcColors.danger, action: OutlinedButton( onPressed: _load, child: Text(T('common.retry')), ), ) else if (models.isEmpty) GcMessage( title: T('dl.empty'), message: T('dl.emptyHint'), icon: Icons.inbox_rounded, ) else for (final model in models) Padding( padding: const EdgeInsets.only(bottom: GcSpace.md), child: _remoteRow(model), ), const SizedBox(height: GcSpace.sm), _jobsPanel(), ], ), ), ], ); } Widget _remoteRow(RemoteModelInfo model) { return GcCard( padding: const EdgeInsets.fromLTRB( GcSpace.lg, GcSpace.md + 2, GcSpace.lg, GcSpace.md + 2, ), child: Row( children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Flexible( child: Text( model.name, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 14, height: 1.35, fontWeight: FontWeight.w600, color: GcColors.ink, ), ), ), const SizedBox(width: GcSpace.sm), Text( 'v${model.version} · ${model.sizeText}', style: TextStyle( fontSize: 12, height: 1.35, color: GcColors.faint, ), ), if (model.installed) ...[ const SizedBox(width: GcSpace.sm), GcPill(T('dl.installed'), color: GcColors.ok), ], ], ), const SizedBox(height: GcSpace.xs + 1), Text( model.description, maxLines: 2, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 12.5, height: 1.5, color: GcColors.muted, ), ), ], ), ), const SizedBox(width: GcSpace.lg), _downloadAction(model), ], ), ); } /// 该模型是否有进行中的下载任务。 JobInfo? _activeJobFor(String modelId) { for (final job in _jobs) { if (job.isActive && job.modelId == modelId) return job; } return null; } /// 按钮位:空闲时是「下载 / 重新下载」,下载中就变成这一行的进度条。 Widget _downloadAction(RemoteModelInfo model) { final job = _activeJobFor(model.id); if (job == null) { if (model.installed) { return OutlinedButton( onPressed: () => _download(model), child: Text(T('dl.reinstall')), ); } return FilledButton( onPressed: () => _download(model), child: Text(T('dl.download')), ); } final value = job.progress.clamp(0.0, 1.0).toDouble(); final percent = (value * 100).round(); final detail = job.detailText.isNotEmpty ? job.detailText : job.statusText; return SizedBox( width: 148, child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Row( children: [ Text( '$percent%', style: TextStyle( fontSize: 12.5, height: 1.2, fontWeight: FontWeight.w600, color: GcColors.accent, ), ), const SizedBox(width: GcSpace.xs + 2), if (detail.isNotEmpty) Expanded( child: Text( detail, maxLines: 1, overflow: TextOverflow.ellipsis, textAlign: TextAlign.right, style: TextStyle( fontSize: 11, height: 1.2, color: GcColors.faint, ), ), ), TextButton( onPressed: () => _jobAction(widget.controller.api.cancelJob, job.id), style: TextButton.styleFrom( minimumSize: const Size(0, 22), padding: const EdgeInsets.symmetric(horizontal: GcSpace.sm), tapTargetSize: MaterialTapTargetSize.shrinkWrap, ), child: Text( T('dl.cancel'), style: const TextStyle(fontSize: 11.5), ), ), ], ), const SizedBox(height: 3), ClipRRect( borderRadius: BorderRadius.circular(3), child: LinearProgressIndicator( // 还没拿到进度时走不确定态,避免显示 0% 让人以为卡住 value: value > 0 ? value : null, minHeight: 6, backgroundColor: GcColors.surfaceAlt, ), ), ], ), ); } Widget _jobsPanel() { return GcCard( padding: const EdgeInsets.all(GcSpace.lg), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ GcSectionTitle( T('dl.background'), trailing: TextButton( onPressed: () async { try { await widget.controller.api.clearFinished(); await _refreshJobs(); } catch (_) { // 忽略 } }, child: Text(T('dl.clearDone')), ), ), const SizedBox(height: GcSpace.md), if (_jobs.isEmpty) GcHint(T('dl.bgEmpty')) else for (final job in _jobs) Padding( padding: const EdgeInsets.only(bottom: GcSpace.md), child: _jobRow(job), ), ], ), ); } Widget _jobRow(JobInfo job) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Expanded( child: Text( '${job.name} v${job.version}', maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 13, height: 1.35, fontWeight: FontWeight.w600, color: GcColors.ink, ), ), ), GcPill( _stateLabel(job.state), color: job.state == 'Failed' ? GcColors.danger : job.state == 'Completed' ? GcColors.ok : GcColors.accent, ), const SizedBox(width: GcSpace.sm), if (job.isActive) TextButton( onPressed: () => _jobAction(widget.controller.api.cancelJob, job.id), child: Text(T('dl.cancel')), ) else if (job.canRetry) TextButton( onPressed: () => _jobAction(widget.controller.api.retryJob, job.id), child: Text(T('dl.retry')), ) else TextButton( onPressed: () => _jobAction(widget.controller.api.removeJob, job.id), child: Text(T('dl.removeTask')), ), ], ), const SizedBox(height: GcSpace.sm - 2), ClipRRect( borderRadius: BorderRadius.circular(2), child: LinearProgressIndicator( value: job.progress.clamp(0, 1).toDouble(), minHeight: 4, backgroundColor: GcColors.surfaceAlt, ), ), const SizedBox(height: GcSpace.xs + 1), Text( job.error ?? job.detailText, style: TextStyle( fontSize: 11.5, height: 1.4, color: GcColors.faint, ), ), ], ); } String _stateLabel(String state) { switch (state) { case 'Queued': return T('dl.status.queued'); case 'Running': case 'Downloading': return T('dl.status.downloading'); case 'Completed': return T('dl.status.done'); case 'Failed': return T('dl.status.failed'); case 'Canceled': return T('dl.status.canceled'); default: return state; } } }