feat(services): add PlaylistService, wire into app providers
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 4.5
parent
449fe4ce76
commit
92f565b1d4
@@ -0,0 +1,41 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'database.dart';
|
||||||
|
|
||||||
|
/// Koordiniert Playlisten- und Favoriten-Operationen; benachrichtigt Listener
|
||||||
|
/// nach jeder Mutation (für Feedback wie SnackBars — die Listen selbst
|
||||||
|
/// beobachten UIs direkt über die watch()-Streams von [MeloDb]).
|
||||||
|
class PlaylistService extends ChangeNotifier {
|
||||||
|
PlaylistService(this.db);
|
||||||
|
final MeloDb db;
|
||||||
|
|
||||||
|
Future<String> createPlaylist(String name, {String? description}) async {
|
||||||
|
final id = await db.createPlaylist(name, description: description);
|
||||||
|
notifyListeners();
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deletePlaylist(String id) async {
|
||||||
|
await db.deletePlaylist(id);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> addSongToPlaylist(String playlistId, String songId, int position) async {
|
||||||
|
await db.addSongToPlaylist(playlistId, songId, position);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> removeSongFromPlaylist(String playlistId, String songId) async {
|
||||||
|
await db.removeSongFromPlaylist(playlistId, songId);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> reorderSong(String playlistId, String songId, int newPosition) async {
|
||||||
|
await db.reorderPlaylistSong(playlistId, songId, newPosition);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> toggleFavorite(String songId) async {
|
||||||
|
await db.toggleFavorite(songId);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import 'home_screen.dart';
|
|||||||
import 'library/database.dart';
|
import 'library/database.dart';
|
||||||
import 'library/library_screen.dart';
|
import 'library/library_screen.dart';
|
||||||
import 'library/library_service.dart';
|
import 'library/library_service.dart';
|
||||||
|
import 'library/playlist_service.dart';
|
||||||
import 'library/search_screen.dart';
|
import 'library/search_screen.dart';
|
||||||
import 'player/audio_handler.dart';
|
import 'player/audio_handler.dart';
|
||||||
import 'player/mini_player.dart';
|
import 'player/mini_player.dart';
|
||||||
@@ -14,11 +15,13 @@ import 'shared/theme.dart';
|
|||||||
late final MeloAudioHandler _handler;
|
late final MeloAudioHandler _handler;
|
||||||
late final MeloDb _db;
|
late final MeloDb _db;
|
||||||
late final LibraryService _library;
|
late final LibraryService _library;
|
||||||
|
late final PlaylistService _playlists;
|
||||||
|
|
||||||
Future<void> main() async {
|
Future<void> main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
_db = MeloDb();
|
_db = MeloDb();
|
||||||
_library = LibraryService(_db);
|
_library = LibraryService(_db);
|
||||||
|
_playlists = PlaylistService(_db);
|
||||||
_handler = await AudioService.init(
|
_handler = await AudioService.init(
|
||||||
builder: () => MeloAudioHandler(),
|
builder: () => MeloAudioHandler(),
|
||||||
config: const AudioServiceConfig(
|
config: const AudioServiceConfig(
|
||||||
@@ -40,6 +43,7 @@ class MeloApp extends StatelessWidget {
|
|||||||
Provider<MeloAudioHandler>.value(value: _handler),
|
Provider<MeloAudioHandler>.value(value: _handler),
|
||||||
Provider<MeloDb>.value(value: _db),
|
Provider<MeloDb>.value(value: _db),
|
||||||
ChangeNotifierProvider<LibraryService>.value(value: _library),
|
ChangeNotifierProvider<LibraryService>.value(value: _library),
|
||||||
|
ChangeNotifierProvider<PlaylistService>.value(value: _playlists),
|
||||||
],
|
],
|
||||||
child: MaterialApp(
|
child: MaterialApp(
|
||||||
title: 'Melo',
|
title: 'Melo',
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import 'package:drift/native.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:melo/library/database.dart';
|
||||||
|
import 'package:melo/library/playlist_service.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late MeloDb db;
|
||||||
|
late PlaylistService service;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
db = MeloDb(NativeDatabase.memory());
|
||||||
|
service = PlaylistService(db);
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async => db.close());
|
||||||
|
|
||||||
|
test('createPlaylist delegates to db and notifies listeners', () async {
|
||||||
|
var notified = false;
|
||||||
|
service.addListener(() => notified = true);
|
||||||
|
|
||||||
|
await service.createPlaylist('Road Trip');
|
||||||
|
|
||||||
|
final playlists = await db.watchPlaylists().first;
|
||||||
|
expect(playlists.length, 1);
|
||||||
|
expect(playlists.first.name, 'Road Trip');
|
||||||
|
expect(notified, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('toggleFavorite delegates to db and notifies listeners', () async {
|
||||||
|
await db.into(db.songs).insert(SongsCompanion.insert(
|
||||||
|
id: 'song-1', path: '/a.mp3', title: 'A', dateAddedMs: 0, updatedAtMs: 0,
|
||||||
|
));
|
||||||
|
|
||||||
|
var notified = false;
|
||||||
|
service.addListener(() => notified = true);
|
||||||
|
|
||||||
|
await service.toggleFavorite('song-1');
|
||||||
|
|
||||||
|
expect(await db.watchIsFavorite('song-1').first, true);
|
||||||
|
expect(notified, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user