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>
This commit is contained in:
Dustin-Mike Jens Hähnel
2026-08-16 19:04:54 +02:00
co-authored by Claude Haiku 4.5
parent b5ab7bd9dd
commit 4fa8d12fa0
11 changed files with 156 additions and 30 deletions
+6
View File
@@ -20,6 +20,12 @@ class HomeScreen extends StatelessWidget {
body: StreamBuilder<List<Song>>(
stream: db.watchRecent(),
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) return const _Welcome();
return Column(
+17 -7
View File
@@ -44,11 +44,15 @@ Future<int> scanAndroidMediaStore(
var coverPath = prev?.coverPath;
if (coverPath == null) {
final art = await audioQuery.queryArtwork(s.id, ArtworkType.AUDIO, size: 512);
if (art != null && art.isNotEmpty) {
final f = File(p.join(covers.path, '$id.img'));
await f.writeAsBytes(art);
coverPath = f.path;
try {
final art = await audioQuery.queryArtwork(s.id, ArtworkType.AUDIO, size: 512);
if (art != null && art.isNotEmpty) {
final f = File(p.join(covers.path, '$id.img'));
await f.writeAsBytes(art);
coverPath = f.path;
}
} catch (_) {
// Kaputtes Cover verzögert nicht den Scan.
}
}
@@ -63,11 +67,17 @@ Future<int> scanAndroidMediaStore(
dateAddedMs: prev?.dateAddedMs ??
(s.dateAdded != null ? s.dateAdded! * 1000 : now),
updatedAtMs: now,
deleted: const Value(false),
));
onProgress?.call(++done, songs.length);
++done;
if (done % 50 == 0 || done == songs.length) {
onProgress?.call(done, songs.length);
}
}
await db.upsertSongs(companions);
await db.markMissing(livePaths, now);
if (livePaths.isNotEmpty) {
await db.markMissing(now);
}
return companions.length;
}
+24 -12
View File
@@ -43,6 +43,7 @@ class MeloDb extends _$MeloDb {
@override
int get schemaVersion => 1;
/// Überwacht alle Songs (nicht getombstonte). Nach Titel sortiert.
Stream<List<Song>> watchSongs() {
return (select(songs)
..where((s) => s.deleted.equals(false))
@@ -50,6 +51,7 @@ class MeloDb extends _$MeloDb {
.watch();
}
/// Überwacht die zuletzt hinzugefügten Songs (nicht getombstonte).
Stream<List<Song>> watchRecent({int limit = 50}) {
return (select(songs)
..where((s) => s.deleted.equals(false))
@@ -60,33 +62,43 @@ class MeloDb extends _$MeloDb {
.watch();
}
/// Sucht Songs nach Titel, Künstler oder Album. Wildcards werden escaped.
Stream<List<Song>> searchSongs(String query) {
final like = '%${query.toLowerCase()}%';
final escaped = query
.toLowerCase()
.replaceAll('\\', '\\\\')
.replaceAll('%', '\\%')
.replaceAll('_', '\\_');
final like = '%$escaped%';
return (select(songs)
..where((s) =>
s.deleted.equals(false) &
(s.title.lower().like(like) |
s.artist.lower().like(like) |
s.album.lower().like(like)))
(s.title.lower().like(like, escapeChar: '\\') |
s.artist.lower().like(like, escapeChar: '\\') |
s.album.lower().like(like, escapeChar: '\\')))
..orderBy([(s) => OrderingTerm(expression: s.title)]))
.watch();
}
/// Alle Songs inkl. getombstonte. Intern für ID-Stabilität beim Scan.
Future<List<Song>> allSongs() => select(songs).get();
/// Upsert Songs nach Pfad. Neue Songs bekommen UUID; bekannte erhalten sie zurück.
/// Setzt [updatedAtMs] auf die Werte der Companions (normalerweise Scan-Startzeit).
Future<void> upsertSongs(List<SongsCompanion> items) async {
await batch((b) => b.insertAllOnConflictUpdate(songs, items));
}
/// Tombstone für Songs, deren Datei beim Scan nicht mehr gefunden wurde.
Future<void> markMissing(List<String> livePaths, int now) async {
final q = update(songs)..where((s) => s.deleted.equals(false));
if (livePaths.isNotEmpty) {
q.where((s) => s.path.isNotIn(livePaths));
}
await q.write(
SongsCompanion(deleted: const Value(true), updatedAtMs: Value(now)),
);
/// Löscht nur Songs, die in diesem Scan (Zeitstempel [now]) nicht aktualisiert wurden.
Future<void> markMissing(int now) async {
await (update(songs)
..where((s) =>
s.deleted.equals(false) &
s.updatedAtMs.isSmallerThanValue(now)))
.write(
SongsCompanion(deleted: const Value(true), updatedAtMs: Value(now)),
);
}
Stream<List<Folder>> watchFolders() =>
+17
View File
@@ -43,6 +43,17 @@ class LibraryScreen extends StatelessWidget {
),
],
),
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),
@@ -62,6 +73,12 @@ class LibraryScreen extends StatelessWidget {
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();
+4
View File
@@ -25,6 +25,7 @@ class LibraryService extends ChangeNotifier {
int scanDone = 0;
int scanTotal = 0;
bool permissionDenied = false;
String? scanError;
/// Auf Android: automatischer Geräte-Scan. Auf Desktop: Ordner wählen + scannen.
Future<void> pickFolderAndScan() async {
@@ -61,6 +62,7 @@ class LibraryService extends ChangeNotifier {
scanning = true;
scanDone = 0;
scanTotal = 0;
scanError = null;
notifyListeners();
try {
if (Platform.isAndroid) {
@@ -69,6 +71,8 @@ class LibraryService extends ChangeNotifier {
final folders = (await db.activeFolders()).map((f) => f.path).toList();
await scanFolders(db, folders, onProgress: _onProgress);
}
} catch (e) {
scanError = 'Scan fehlgeschlagen: $e';
} finally {
scanning = false;
notifyListeners();
+15 -5
View File
@@ -68,9 +68,13 @@ Future<int> scanFolders(
var coverPath = prev?.coverPath;
final pics = meta?.pictures ?? const <Picture>[];
if (pics.isNotEmpty && coverPath == null) {
final f = File(p.join(covers.path, '$id.img'));
await f.writeAsBytes(pics.first.bytes);
coverPath = f.path;
try {
final f = File(p.join(covers.path, '$id.img'));
await f.writeAsBytes(pics.first.bytes);
coverPath = f.path;
} catch (_) {
// Kaputtes Cover verzögert nicht den Scan.
}
}
companions.add(SongsCompanion.insert(
@@ -83,11 +87,17 @@ Future<int> scanFolders(
coverPath: Value(coverPath),
dateAddedMs: prev?.dateAddedMs ?? now,
updatedAtMs: now,
deleted: const Value(false),
));
onProgress?.call(++done, files.length);
++done;
if (done % 50 == 0 || done == files.length) {
onProgress?.call(done, files.length);
}
}
await db.upsertSongs(companions);
await db.markMissing(livePaths, now);
if (livePaths.isNotEmpty) {
await db.markMissing(now);
}
return companions.length;
}
+6
View File
@@ -37,6 +37,12 @@ class _SearchScreenState extends State<SearchScreen> {
: StreamBuilder<List<Song>>(
stream: db.searchSongs(_query),
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) {
return const Center(
+8 -3
View File
@@ -34,8 +34,12 @@ class MeloAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
final sources = items
.map((item) => AudioSource.uri(Uri.parse(item.id), tag: item))
.toList();
await _player.setAudioSources(sources, initialIndex: startIndex);
play();
try {
await _player.setAudioSources(sources, initialIndex: startIndex);
await play();
} catch (e) {
playbackState.addError(e);
}
}
@override
@@ -97,7 +101,8 @@ class MeloAudioHandler extends BaseAudioHandler with QueueHandler, SeekHandler {
ProcessingState.buffering: AudioProcessingState.buffering,
ProcessingState.ready: AudioProcessingState.ready,
ProcessingState.completed: AudioProcessingState.completed,
}[_player.processingState]!,
}[_player.processingState] ??
AudioProcessingState.idle,
playing: _player.playing,
updatePosition: _player.position,
bufferedPosition: _player.bufferedPosition,
+1
View File
@@ -56,6 +56,7 @@ class MiniPlayer extends StatelessWidget {
builder: (context, snap) {
final playing = snap.data?.playing ?? false;
return IconButton(
tooltip: playing ? 'Pause' : 'Abspielen',
icon: Icon(playing ? Icons.pause : Icons.play_arrow),
onPressed: playing ? handler.pause : handler.play,
);
+7
View File
@@ -98,6 +98,8 @@ class _ProgressBar extends StatelessWidget {
Slider(
value: valueMs,
max: maxMs,
semanticFormatterCallback: (v) =>
_fmt(Duration(milliseconds: v.round())),
onChanged: hasDuration
? (v) => handler.seek(Duration(milliseconds: v.round()))
: null,
@@ -146,6 +148,7 @@ class _Controls extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
tooltip: 'Zufallswiedergabe',
icon: Icon(Icons.shuffle,
color: shuffle ? MeloTheme.red : Colors.white54),
onPressed: () => handler.setShuffleMode(shuffle
@@ -153,22 +156,26 @@ class _Controls extends StatelessWidget {
: AudioServiceShuffleMode.all),
),
IconButton(
tooltip: 'Vorheriger Titel',
iconSize: 40,
icon: const Icon(Icons.skip_previous),
onPressed: handler.skipToPrevious,
),
IconButton(
tooltip: playing ? 'Pause' : 'Abspielen',
iconSize: 64,
icon: Icon(playing ? Icons.pause_circle : Icons.play_circle,
color: MeloTheme.red),
onPressed: playing ? handler.pause : handler.play,
),
IconButton(
tooltip: 'Nächster Titel',
iconSize: 40,
icon: const Icon(Icons.skip_next),
onPressed: handler.skipToNext,
),
IconButton(
tooltip: 'Wiederholen',
icon: Icon(
repeat == AudioServiceRepeatMode.one
? Icons.repeat_one