Highlight active playlist. Various optimizations and fixes.
This commit is contained in:
@@ -4,6 +4,7 @@ public class PlaylistManager : IPlaylistManager
|
||||
{
|
||||
private readonly IPlaylistRepository playlistRepository;
|
||||
private readonly List<Playlist> _playlists = [];
|
||||
private readonly Dictionary<string, Playlist> _playlistsBySongUid = [];
|
||||
|
||||
public IReadOnlyList<Playlist> Playlists => _playlists;
|
||||
|
||||
@@ -37,6 +38,11 @@ public class PlaylistManager : IPlaylistManager
|
||||
foreach (Playlist playlist in _playlists)
|
||||
{
|
||||
playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
|
||||
foreach (PlaylistSong song in playlist.Songs)
|
||||
{
|
||||
_playlistsBySongUid[song.UID] = playlist;
|
||||
}
|
||||
}
|
||||
|
||||
CurrentPlaylist = _playlists.Count > 0 ? _playlists[0] : await AddPlaylistAsync();
|
||||
@@ -44,14 +50,16 @@ public class PlaylistManager : IPlaylistManager
|
||||
|
||||
public async Task<Playlist> AddPlaylistAsync()
|
||||
{
|
||||
Playlist playlist = new()
|
||||
{
|
||||
Name = "New Playlist"
|
||||
};
|
||||
Playlist playlist = new("New Playlist");
|
||||
|
||||
playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
_playlists.Add(playlist);
|
||||
|
||||
foreach (PlaylistSong song in playlist.Songs)
|
||||
{
|
||||
_playlistsBySongUid[song.UID] = playlist;
|
||||
}
|
||||
|
||||
await playlistRepository.SaveAsync(playlist);
|
||||
|
||||
PlaylistAdded?.Invoke(this, new(playlist));
|
||||
@@ -64,14 +72,19 @@ public class PlaylistManager : IPlaylistManager
|
||||
playlist.PlaylistUpdated -= OnPlaylistUpdated;
|
||||
_playlists.Remove(playlist);
|
||||
|
||||
foreach (PlaylistSong song in playlist.Songs)
|
||||
{
|
||||
_playlistsBySongUid.Remove(song.UID);
|
||||
}
|
||||
|
||||
playlistRepository.Delete(playlist);
|
||||
|
||||
PlaylistRemoved?.Invoke(this, new(playlist));
|
||||
}
|
||||
|
||||
public Playlist? GetPlaylist(PlaylistSong playlistSong)
|
||||
public Playlist? FindPlaylistContaining(PlaylistSong playlistSong)
|
||||
{
|
||||
return _playlists.FirstOrDefault(playlist => playlist.Songs.Any(song => song.UID == playlistSong.UID));
|
||||
return _playlistsBySongUid.GetValueOrDefault(playlistSong.UID);
|
||||
}
|
||||
|
||||
private async void OnPlaylistUpdated(object? sender, PlaylistUpdatedEventArgs e)
|
||||
@@ -79,6 +92,22 @@ public class PlaylistManager : IPlaylistManager
|
||||
if (sender is not Playlist playlist)
|
||||
return;
|
||||
|
||||
switch (e.Action)
|
||||
{
|
||||
case PlaylistUpdateAction.Add:
|
||||
foreach (PlaylistSong song in e.Songs)
|
||||
{
|
||||
_playlistsBySongUid[song.UID] = playlist;
|
||||
}
|
||||
break;
|
||||
case PlaylistUpdateAction.Remove:
|
||||
foreach (PlaylistSong song in e.Songs)
|
||||
{
|
||||
_playlistsBySongUid.Remove(song.UID);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
await playlistRepository.SaveAsync(playlist);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user