Made repository methods asynchronous. Simplified playlist repository. Use playlist manager in the app, and left playlist repository to only be used by the playlist manager.

This commit is contained in:
2026-07-24 09:49:58 -04:00
parent 0acbab1215
commit 63938b0139
14 changed files with 181 additions and 96 deletions

View File

@@ -1,7 +1,12 @@
namespace Harmonia.Core.Playlists;
public class PlaylistManager(IPlaylistRepository playlistRepository) : IPlaylistManager
public class PlaylistManager : IPlaylistManager
{
private readonly IPlaylistRepository playlistRepository;
private readonly List<Playlist> _playlists = [];
public IReadOnlyList<Playlist> Playlists => _playlists;
private Playlist? _currentPlaylist;
public Playlist? CurrentPlaylist
{
@@ -20,22 +25,60 @@ public class PlaylistManager(IPlaylistRepository playlistRepository) : IPlaylist
public event EventHandler<PlaylistAddedEventArgs>? PlaylistAdded;
public event EventHandler<PlaylistRemovedEventArgs>? PlaylistRemoved;
public void AddPlaylist()
public PlaylistManager(IPlaylistRepository playlistRepository)
{
this.playlistRepository = playlistRepository;
}
public async Task InitializeAsync()
{
_playlists.AddRange(playlistRepository.Get());
foreach (Playlist playlist in _playlists)
{
playlist.PlaylistUpdated += OnPlaylistUpdated;
}
CurrentPlaylist = _playlists.Count > 0 ? _playlists[0] : await AddPlaylistAsync();
}
public async Task<Playlist> AddPlaylistAsync()
{
Playlist playlist = new()
{
Name = "New Playlist"
};
playlistRepository.Save(playlist);
playlist.PlaylistUpdated += OnPlaylistUpdated;
_playlists.Add(playlist);
await playlistRepository.SaveAsync(playlist);
PlaylistAdded?.Invoke(this, new(playlist));
return playlist;
}
public void RemovePlaylist(Playlist playlist)
{
playlist.PlaylistUpdated -= OnPlaylistUpdated;
_playlists.Remove(playlist);
playlistRepository.Delete(playlist);
PlaylistRemoved?.Invoke(this, new(playlist));
}
public Playlist? GetPlaylist(PlaylistSong playlistSong)
{
return _playlists.FirstOrDefault(playlist => playlist.Songs.Any(song => song.UID == playlistSong.UID));
}
private async void OnPlaylistUpdated(object? sender, PlaylistUpdatedEventArgs e)
{
if (sender is not Playlist playlist)
return;
await playlistRepository.SaveAsync(playlist);
}
}