feat(playlists): Playlist-Detailansicht mit Drag&Drop-Umsortieren
PlaylistDetailScreen zeigt die Songs einer Playlist (watchPlaylistSongs), erlaubt Entfernen einzelner Songs und Umsortieren per Drag & Drop (ReorderableListView.builder). Tippen auf einen Song spielt die Playlist ab dieser Stelle. Leer-Zustand mit Hinweistext. Da bestehende position-Werte nach Einfügereihenfolge lückenhaft sein können, schreibt das Umsortieren nicht nur den verschobenen Song um, sondern vergibt für die gesamte Playlist neue lückenlose Positionen (0..N-1): MeloDb.reorderAllPlaylistSongs (in einer Transaktion) + PlaylistService.reorderAll als dünner Wrapper. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 4.5
parent
4b5d77c196
commit
96d9f38a64
@@ -232,6 +232,20 @@ class MeloDb extends _$MeloDb {
|
|||||||
.write(PlaylistSongsCompanion(position: Value(newPosition)));
|
.write(PlaylistSongsCompanion(position: Value(newPosition)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Schreibt für die gesamte Playlist neue, lückenlose Positionen (0..N-1)
|
||||||
|
/// gemäß [orderedSongIds] — vermeidet Mehrdeutigkeiten beim Umsortieren,
|
||||||
|
/// da einzelne [reorderPlaylistSong]-Aufrufe nur einen Song verschieben.
|
||||||
|
Future<void> reorderAllPlaylistSongs(String playlistId, List<String> orderedSongIds) async {
|
||||||
|
await transaction(() async {
|
||||||
|
for (var i = 0; i < orderedSongIds.length; i++) {
|
||||||
|
await (update(playlistSongs)
|
||||||
|
..where((ps) =>
|
||||||
|
ps.playlistId.equals(playlistId) & ps.songId.equals(orderedSongIds[i])))
|
||||||
|
.write(PlaylistSongsCompanion(position: Value(i)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// === Favorites ===
|
// === Favorites ===
|
||||||
Future<void> toggleFavorite(String songId) async {
|
Future<void> toggleFavorite(String songId) async {
|
||||||
final existing = await (select(favorites)..where((f) => f.songId.equals(songId))).getSingleOrNull();
|
final existing = await (select(favorites)..where((f) => f.songId.equals(songId))).getSingleOrNull();
|
||||||
|
|||||||
@@ -34,6 +34,11 @@ class PlaylistService extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> reorderAll(String playlistId, List<String> orderedSongIds) async {
|
||||||
|
await db.reorderAllPlaylistSongs(playlistId, orderedSongIds);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> toggleFavorite(String songId) async {
|
Future<void> toggleFavorite(String songId) async {
|
||||||
await db.toggleFavorite(songId);
|
await db.toggleFavorite(songId);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
import '../library/database.dart';
|
||||||
|
import '../library/playlist_service.dart';
|
||||||
|
import '../library/song_media.dart';
|
||||||
|
import '../player/audio_handler.dart';
|
||||||
|
import '../shared/cover.dart';
|
||||||
|
|
||||||
|
/// Playlist-Detailansicht: Songs anzeigen, per Drag & Drop umsortieren, entfernen.
|
||||||
|
class PlaylistDetailScreen extends StatelessWidget {
|
||||||
|
const PlaylistDetailScreen({super.key, required this.playlist});
|
||||||
|
final Playlist playlist;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final db = context.read<MeloDb>();
|
||||||
|
final handler = context.read<MeloAudioHandler>();
|
||||||
|
final service = context.read<PlaylistService>();
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: Text(playlist.name)),
|
||||||
|
body: StreamBuilder<List<Song>>(
|
||||||
|
stream: db.watchPlaylistSongs(playlist.id),
|
||||||
|
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(
|
||||||
|
child: Text('Playlist ist leer — füge Songs aus der Bibliothek hinzu',
|
||||||
|
style: TextStyle(color: Colors.white54)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return ReorderableListView.builder(
|
||||||
|
itemCount: songs.length,
|
||||||
|
onReorderItem: (oldIndex, newIndex) {
|
||||||
|
final reordered = List<Song>.from(songs);
|
||||||
|
final moved = reordered.removeAt(oldIndex);
|
||||||
|
reordered.insert(newIndex, moved);
|
||||||
|
service.reorderAll(playlist.id, reordered.map((s) => s.id).toList());
|
||||||
|
},
|
||||||
|
itemBuilder: (context, i) {
|
||||||
|
final s = songs[i];
|
||||||
|
return ListTile(
|
||||||
|
key: ValueKey(s.id),
|
||||||
|
leading: CoverImage(
|
||||||
|
artUri: s.coverPath != null ? Uri.file(s.coverPath!) : null,
|
||||||
|
size: 48,
|
||||||
|
radius: 6,
|
||||||
|
),
|
||||||
|
title: Text(s.title, maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||||
|
subtitle: Text(s.artist ?? 'Unbekannt',
|
||||||
|
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||||
|
trailing: IconButton(
|
||||||
|
tooltip: 'Aus Playlist entfernen',
|
||||||
|
icon: const Icon(Icons.remove_circle_outline),
|
||||||
|
onPressed: () => service.removeSongFromPlaylist(playlist.id, s.id),
|
||||||
|
),
|
||||||
|
onTap: () async {
|
||||||
|
try {
|
||||||
|
await playSongs(handler, songs, i);
|
||||||
|
} catch (e) {
|
||||||
|
if (context.mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('Wiedergabe fehlgeschlagen: $e')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,4 +39,26 @@ void main() {
|
|||||||
expect(await db.watchIsFavorite('song-1').first, true);
|
expect(await db.watchIsFavorite('song-1').first, true);
|
||||||
expect(notified, true);
|
expect(notified, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('reorderAll schreibt lückenlose Positionen 0..N-1 in neuer Reihenfolge', () async {
|
||||||
|
final playlistId = await db.createPlaylist('Mix');
|
||||||
|
for (final id in ['song-1', 'song-2', 'song-3']) {
|
||||||
|
await db.into(db.songs).insert(SongsCompanion.insert(
|
||||||
|
id: id, path: '/$id.mp3', title: id, dateAddedMs: 0, updatedAtMs: 0,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
// Ursprüngliche (evtl. lückenhafte) Positionen.
|
||||||
|
await db.addSongToPlaylist(playlistId, 'song-1', 0);
|
||||||
|
await db.addSongToPlaylist(playlistId, 'song-2', 5);
|
||||||
|
await db.addSongToPlaylist(playlistId, 'song-3', 9);
|
||||||
|
|
||||||
|
var notified = false;
|
||||||
|
service.addListener(() => notified = true);
|
||||||
|
|
||||||
|
await service.reorderAll(playlistId, ['song-3', 'song-1', 'song-2']);
|
||||||
|
|
||||||
|
final songs = await db.watchPlaylistSongs(playlistId).first;
|
||||||
|
expect(songs.map((s) => s.id).toList(), ['song-3', 'song-1', 'song-2']);
|
||||||
|
expect(notified, true);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user