Files
Melo/lib/library/android_scan.dart
T
Dustin-Mike Jens HähnelandClaude Haiku 4.5 d04783073f Fix critical blocker issues and improve tests
## Critical Blockers Fixed
- Remove playbackState.addError() which broke audio_service permanently
  - Move error handling from audio_handler to song_list where UI context exists
  - Display error in SnackBar instead of destroying playback stream

- Add escapeChar parameter to LIKE queries for correct wildcard handling
  - Fixes broken search for titles with _ or % characters

- Add dismissScanError() method to allow closing error banner
  - Previously banner couldn't be dismissed (missing notifyListeners())

## Improvements
- Remove dead livePaths list, use companions.isNotEmpty instead
- Add initial progress update to show total files when scan starts
- Add tooltip to now_playing_screen Zurück button
- Add proper error handling in song_list with async/await

## Testing
- Add regression test for CRITICAL-1: empty scan doesn't delete songs
- Add regression test for CRITICAL-2: tombstoned files restore on re-scan
- All 11 tests pass

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

84 lines
2.5 KiB
Dart

import 'dart:io';
import 'package:drift/drift.dart';
import 'package:on_audio_query/on_audio_query.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart';
import 'database.dart';
const _uuid = Uuid();
/// Android-Scan über MediaStore (der einzige zuverlässige Weg ab Android 11,
/// da Scoped Storage direkten Dateizugriff blockiert). Findet automatisch alle
/// Musik auf dem Gerät. Schreibt in dieselbe DB wie der Desktop-Scan,
/// UUID-stabil über den Dateipfad.
Future<int> scanAndroidMediaStore(
MeloDb db, {
Directory? coverDir,
void Function(int done, int total)? onProgress,
}) async {
final audioQuery = OnAudioQuery();
final existing = {for (final s in await db.allSongs()) s.path: s};
final covers = coverDir ??
Directory(p.join((await getApplicationSupportDirectory()).path, 'covers'));
await covers.create(recursive: true);
final all = await audioQuery.querySongs(
sortType: SongSortType.TITLE,
orderType: OrderType.ASC_OR_SMALLER,
uriType: UriType.EXTERNAL,
);
final songs = all.where((s) => s.isMusic ?? true).toList();
final now = DateTime.now().millisecondsSinceEpoch;
final companions = <SongsCompanion>[];
var done = 0;
if (songs.isNotEmpty) onProgress?.call(0, songs.length);
for (final s in songs) {
final prev = existing[s.data];
final id = prev?.id ?? _uuid.v4();
var coverPath = prev?.coverPath;
if (coverPath == null) {
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.
}
}
companions.add(SongsCompanion.insert(
id: id,
path: s.data,
title: s.title,
artist: Value(s.artist == '<unknown>' ? null : s.artist),
album: Value(s.album),
durationMs: Value(s.duration),
coverPath: Value(coverPath),
dateAddedMs: prev?.dateAddedMs ??
(s.dateAdded != null ? s.dateAdded! * 1000 : now),
updatedAtMs: now,
deleted: const Value(false),
));
++done;
if (done % 50 == 0 || done == songs.length) {
onProgress?.call(done, songs.length);
}
}
await db.upsertSongs(companions);
if (companions.isNotEmpty) {
await db.markMissing(now);
}
return companions.length;
}