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:
@@ -2,10 +2,13 @@
|
||||
|
||||
public interface IPlaylistManager
|
||||
{
|
||||
IReadOnlyList<Playlist> Playlists { get; }
|
||||
Playlist? CurrentPlaylist { get; set; }
|
||||
|
||||
void AddPlaylist();
|
||||
Task InitializeAsync();
|
||||
Task<Playlist> AddPlaylistAsync();
|
||||
void RemovePlaylist(Playlist playlist);
|
||||
Playlist? GetPlaylist(PlaylistSong playlistSong);
|
||||
|
||||
event EventHandler? CurrentPlaylistChanged;
|
||||
event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
|
||||
|
||||
@@ -5,9 +5,9 @@ namespace Harmonia.Core.Playlists;
|
||||
public interface IPlaylistRepository : IRepository<Playlist>
|
||||
{
|
||||
Playlist? GetPlaylist(PlaylistSong playlistSong);
|
||||
void AddPlaylist();
|
||||
void RemovePlaylist(Playlist playlist);
|
||||
//void AddPlaylist();
|
||||
//void RemovePlaylist(Playlist playlist);
|
||||
|
||||
event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
|
||||
event EventHandler<PlaylistRemovedEventArgs> PlaylistRemoved;
|
||||
//event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
|
||||
//event EventHandler<PlaylistRemovedEventArgs> PlaylistRemoved;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -6,26 +6,26 @@ public class PlaylistRepository : JsonFileRepository<Playlist>, IPlaylistReposit
|
||||
{
|
||||
protected override string DirectoryName => Path.Combine("Playlists");
|
||||
|
||||
public PlaylistRepository()
|
||||
{
|
||||
List<Playlist> playlists = Get();
|
||||
//public PlaylistRepository()
|
||||
//{
|
||||
// List<Playlist> playlists = Get();
|
||||
|
||||
foreach (Playlist playlist in playlists)
|
||||
{
|
||||
playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
}
|
||||
// foreach (Playlist playlist in playlists)
|
||||
// {
|
||||
// playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
// }
|
||||
|
||||
if (playlists.Count == 0)
|
||||
AddPlaylist();
|
||||
}
|
||||
// if (playlists.Count == 0)
|
||||
// AddPlaylist();
|
||||
//}
|
||||
|
||||
private void OnPlaylistUpdated(object? sender, PlaylistUpdatedEventArgs e)
|
||||
{
|
||||
if (sender is not Playlist playlist)
|
||||
return;
|
||||
//private void OnPlaylistUpdated(object? sender, PlaylistUpdatedEventArgs e)
|
||||
//{
|
||||
// if (sender is not Playlist playlist)
|
||||
// return;
|
||||
|
||||
Save(playlist);
|
||||
}
|
||||
// Save(playlist);
|
||||
//}
|
||||
|
||||
public Playlist? GetPlaylist(PlaylistSong playlistSong)
|
||||
{
|
||||
@@ -46,27 +46,27 @@ public class PlaylistRepository : JsonFileRepository<Playlist>, IPlaylistReposit
|
||||
throw new Exception("Unable to determine new fileName");
|
||||
}
|
||||
|
||||
public event EventHandler<PlaylistAddedEventArgs>? PlaylistAdded;
|
||||
public event EventHandler<PlaylistRemovedEventArgs>? PlaylistRemoved;
|
||||
//public event EventHandler<PlaylistAddedEventArgs>? PlaylistAdded;
|
||||
//public event EventHandler<PlaylistRemovedEventArgs>? PlaylistRemoved;
|
||||
|
||||
public void AddPlaylist()
|
||||
{
|
||||
Playlist playlist = new()
|
||||
{
|
||||
Name = "New Playlist"
|
||||
};
|
||||
//public void AddPlaylist()
|
||||
//{
|
||||
// Playlist playlist = new()
|
||||
// {
|
||||
// Name = "New Playlist"
|
||||
// };
|
||||
|
||||
playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
// playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
|
||||
Save(playlist);
|
||||
PlaylistAdded?.Invoke(this, new(playlist));
|
||||
}
|
||||
// Save(playlist);
|
||||
// PlaylistAdded?.Invoke(this, new(playlist));
|
||||
//}
|
||||
|
||||
public void RemovePlaylist(Playlist playlist)
|
||||
{
|
||||
playlist.PlaylistUpdated -= OnPlaylistUpdated;
|
||||
//public void RemovePlaylist(Playlist playlist)
|
||||
//{
|
||||
// playlist.PlaylistUpdated -= OnPlaylistUpdated;
|
||||
|
||||
Delete(playlist);
|
||||
PlaylistRemoved?.Invoke(this, new(playlist));
|
||||
}
|
||||
// Delete(playlist);
|
||||
// PlaylistRemoved?.Invoke(this, new(playlist));
|
||||
//}
|
||||
}
|
||||
Reference in New Issue
Block a user