This commit is contained in:
Forrest 2026-07-29 14:14:18 -04:00
commit d205277cdd
182 changed files with 22978 additions and 0 deletions

View file

@ -0,0 +1,234 @@
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(playbackProvider.notifier);
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: const Text('Clear all',
style: TextStyle(color: RatuneColors.dimmed)),
),
],
),
body: SafeArea(
child: (active.isEmpty && completed.isEmpty)
? const Center(
child: Text('No downloads yet.',
style: TextStyle(color: RatuneColors.dimmed)),
)
: ListView(
padding: const EdgeInsets.all(RatuneSpacing.lg),
children: [
if (active.isNotEmpty) ...[
HairlinePanel(
title: 'Downloading',
trailing: '(${active.length})',
padding: const EdgeInsets.symmetric(
vertical: RatuneSpacing.md),
child: Column(
children: [
for (final d in active) _ActiveRow(info: d),
],
),
),
const SizedBox(height: RatuneSpacing.xl),
],
HairlinePanel(
title: 'Saved',
active: true,
trailing: completed.isEmpty
? null
: '${completed.length} · ${_fmtBytes(downloads.totalBytes)}',
padding:
const EdgeInsets.symmetric(vertical: RatuneSpacing.md),
child: completed.isEmpty
? const Padding(
padding: EdgeInsets.all(RatuneSpacing.lg),
child: Text('Nothing saved for offline yet.',
style: TextStyle(color: RatuneColors.dimmed)),
)
: Column(
children: [
for (final d in completed)
_SavedRow(
info: d,
onPlay: () =>
playback.playSongs([d.song]),
onRemove: () => controller.remove(d.song.id),
),
],
),
),
],
),
),
);
}
Future<void> _confirmClear(
BuildContext context, DownloadController controller) async {
final ok = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
backgroundColor: RatuneColors.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: RatuneSpacing.lg, vertical: RatuneSpacing.xs),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(info.song.title ?? 'Untitled',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(color: RatuneColors.foreground)),
const SizedBox(height: RatuneSpacing.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: RatuneColors.border,
color: accent,
),
],
),
),
const SizedBox(width: RatuneSpacing.md),
Text(
failed
? '—'
: (info.status == DownloadStatus.queued ? 'Queued' : ''),
style: const TextStyle(color: RatuneColors.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: RatuneSpacing.minTouchTarget),
padding: const EdgeInsets.only(left: RatuneSpacing.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: const TextStyle(color: RatuneColors.foreground)),
Text(
[
info.song.artist,
'$quality · ${_fmtBytes(info.sizeBytes ?? 0)}',
].where((e) => e != null && e.isNotEmpty).join(' · '),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style:
const TextStyle(color: RatuneColors.dimmed, fontSize: 12),
),
],
),
),
InkWell(
onTap: onRemove,
customBorder: const CircleBorder(),
child: const SizedBox(
width: RatuneSpacing.minTouchTarget,
height: RatuneSpacing.minTouchTarget,
child: Icon(Icons.delete_outline,
size: 20, color: RatuneColors.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]}';
}