UI: Alben sind Kategorien — Reiter Lieder/Kategorie/Kuenstler

Auf Wunsch von Dustin: seine beste Freundin sortiert ihre Sammlung ueber
das Album-Feld. In Melo ist der Album-Titel deshalb ab jetzt die Kategorie
— sie muss nichts neu machen.

1) Album = Kategorie
- kategorienAusTags(): Album-Titel steht VORN in der Kategorienliste (die
  erste Kategorie bestimmt das Coverbild), Genres dahinter.
- Beide Scans (android_scan + scan_service) tragen ihn ein; von Hand
  gepflegte Kategorien (categoriesEdited) bleiben unberuehrt.
- DB-Schema 9: einmalige Nachruestung bestehender Bibliotheken, damit das
  nicht erst beim naechsten vollstaendigen Scan sichtbar wird (der auf
  Android nur laeuft, wenn sich die Dateianzahl aendert).

2) Reiter: 'Songs/Kuenstler/Alben' -> 'Lieder/Kategorie/Kuenstler'
- Neu: library/category_list.dart mit groupByCategory(); Lieder ohne
  Kategorie sammeln sich am Ende unter "Ohne Kategorie".
- Entfernt: library/album_list.dart, groupByAlbum(), albumArtistLabel() —
  mit dem Alben-Reiter tot geworden. Das Album-FELD bleibt erhalten.

3) YouTube-Downloads ohne Original-Album
- Feld "Kategorie (optional)" im YouTube-Bereich, mit Vorschlaegen aus der
  Bibliothek und freier Eingabe.
- ordneDownloadEin() verwirft nach dem Scan das Album-Tag (yt-dlp leitet es
  aus Kanal/Playlist ab — als Kategorie waere das Unsinn) und setzt
  stattdessen die gewaehlte Kategorie. Beides als "von Hand gesetzt"
  markiert, damit der naechste Scan es nicht zurueckholt.
- MeloDb.songByPath() und MeloDb.verwirfAlbum() neu.
- CategoryService.alleNamen: Kategorienamen ohne zusaetzliche Abfrage —
  ohne das flackerte die Vorschlagsliste und im Widget-Test blieb ein
  Aufraeum-Timer von drift haengen.

295 Tests gruen (19 neue, 1 uebersprungen), flutter analyze ohne Befund.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FpPu4nuKjKKeX1RpdDeX81
This commit is contained in:
Hermes (Server)
2026-08-21 09:36:46 +02:00
co-authored by Claude Opus 5
parent cd79213bb3
commit c7a7a7c458
17 changed files with 726 additions and 157 deletions
@@ -0,0 +1,98 @@
import 'package:drift/drift.dart' show Value;
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:melo/downloads/download_einordnung.dart';
import 'package:melo/library/database.dart';
const _pfad = '/storage/emulated/0/Music/Melo/Neues Lied.mp3';
Future<MeloDb> _dbMitDownload({String? album}) async {
final db = MeloDb(NativeDatabase.memory());
await db.upsertSongs([
SongsCompanion.insert(
id: 'yt-1',
path: _pfad,
title: 'Neues Lied',
album: Value(album),
dateAddedMs: 0,
updatedAtMs: 0,
),
]);
// So sieht es nach dem Scan aus: das Album-Tag von YouTube ist bereits
// als Kategorie gelandet.
if (album != null) await db.setCategories('yt-1', [album]);
return db;
}
void main() {
group('ordneDownloadEin', () {
test('das Album-Tag von YouTube wird verworfen', () async {
final db = await _dbMitDownload(album: 'Topic - Various Artists');
addTearDown(db.close);
await ordneDownloadEin(db, _pfad, 'Nightcore');
final song = (await db.allSongs()).single;
expect(song.album, isNull);
expect(await db.categoriesOf('yt-1'), ['Nightcore']);
});
test('die gewählte Kategorie überlebt den nächsten Scan', () async {
final db = await _dbMitDownload(album: 'Topic - Various Artists');
addTearDown(db.close);
await ordneDownloadEin(db, _pfad, 'Nightcore');
final song = (await db.allSongs()).single;
expect(song.metadataEdited, isTrue, reason: 'Album bleibt verworfen');
expect(song.categoriesEdited, isTrue, reason: 'Kategorie bleibt stehen');
});
test('ohne Angabe bleibt der Titel bewusst ohne Kategorie', () async {
final db = await _dbMitDownload(album: 'Topic - Various Artists');
addTearDown(db.close);
await ordneDownloadEin(db, _pfad, ' ');
expect(await db.categoriesOf('yt-1'), isEmpty);
expect((await db.allSongs()).single.album, isNull);
});
test('umgebende Leerzeichen zählen nicht zum Namen', () async {
final db = await _dbMitDownload();
addTearDown(db.close);
await ordneDownloadEin(db, _pfad, ' Nightcore ');
expect(await db.categoriesOf('yt-1'), ['Nightcore']);
});
test('ein unbekannter Pfad ändert nichts', () async {
final db = await _dbMitDownload(album: 'Bleibt');
addTearDown(db.close);
await ordneDownloadEin(db, '/gibt/es/nicht.mp3', 'Nightcore');
expect((await db.allSongs()).single.album, 'Bleibt');
expect(await db.categoriesOf('yt-1'), ['Bleibt']);
});
});
group('kategorieVorschlaege', () {
const vorhanden = ['Nightcore', 'Schwarz', 'Rock Classics'];
test('leere Eingabe zeigt alles', () {
expect(kategorieVorschlaege('', vorhanden), vorhanden);
});
test('filtert nach Teiltreffer, unabhängig von Groß-/Kleinschreibung', () {
expect(kategorieVorschlaege('night', vorhanden), ['Nightcore']);
expect(kategorieVorschlaege('ROCK', vorhanden), ['Rock Classics']);
});
test('ohne Treffer bleibt die Liste leer — die Eingabe wird neue '
'Kategorie', () {
expect(kategorieVorschlaege('Gibtsnicht', vorhanden), isEmpty);
});
});
}
+31 -11
View File
@@ -5,7 +5,10 @@ import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:drift/native.dart';
import 'package:melo/downloads/downloads_screen.dart';
import 'package:melo/library/category_service.dart';
import 'package:melo/library/database.dart';
import 'package:melo/services/baka_auth.dart';
import 'package:melo/services/yt_download_service.dart';
import 'package:provider/provider.dart';
@@ -21,9 +24,15 @@ class _MemorySpeicher implements TokenSpeicher {
Future<void> loeschen(String key) async => werte.remove(key);
}
Widget _wrap(BakaAuth auth) {
Widget _wrap(BakaAuth auth,
{required MeloDb db, required CategoryService categories}) {
return MultiProvider(
providers: [
Provider<MeloDb>.value(value: db),
// Bewusst .value: der Test besitzt den Dienst und schließt ihn im
// tearDown. Würde ihn der Baum beim Abbauen schließen, bliebe ein
// Aufräum-Timer von drift im Test-Rahmen hängen.
ChangeNotifierProvider<CategoryService>.value(value: categories),
ChangeNotifierProvider<BakaAuth>.value(value: auth),
ChangeNotifierProvider<YtDownloadService>(
create: (_) => YtDownloadService(
@@ -63,11 +72,20 @@ void main() {
});
}
late MeloDb db;
late CategoryService categories;
setUp(() {
SharedPreferences.setMockInitialValues({});
nurInternerSpeicher();
db = MeloDb(NativeDatabase.memory());
categories = CategoryService(db);
});
tearDown(() async {
messenger.setMockMethodCallHandler(medienKanal, null);
categories.dispose();
await db.close();
});
tearDown(() => messenger.setMockMethodCallHandler(medienKanal, null));
testWidgets('Online-Tab hat die Unterreiter YouTube und Server',
(tester) async {
@@ -75,7 +93,7 @@ void main() {
BakaAuth(client: MockClient((_) async => http.Response('', 500)),
speicher: _MemorySpeicher());
await tester.pumpWidget(_wrap(auth));
await tester.pumpWidget(_wrap(auth, db: db, categories: categories));
await tester.pump();
expect(find.text('YouTube'), findsOneWidget);
@@ -88,7 +106,7 @@ void main() {
BakaAuth(client: MockClient((_) async => http.Response('', 500)),
speicher: _MemorySpeicher());
await tester.pumpWidget(_wrap(auth));
await tester.pumpWidget(_wrap(auth, db: db, categories: categories));
await tester.pump();
expect(find.text('Beim Baka-Konto anmelden'), findsOneWidget);
@@ -104,16 +122,18 @@ void main() {
);
await auth.anmelden('Baka', 'geheim');
await tester.pumpWidget(_wrap(auth));
await tester.pumpWidget(_wrap(auth, db: db, categories: categories));
await tester.pump();
expect(find.text('Angemeldet als Baka'), findsOneWidget);
expect(find.byType(TextField), findsOneWidget);
expect(find.text('YouTube-Adresse'), findsOneWidget);
// Zweites Feld: die Kategorie, in die der Download wandert.
expect(find.text('Kategorie (optional)'), findsOneWidget);
expect(find.text('Herunterladen'), findsOneWidget);
});
testWidgets('Der Cookie-Schalter steht anfangs an', (tester) async {
await tester.pumpWidget(_wrap(await _angemeldet()));
await tester.pumpWidget(_wrap(await _angemeldet(), db: db, categories: categories));
await tester.pumpAndSettle();
expect(find.text('YouTube-Cookies des Servers verwenden'), findsOneWidget);
@@ -122,7 +142,7 @@ void main() {
});
testWidgets('Abschalten des Cookie-Schalters wird gemerkt', (tester) async {
await tester.pumpWidget(_wrap(await _angemeldet()));
await tester.pumpWidget(_wrap(await _angemeldet(), db: db, categories: categories));
await tester.pumpAndSettle();
await tester.tap(find.byType(SwitchListTile));
@@ -138,7 +158,7 @@ void main() {
(tester) async {
SharedPreferences.setMockInitialValues({'yt_cookies': false});
await tester.pumpWidget(_wrap(await _angemeldet()));
await tester.pumpWidget(_wrap(await _angemeldet(), db: db, categories: categories));
await tester.pumpAndSettle();
expect(tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value,
@@ -155,7 +175,7 @@ void main() {
];
});
await tester.pumpWidget(_wrap(await _angemeldet()));
await tester.pumpWidget(_wrap(await _angemeldet(), db: db, categories: categories));
await tester.pumpAndSettle();
expect(find.byType(DropdownButtonFormField<String>), findsOneWidget);
@@ -164,7 +184,7 @@ void main() {
testWidgets('Bei nur einem Speicherort gibt es keine Auswahl',
(tester) async {
await tester.pumpWidget(_wrap(await _angemeldet()));
await tester.pumpWidget(_wrap(await _angemeldet(), db: db, categories: categories));
await tester.pumpAndSettle();
expect(find.byType(DropdownButtonFormField<String>), findsNothing);
+213
View File
@@ -0,0 +1,213 @@
import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:melo/library/categories.dart';
import 'package:melo/library/database.dart';
import 'package:melo/library/scan_service.dart';
import 'package:path/path.dart' as p;
Song _song(String id, {String titel = 'Lied'}) => Song(
id: id,
path: '/musik/$id.mp3',
title: titel,
dateAddedMs: 0,
updatedAtMs: 0,
deleted: false,
playCount: 0,
categoriesEdited: false,
metadataEdited: false,
);
void main() {
group('kategorienAusTags', () {
test('das Album steht vorn — es bestimmt das Coverbild', () {
expect(
kategorienAusTags(album: 'Schwarz', genres: const ['Nightcore']),
['Schwarz', 'Nightcore'],
);
});
test('ohne Album bleiben nur die Genres', () {
expect(kategorienAusTags(genres: const ['Rock']), ['Rock']);
expect(kategorienAusTags(album: ' ', genres: const ['Rock']), ['Rock']);
});
test('ohne alles gibt es keine Kategorie', () {
expect(kategorienAusTags(), isEmpty);
});
test('Album und Genre mit gleichem Namen erscheinen nur einmal', () {
expect(
kategorienAusTags(album: 'Nightcore', genres: const ['nightcore']),
['Nightcore'],
);
});
test('mehrere Genres in einem Feld werden aufgetrennt', () {
expect(
kategorienAusTags(album: 'Mix', genres: const ['Pop; Rock']),
['Mix', 'Pop', 'Rock'],
);
});
});
group('groupByCategory', () {
test('ein Titel erscheint unter jeder seiner Kategorien', () {
final songs = [_song('1'), _song('2')];
final grouped = groupByCategory(songs, {
'1': ['Schwarz', 'Nightcore'],
'2': ['Nightcore'],
});
expect(grouped.keys, ['Nightcore', 'Schwarz']);
expect(grouped['Nightcore']!.map((s) => s.id), ['1', '2']);
expect(grouped['Schwarz']!.map((s) => s.id), ['1']);
});
test('Titel ohne Kategorie sammeln sich am Ende', () {
final grouped = groupByCategory(
[_song('1'), _song('2')],
{
'1': ['Schwarz']
},
);
expect(grouped.keys.last, ohneKategorie);
expect(grouped[ohneKategorie]!.map((s) => s.id), ['2']);
});
test('ohne Titel ohne Kategorie fehlt der Sammel-Eintrag', () {
final grouped = groupByCategory([
_song('1')
], {
'1': ['Schwarz']
});
expect(grouped.containsKey(ohneKategorie), isFalse);
});
test('leere Bibliothek ergibt nichts', () {
expect(groupByCategory(const [], const {}), isEmpty);
});
});
group('Scan trägt das Album als Kategorie ein', () {
final fixtures = p.join(Directory.current.path, 'test', 'fixtures', 'audio');
late MeloDb db;
late Directory coverDir;
setUp(() async {
db = MeloDb(NativeDatabase.memory());
coverDir = await Directory.systemTemp.createTemp('melo_covers');
});
tearDown(() async {
await db.close();
await coverDir.delete(recursive: true);
});
test('nach dem Scan ist der Album-Titel eine Kategorie', () async {
await scanFolders(db, [fixtures], coverDir: coverDir);
final song =
(await db.watchSongs().first).firstWhere((s) => s.title == 'Nachtpuls');
expect(song.album, 'Schwarz');
expect(await db.categoriesOf(song.id), contains('Schwarz'));
expect((await db.categoriesOf(song.id)).first, 'Schwarz');
});
test('von Hand gesetzte Kategorien überschreibt der Scan nicht', () async {
await scanFolders(db, [fixtures], coverDir: coverDir);
final song =
(await db.watchSongs().first).firstWhere((s) => s.title == 'Nachtpuls');
await db.setCategories(song.id, ['Eigene Kiste'], byUser: true);
await scanFolders(db, [fixtures], coverDir: coverDir);
expect(await db.categoriesOf(song.id), ['Eigene Kiste']);
});
});
group('Nachrüstung bestehender Bibliotheken (Schema 9)', () {
test('das Album wird als erste Kategorie ergänzt', () async {
final db = MeloDb(NativeDatabase.memory());
addTearDown(db.close);
await db.upsertSongs([
SongsCompanion.insert(
id: 'a',
path: '/a.mp3',
title: 'A',
album: const Value('Schwarz'),
dateAddedMs: 0,
updatedAtMs: 0,
),
]);
await db.setCategories('a', ['Nightcore']);
await db.ergaenzeAlbumKategorienFuerTest();
expect(await db.categoriesOf('a'), ['Schwarz', 'Nightcore']);
});
test('ein bereits als Kategorie vorhandenes Album rutscht nach vorn',
() async {
final db = MeloDb(NativeDatabase.memory());
addTearDown(db.close);
await db.upsertSongs([
SongsCompanion.insert(
id: 'a',
path: '/a.mp3',
title: 'A',
album: const Value('Schwarz'),
dateAddedMs: 0,
updatedAtMs: 0,
),
]);
await db.setCategories('a', ['Nightcore', 'Schwarz']);
await db.ergaenzeAlbumKategorienFuerTest();
expect(await db.categoriesOf('a'), ['Schwarz', 'Nightcore']);
});
test('von Hand gepflegte Kategorien bleiben unangetastet', () async {
final db = MeloDb(NativeDatabase.memory());
addTearDown(db.close);
await db.upsertSongs([
SongsCompanion.insert(
id: 'a',
path: '/a.mp3',
title: 'A',
album: const Value('Schwarz'),
dateAddedMs: 0,
updatedAtMs: 0,
),
]);
await db.setCategories('a', ['Eigene Kiste'], byUser: true);
await db.ergaenzeAlbumKategorienFuerTest();
expect(await db.categoriesOf('a'), ['Eigene Kiste']);
});
test('ohne Album passiert nichts', () async {
final db = MeloDb(NativeDatabase.memory());
addTearDown(db.close);
await db.upsertSongs([
SongsCompanion.insert(
id: 'a',
path: '/a.mp3',
title: 'A',
dateAddedMs: 0,
updatedAtMs: 0,
),
]);
await db.setCategories('a', ['Nightcore']);
await db.ergaenzeAlbumKategorienFuerTest();
expect(await db.categoriesOf('a'), ['Nightcore']);
});
});
}
+35 -7
View File
@@ -48,7 +48,7 @@ Future<void> _insert(
void main() {
setUp(() => SharedPreferences.setMockInitialValues({}));
testWidgets('Meine Musik hat die Unterreiter Songs, Künstler und Alben',
testWidgets('Meine Musik hat die Unterreiter Lieder, Kategorie und Künstler',
(tester) async {
final db = MeloDb(NativeDatabase.memory());
final lib = LibraryService(db);
@@ -60,9 +60,11 @@ void main() {
_wrap(db, lib, playlists, handler, const MyMusicScreen()));
await tester.pumpAndSettle();
expect(find.text('Songs'), findsOneWidget);
expect(find.text('Lieder'), findsOneWidget);
expect(find.text('Kategorie'), findsOneWidget);
expect(find.text('Künstler'), findsOneWidget);
expect(find.text('Alben'), findsOneWidget);
// Der Alben-Reiter ist im Kategorie-Reiter aufgegangen.
expect(find.text('Alben'), findsNothing);
handler.dispose();
await db.close();
@@ -87,14 +89,15 @@ void main() {
// Unterreiter wechseln den Inhalt, sie öffnen keinen neuen Bildschirm —
// Suchfeld und Reiter bleiben sichtbar.
expect(find.text('Titel, Künstler und Alben suchen'), findsOneWidget);
expect(find.text('Songs'), findsOneWidget);
expect(find.text('Lieder'), findsOneWidget);
expect(find.text('Shuffle-Wiedergabe'), findsNothing);
handler.dispose();
await db.close();
});
testWidgets('Unterreiter Alben zeigt die Albenliste', (tester) async {
testWidgets('Unterreiter Kategorie zeigt den Album-Titel als Kategorie',
(tester) async {
final db = MeloDb(NativeDatabase.memory());
final lib = LibraryService(db);
final playlists = PlaylistService(db);
@@ -105,12 +108,37 @@ void main() {
_wrap(db, lib, playlists, handler, const MyMusicScreen()));
await tester.pumpAndSettle();
await tester.tap(find.text('Alben'));
// Das Album ist die Kategorie (siehe kategorienAusTags) — der Titel
// taucht deshalb unter "Roses" auf.
await db.setCategories('1', ['Roses']);
await tester.tap(find.text('Kategorie'));
await tester.pumpAndSettle();
expect(find.text('Roses'), findsOneWidget);
expect(find.text('1 Lieder'), findsOneWidget);
expect(find.text('Titel, Künstler und Alben suchen'), findsOneWidget);
expect(find.text('Songs'), findsOneWidget);
expect(find.text('Lieder'), findsOneWidget);
handler.dispose();
await db.close();
});
testWidgets('Titel ohne Kategorie sammeln sich unter "Ohne Kategorie"',
(tester) async {
final db = MeloDb(NativeDatabase.memory());
final lib = LibraryService(db);
final playlists = PlaylistService(db);
final handler = MeloAudioHandler(db: db);
await _insert(db, id: '1', title: 'Song A');
await tester.pumpWidget(
_wrap(db, lib, playlists, handler, const MyMusicScreen()));
await tester.pumpAndSettle();
await tester.tap(find.text('Kategorie'));
await tester.pumpAndSettle();
expect(find.text('Ohne Kategorie'), findsOneWidget);
handler.dispose();
await db.close();
+1 -46
View File
@@ -67,49 +67,4 @@ void main() {
expect(groupByArtist(const []), isEmpty);
});
});
group('groupByAlbum', () {
test('gruppiert mehrere Alben getrennt', () {
final songs = [
_song(id: '1', title: 'A', album: 'Best Of'),
_song(id: '2', title: 'B', album: 'Anthology'),
];
final grouped = groupByAlbum(songs);
expect(grouped.keys.toList(), ['Anthology', 'Best Of']);
});
test('gruppiert Songs ohne Album unter "Unbekanntes Album"', () {
final songs = [_song(id: '1', title: 'A', album: null)];
final grouped = groupByAlbum(songs);
expect(grouped.keys, [unbekanntesAlbum]);
});
test('leere Liste ergibt leere Map', () {
expect(groupByAlbum(const []), isEmpty);
});
});
group('albumArtistLabel', () {
test('zeigt gemeinsamen Künstler, wenn alle Songs von ihm stammen', () {
final songs = [
_song(id: '1', title: 'A', artist: 'Alice', album: 'X'),
_song(id: '2', title: 'B', artist: 'Alice', album: 'X'),
];
expect(albumArtistLabel(songs), 'Alice');
});
test('zeigt "Verschiedene Interpreten" bei unterschiedlichen Künstlern', () {
final songs = [
_song(id: '1', title: 'A', artist: 'Alice', album: 'X'),
_song(id: '2', title: 'B', artist: 'Bob', album: 'X'),
];
expect(albumArtistLabel(songs), 'Verschiedene Interpreten');
});
});
}
}