Highlight active playlist. Various optimizations and fixes.

This commit is contained in:
2026-08-05 00:50:56 -04:00
parent 3c3cf37cd5
commit 1d45826c38
14 changed files with 327 additions and 55 deletions

View File

@@ -5,15 +5,48 @@ namespace Harmonia.Core.Playlists;
public class Playlist
{
private readonly List<PlaylistSong> _songs = [];
private readonly List<GroupOption> _groupOptions = [];
private readonly List<SortOption> _sortOptions = [];
public string UID { get; init; } = Guid.NewGuid().ToString();
public string? Name { get; set; }
public List<PlaylistSong> Songs { get; init; } = []; // TODO: Change to "private init" once deserialization is fixed
public List<GroupOption> GroupOptions { get; set; } = [];
public List<SortOption> SortOptions { get; set; } = [];
public bool IsLocked { get; set; }
public string? Name { get; private set; }
public IReadOnlyList<PlaylistSong> Songs => _songs;
public IReadOnlyList<GroupOption> GroupOptions => _groupOptions;
public IReadOnlyList<SortOption> SortOptions => _sortOptions;
public bool IsLocked { get; private set; }
public event EventHandler<PlaylistUpdatedEventArgs>? PlaylistUpdated;
public Playlist()
{
}
public Playlist(string? name)
{
Name = name;
}
internal static Playlist Restore(string uid, string? name, IEnumerable<PlaylistSong> songs, IEnumerable<GroupOption> groupOptions, IEnumerable<SortOption> sortOptions, bool isLocked)
{
Playlist playlist = new(name)
{
UID = uid,
IsLocked = isLocked
};
playlist._songs.AddRange(songs);
playlist._groupOptions.AddRange(groupOptions);
playlist._sortOptions.AddRange(sortOptions);
return playlist;
}
public int IndexOf(PlaylistSong playlistSong)
{
return _songs.IndexOf(playlistSong);
}
public void SetName(string name)
{
if (string.Equals(Name, name, StringComparison.Ordinal))
@@ -88,9 +121,9 @@ public class Playlist
if (playlistSongs.Length == 0)
return;
int insertIndex = index ?? Songs.Count;
int insertIndex = index ?? _songs.Count;
Songs.InsertRange(insertIndex, playlistSongs);
_songs.InsertRange(insertIndex, playlistSongs);
PlaylistUpdatedEventArgs eventArgs = new()
{
@@ -105,7 +138,7 @@ public class Playlist
public void MoveSong(PlaylistSong playlistSong, int newIndex)
{
int currentIndex = Songs.IndexOf(playlistSong);
int currentIndex = _songs.IndexOf(playlistSong);
MoveSong(currentIndex, newIndex);
}
@@ -118,10 +151,10 @@ public class Playlist
if (oldIndex == newIndex)
return;
PlaylistSong playlistSong = Songs[oldIndex];
PlaylistSong playlistSong = _songs[oldIndex];
Songs.Remove(playlistSong);
Songs.Insert(newIndex, playlistSong);
_songs.Remove(playlistSong);
_songs.Insert(newIndex, playlistSong);
PlaylistUpdatedEventArgs eventArgs = new()
{
@@ -141,8 +174,8 @@ public class Playlist
return;
Dictionary<int, PlaylistSong> oldPlaylistSongs = playlistSongs
.OrderBy(Songs.IndexOf)
.ToDictionary(Songs.IndexOf, playlistSong => playlistSong);
.OrderBy(_songs.IndexOf)
.ToDictionary(_songs.IndexOf, playlistSong => playlistSong);
Song[] songs = [.. playlistSongs.Select(playlistSong => playlistSong.Song)];
Song[] sortedSongs = [.. songs.SortBy(sortOptions)];
@@ -158,8 +191,8 @@ public class Playlist
if (newPlaylistSong == playlistSong)
continue;
Songs.RemoveAt(index);
Songs.Insert(index, newPlaylistSong);
_songs.RemoveAt(index);
_songs.Insert(index, newPlaylistSong);
}
PlaylistUpdatedEventArgs eventArgs = new()
@@ -178,11 +211,11 @@ public class Playlist
if (IsLocked)
return;
int[] originalIndexes = [.. playlistSongs.Select(playlistSong => Songs.IndexOf(playlistSong))];
int[] originalIndexes = [.. playlistSongs.Select(playlistSong => _songs.IndexOf(playlistSong))];
PlaylistSong[] shuffledSongs = [.. playlistSongs.Shuffle()];
for (int i = 0; i < originalIndexes.Length; i++)
Songs[originalIndexes[i]] = shuffledSongs[i];
_songs[originalIndexes[i]] = shuffledSongs[i];
PlaylistUpdatedEventArgs eventArgs = new()
{
@@ -200,11 +233,11 @@ public class Playlist
if (IsLocked)
return;
int[] originalIndexes = [.. playlistSongs.Select(playlistSong => Songs.IndexOf(playlistSong))];
PlaylistSong[] reversedSongs = [.. originalIndexes.Select(i => Songs[i]).Reverse()];
int[] originalIndexes = [.. playlistSongs.Select(playlistSong => _songs.IndexOf(playlistSong))];
PlaylistSong[] reversedSongs = [.. originalIndexes.Select(i => _songs[i]).Reverse()];
for (int i = 0; i < originalIndexes.Length; i++)
Songs[originalIndexes[i]] = reversedSongs[i];
_songs[originalIndexes[i]] = reversedSongs[i];
PlaylistUpdatedEventArgs eventArgs = new()
{
@@ -224,7 +257,7 @@ public class Playlist
public void RemoveSongs(int index, int count)
{
PlaylistSong[] playlistSongs = [.. Songs.GetRange(index, count)];
PlaylistSong[] playlistSongs = [.. _songs.GetRange(index, count)];
RemoveSongs(playlistSongs);
}
@@ -243,7 +276,7 @@ public class Playlist
foreach (PlaylistSong playlistSong in playlistSongs)
{
if (Songs.Remove(playlistSong))
if (_songs.Remove(playlistSong))
{
removedSongs.Add(playlistSong);
}