- AutoScan: beim Zurueckkehren in die App wird die Zahl der Musikdateien mit der Bibliothek verglichen; nur bei Abweichung laeuft ein Scan, und hoechstens alle 5 Minuten. Gleich viele Loeschungen wie Neuzugaenge bleiben unbemerkt - dafuer gibt es den Scan von Hand. - ServerNeuheiten: meldet neu auf Navidrome aufgetauchte Alben im Online-Tab. Der erste Abruf meldet bewusst nichts, sonst waere die ganze Sammlung "neu"; er nutzt die ohnehin geladenen Alben statt eines zweiten Serveraufrufs. - Neu: MeloDb.countSongs(), countAndroidMediaStoreSongs(). 189 Tests gruen, flutter analyze ohne Befund. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NLW1q1hzEC9goDQ1dJ98d
101 lines
3.2 KiB
Dart
101 lines
3.2 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 'categories.dart';
|
|
import 'database.dart';
|
|
|
|
const _uuid = Uuid();
|
|
|
|
/// Zählt die Musikdateien, die der MediaStore kennt. Grundlage für die
|
|
/// automatische Erkennung: weicht die Zahl von der Bibliothek ab, ist etwas
|
|
/// dazugekommen oder verschwunden.
|
|
Future<int> countAndroidMediaStoreSongs() async {
|
|
final all = await OnAudioQuery().querySongs(uriType: UriType.EXTERNAL);
|
|
return all.where((s) => s.isMusic ?? true).length;
|
|
}
|
|
|
|
/// 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>[];
|
|
// Siehe scan_service.dart: erst Songs, dann Kategorien.
|
|
final categories = <String, List<String>>{};
|
|
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),
|
|
));
|
|
if (prev?.categoriesEdited != true) {
|
|
categories[id] = parseCategories(s.genre);
|
|
}
|
|
++done;
|
|
if (done % 50 == 0 || done == songs.length) {
|
|
onProgress?.call(done, songs.length);
|
|
}
|
|
}
|
|
|
|
await db.upsertSongs(companions);
|
|
for (final entry in categories.entries) {
|
|
await db.setCategories(entry.key, entry.value);
|
|
}
|
|
if (companions.isNotEmpty) {
|
|
await db.markMissing(now);
|
|
}
|
|
return companions.length;
|
|
}
|