import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../downloads/download_manager.dart'; import '../state/providers.dart'; import '../theme/tokens.dart'; import '../widgets/hairline_panel.dart'; /// Manage offline downloads: what's saved, how much space it uses, and any /// in-flight transfers. Tapping a completed track plays it. class DownloadsScreen extends ConsumerWidget { const DownloadsScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final downloads = ref.watch(downloadManagerProvider); final controller = ref.read(downloadManagerProvider.notifier); final playback = ref.read(playbackCommandsProvider); final active = downloads.byId.values.where((d) => d.isActive).toList(); final completed = downloads.completed; return Scaffold( appBar: AppBar( title: const Text('Downloads', style: TextStyle(fontWeight: FontWeight.w700)), actions: [ if (completed.isNotEmpty) TextButton( onPressed: () => _confirmClear(context, controller), child: Text('Clear all', style: TextStyle(color: TimbreColors.dimmed)), ), ], ), body: SafeArea( child: (active.isEmpty && completed.isEmpty) ? Center( child: Text('No downloads yet.', style: TextStyle(color: TimbreColors.dimmed)), ) : ListView( padding: const EdgeInsets.all(TimbreSpacing.lg), children: [ if (active.isNotEmpty) ...[ HairlinePanel( title: 'Downloading', trailing: '(${active.length})', padding: const EdgeInsets.symmetric( vertical: TimbreSpacing.md), child: Column( children: [ for (final d in active) _ActiveRow(info: d), ], ), ), const SizedBox(height: TimbreSpacing.xl), ], HairlinePanel( title: 'Saved', active: true, trailing: completed.isEmpty ? null : '${completed.length} · ${_fmtBytes(downloads.totalBytes)}', padding: const EdgeInsets.symmetric(vertical: TimbreSpacing.md), child: completed.isEmpty ? Padding( padding: EdgeInsets.all(TimbreSpacing.lg), child: Text('Nothing saved for offline yet.', style: TextStyle(color: TimbreColors.dimmed)), ) : Column( children: [ for (final d in completed) _SavedRow( info: d, onPlay: () => playback.playSongs([d.song]), onRemove: () => controller.remove(d.song.id), ), ], ), ), ], ), ), ); } Future _confirmClear( BuildContext context, DownloadController controller) async { final ok = await showDialog( context: context, builder: (ctx) => AlertDialog( backgroundColor: TimbreColors.surface, title: const Text('Remove all downloads?'), content: const Text( 'This deletes every saved file for this server. It cannot be undone.'), actions: [ TextButton( onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')), TextButton( onPressed: () => Navigator.pop(ctx, true), child: const Text('Remove all')), ], ), ); if (ok == true) await controller.clearAll(); } } class _ActiveRow extends StatelessWidget { const _ActiveRow({required this.info}); final DownloadInfo info; @override Widget build(BuildContext context) { final accent = Theme.of(context).colorScheme.primary; final failed = info.status == DownloadStatus.failed; return Padding( padding: const EdgeInsets.symmetric( horizontal: TimbreSpacing.lg, vertical: TimbreSpacing.xs), child: Row( children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(info.song.title ?? 'Untitled', maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(color: TimbreColors.foreground)), const SizedBox(height: TimbreSpacing.xs), if (failed) const Text('Failed', style: TextStyle(color: Color(0xFFE06C75), fontSize: 12)) else LinearProgressIndicator( value: info.progress > 0 ? info.progress : null, minHeight: 3, backgroundColor: TimbreColors.border, color: accent, ), ], ), ), const SizedBox(width: TimbreSpacing.md), Text( failed ? '—' : (info.status == DownloadStatus.queued ? 'Queued' : ''), style: TextStyle(color: TimbreColors.dimmed, fontSize: 12), ), ], ), ); } } class _SavedRow extends StatelessWidget { const _SavedRow({ required this.info, required this.onPlay, required this.onRemove, }); final DownloadInfo info; final VoidCallback onPlay; final VoidCallback onRemove; @override Widget build(BuildContext context) { final quality = info.format ?? (info.bitRate != null ? '${info.bitRate} kbps' : 'Original'); return InkWell( onTap: onPlay, child: Container( constraints: const BoxConstraints(minHeight: TimbreSpacing.minTouchTarget), padding: const EdgeInsets.only(left: TimbreSpacing.lg), child: Row( children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text(info.song.title ?? 'Untitled', maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(color: TimbreColors.foreground)), Text( [ info.song.artist, '$quality · ${_fmtBytes(info.sizeBytes ?? 0)}', ].where((e) => e != null && e.isNotEmpty).join(' · '), maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(color: TimbreColors.dimmed, fontSize: 12), ), ], ), ), InkWell( onTap: onRemove, customBorder: const CircleBorder(), child: SizedBox( width: TimbreSpacing.minTouchTarget, height: TimbreSpacing.minTouchTarget, child: Icon(Icons.delete_outline, size: 20, color: TimbreColors.dimmed), ), ), ], ), ), ); } } String _fmtBytes(int bytes) { if (bytes <= 0) return '0 MB'; const units = ['B', 'KB', 'MB', 'GB']; var size = bytes.toDouble(); var i = 0; while (size >= 1024 && i < units.length - 1) { size /= 1024; i++; } return '${size.toStringAsFixed(size >= 10 || i == 0 ? 0 : 1)} ${units[i]}'; }