## 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>
84 lines
2.5 KiB
Dart
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>[];
|
|
final livePaths = <String>[];
|
|
var done = 0;
|
|
|
|
for (final s in songs) {
|
|
livePaths.add(s.data);
|
|
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 (livePaths.isNotEmpty) {
|
|
await db.markMissing(now);
|
|
}
|
|
return companions.length;
|
|
}
|