Updated playlist model class. Added playlist tests.
This commit is contained in:
@@ -5,35 +5,37 @@ namespace Harmonia.Core.Models;
|
||||
|
||||
public class Playlist(string name)
|
||||
{
|
||||
public string UID { get; set; } = Guid.NewGuid().ToString();
|
||||
private readonly List<PlaylistSong> _songs = [];
|
||||
|
||||
public string UID { get; init; } = Guid.NewGuid().ToString();
|
||||
public string Name { get; set; } = name;
|
||||
public List<PlaylistSong> Songs { get; set; } = [];
|
||||
public IReadOnlyList<PlaylistSong> Songs => _songs;
|
||||
public List<GroupOption> GroupOptions { get; set; } = [];
|
||||
public List<SortOption> SortOptions { get; set; } = [];
|
||||
public bool IsLocked { get; set; }
|
||||
|
||||
public event EventHandler<PlaylistUpdatedEventArgs>? PlaylistUpdated;
|
||||
|
||||
public void AddSong(Song song, int? index)
|
||||
public void AddSong(Song song, int? index = null)
|
||||
{
|
||||
AddSongs([song], index);
|
||||
}
|
||||
|
||||
public void AddSongs(Song[] songs, int? index)
|
||||
public void AddSongs(Song[] songs, int? index = null)
|
||||
{
|
||||
PlaylistSong[] playlistSongs = songs.Select(song => new PlaylistSong(song)).ToArray();
|
||||
|
||||
AddSongs(playlistSongs, index);
|
||||
}
|
||||
|
||||
private void AddSongs(PlaylistSong[] playlistSongs, int? index)
|
||||
private void AddSongs(PlaylistSong[] playlistSongs, int? index = null)
|
||||
{
|
||||
if (IsLocked)
|
||||
return;
|
||||
|
||||
int insertIndex = index ?? Songs.Count;
|
||||
|
||||
Songs.InsertRange(insertIndex, playlistSongs);
|
||||
_songs.InsertRange(insertIndex, playlistSongs);
|
||||
|
||||
PlaylistUpdatedEventArgs eventArgs = new()
|
||||
{
|
||||
@@ -48,7 +50,7 @@ public class Playlist(string name)
|
||||
|
||||
public void MoveSong(PlaylistSong playlistSong, int newIndex)
|
||||
{
|
||||
int currentIndex = Songs.IndexOf(playlistSong);
|
||||
int currentIndex = _songs.IndexOf(playlistSong);
|
||||
|
||||
MoveSong(currentIndex, newIndex);
|
||||
}
|
||||
@@ -63,8 +65,8 @@ public class Playlist(string name)
|
||||
|
||||
PlaylistSong playlistSong = Songs[oldIndex];
|
||||
|
||||
Songs.Remove(playlistSong);
|
||||
Songs.Insert(newIndex, playlistSong);
|
||||
_songs.Remove(playlistSong);
|
||||
_songs.Insert(newIndex, playlistSong);
|
||||
|
||||
PlaylistUpdatedEventArgs eventArgs = new()
|
||||
{
|
||||
@@ -81,8 +83,8 @@ public class Playlist(string name)
|
||||
public void SortSongs(PlaylistSong[] playlistSongs, SortOption[] sortOptions)
|
||||
{
|
||||
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).ToArray();
|
||||
Song[] sortedSongs = [.. songs.SortBy(sortOptions)];
|
||||
@@ -98,8 +100,8 @@ public class Playlist(string name)
|
||||
if (newPlaylistSong == playlistSong)
|
||||
continue;
|
||||
|
||||
Songs.Remove(playlistSong);
|
||||
Songs.Insert(index, newPlaylistSong);
|
||||
_songs.RemoveAt(index);
|
||||
_songs.Insert(index, newPlaylistSong);
|
||||
}
|
||||
|
||||
PlaylistUpdatedEventArgs eventArgs = new()
|
||||
@@ -120,22 +122,9 @@ public class Playlist(string name)
|
||||
|
||||
public void RemoveSongs(int index, int count)
|
||||
{
|
||||
if (IsLocked)
|
||||
return;
|
||||
PlaylistSong[] playlistSongs = [.. _songs.GetRange(index, count)];
|
||||
|
||||
PlaylistSong[] playlistSongs = [.. Songs.GetRange(index, count)];
|
||||
|
||||
Songs.RemoveRange(index, count);
|
||||
|
||||
PlaylistUpdatedEventArgs eventArgs = new()
|
||||
{
|
||||
Action = PlaylistUpdateAction.Remove,
|
||||
Index = index,
|
||||
Count = count,
|
||||
Songs = playlistSongs
|
||||
};
|
||||
|
||||
PlaylistUpdated?.Invoke(this, eventArgs);
|
||||
RemoveSongs(playlistSongs);
|
||||
}
|
||||
|
||||
public void RemoveSong(PlaylistSong playlistSong)
|
||||
@@ -145,11 +134,14 @@ public class Playlist(string name)
|
||||
|
||||
public void RemoveSongs(PlaylistSong[] playlistSongs)
|
||||
{
|
||||
if (IsLocked)
|
||||
return;
|
||||
|
||||
List<PlaylistSong> removedSongs = [];
|
||||
|
||||
foreach (PlaylistSong playlistSong in playlistSongs)
|
||||
{
|
||||
if (Songs.Remove(playlistSong))
|
||||
if (_songs.Remove(playlistSong))
|
||||
{
|
||||
removedSongs.Add(playlistSong);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user