Files
Melo/lib/library/library_screen.dart
T
Dustin-Mike Jens HähnelandClaude Haiku 4.5 4fa8d12fa0 Fix critical data loss bugs and improve error handling
## Critical Fixes
- Fix CRITICAL-1: Empty scans no longer delete entire library
  - Use timestamp-based tombstone logic instead of path-based
  - Only mark missing if livePaths is not empty

- Fix CRITICAL-2: Re-scanned files now return from deleted state
  - Add deleted=false to SongsCompanion.insert in both scanners

## High Priority Fixes
- HIGH-1: Fix SQLite variable limit crash on large libraries
  - Replace isNotIn() with timestamp comparison (O(1) not O(n))

- HIGH-2: Fix UI freezing during scan
  - Throttle progress updates (every 50 files or end of scan)

- HIGH-3: Add error handling for scan failures
  - Wrap scanFolders/scanAndroidMediaStore in try/catch
  - Display scanError banner in LibraryScreen

- HIGH-4: Add error handling for playback failures
  - Wrap loadPlaylist in try/catch

## Medium Priority Fixes
- MEDIUM-1: Replace ! with ?? to handle unknown ProcessingState
- MEDIUM-2: Escape LIKE wildcards in search queries
- MEDIUM-4: Display StreamBuilder errors instead of treating as empty
- MEDIUM-8: Add tooltips to all IconButtons for accessibility
- Add doc comments to public database APIs

## Testing
- Add regression tests for CRITICAL-1 and CRITICAL-2
- All 9 existing tests pass

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-08-16 19:04:54 +02:00

121 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'database.dart';
import 'library_service.dart';
import 'permissions.dart';
import 'song_list.dart';
class LibraryScreen extends StatelessWidget {
const LibraryScreen({super.key});
@override
Widget build(BuildContext context) {
final db = context.read<MeloDb>();
final lib = context.watch<LibraryService>();
return Scaffold(
appBar: AppBar(
title: const Text('Bibliothek'),
actions: [
IconButton(
tooltip: addMusicLabel,
icon: const Icon(Icons.create_new_folder_outlined),
onPressed: lib.scanning ? null : lib.pickFolderAndScan,
),
IconButton(
tooltip: 'Erneut scannen',
icon: const Icon(Icons.refresh),
onPressed: lib.scanning ? null : lib.rescan,
),
],
),
body: Column(
children: [
if (lib.permissionDenied)
MaterialBanner(
backgroundColor: Colors.red.shade900,
content: const Text('Zugriff auf Musik verweigert. '
'Bitte in den Einstellungen erlauben.'),
actions: [
TextButton(
onPressed: openMusicPermissionSettings,
child: const Text('Einstellungen'),
),
],
),
if (lib.scanError != null)
MaterialBanner(
backgroundColor: Colors.red.shade900,
content: Text(lib.scanError!),
actions: [
TextButton(
onPressed: () => lib.scanError = null,
child: const Text('Verwerfen'),
),
],
),
if (lib.scanning)
Padding(
padding: const EdgeInsets.all(12),
child: Column(
children: [
const LinearProgressIndicator(),
const SizedBox(height: 6),
Text(
'Scanne … ${lib.scanDone}'
'${lib.scanTotal > 0 ? ' / ${lib.scanTotal}' : ''}',
style: const TextStyle(color: Colors.white54, fontSize: 12),
),
],
),
),
Expanded(
child: StreamBuilder<List<Song>>(
stream: db.watchSongs(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text('Fehler: ${snapshot.error}',
style: const TextStyle(color: Colors.white54)),
);
}
final songs = snapshot.data ?? const [];
if (songs.isEmpty && !lib.scanning) {
return const _Empty();
}
return SongList(songs);
},
),
),
],
),
);
}
}
class _Empty extends StatelessWidget {
const _Empty();
@override
Widget build(BuildContext context) {
final lib = context.read<LibraryService>();
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.library_music, size: 64, color: Colors.white24),
const SizedBox(height: 12),
const Text('Noch keine Musik',
style: TextStyle(color: Colors.white54)),
const SizedBox(height: 16),
FilledButton.icon(
icon: const Icon(Icons.create_new_folder_outlined),
label: Text(addMusicLabel),
onPressed: lib.pickFolderAndScan,
),
],
),
);
}
}