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/art_image.dart'; import '../widgets/hairline_panel.dart'; import '../widgets/toast.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; // Ordered play list — index i here matches the i-th rendered saved row. final savedSongs = completed.map((d) => d.song).toList(); 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)}', action: Row( mainAxisSize: MainAxisSize.min, children: [ InkWell( onTap: savedSongs.isEmpty ? null : () => playback.playSongs(savedSongs), child: Padding( padding: const EdgeInsets.all(TimbreSpacing.xs), child: Icon( Icons.play_arrow, size: 18, color: TimbreColors.dimmed, ), ), ), InkWell( onTap: savedSongs.isEmpty ? null : () { playback.toggleShuffle(); playback.playSongs(savedSongs); }, child: Padding( padding: const EdgeInsets.all(TimbreSpacing.xs), child: Icon( Icons.shuffle, size: 18, color: TimbreColors.dimmed, ), ), ), ], ), 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 (i, d) in completed.indexed) _SavedRow( info: d, artUri: resolveArtUriW( ref, coverArt: d.song.coverArt, size: 128, )?.toString(), onPlay: () => playback.playSongs( savedSongs, startIndex: i, ), onPlayNext: () => playback.playNext(d.song), onAddToQueue: () => playback.addToQueue(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.artUri, required this.onPlay, required this.onPlayNext, required this.onAddToQueue, required this.onRemove, }); final DownloadInfo info; /// Resolved cover-art URI (downloaded art is local, so it shows offline). final String? artUri; final VoidCallback onPlay; final VoidCallback onPlayNext; final VoidCallback onAddToQueue; 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: [ ArtImage( artUri, width: 40, height: 40, fit: BoxFit.cover, borderRadius: BorderRadius.circular(4), ), const SizedBox(width: TimbreSpacing.md), 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), ), ], ), ), PopupMenuButton( icon: Icon(Icons.more_vert, size: 20, color: TimbreColors.dimmed), color: TimbreColors.surface, onSelected: (v) { switch (v) { case 'next': onPlayNext(); showToast(context, 'Playing next', icon: Icons.check); case 'queue': onAddToQueue(); showToast(context, 'Added to queue', icon: Icons.check); case 'remove': onRemove(); } }, itemBuilder: (_) => [ const PopupMenuItem(value: 'next', child: Text('Play next')), const PopupMenuItem( value: 'queue', child: Text('Add to queue'), ), const PopupMenuItem( value: 'remove', child: Text('Remove download'), ), ], ), ], ), ), ); } } 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]}'; }