Added playlist logic.

This commit is contained in:
2025-02-16 19:07:07 -05:00
parent 8f3b07543f
commit cccf0b57a5
4 changed files with 228 additions and 1 deletions

View File

@@ -0,0 +1,21 @@
namespace Harmonia.Core.Events;
public enum PlaylistUpdateAction
{
/// <summary>
/// An item was added to the collection.
/// </summary>
Add,
/// <summary>
/// An item was removed from the collection.
/// </summary>
Remove,
/// <summary>
/// An item was moved within the collection.
/// </summary>
Move,
/// <summary>
/// The contents of the collection changed dramatically.
/// </summary>
Reset
}

View File

@@ -0,0 +1,12 @@
using Harmonia.Core.Models;
namespace Harmonia.Core.Events;
public class PlaylistUpdatedEventArgs : EventArgs
{
public required PlaylistUpdateAction Action { get; init; }
public required int Index { get; init; }
public int? NewIndex { get; init; } = -1;
public required int Count { get; init; }
public required PlaylistSong[] Songs { get; init; } = [];
}

View File

@@ -0,0 +1,34 @@
using Harmonia.Core.Models;
using System.ComponentModel;
using System.Reflection;
namespace Harmonia.Core.Extensions;
public static class EnumerableExtensions
{
public static IOrderedEnumerable<T> SortBy<T>(this IEnumerable<T> source, SortOption[] sortOptions)
{
IOrderedEnumerable<T> orderedQuery = source.OrderBy(x => 0);
Type type = typeof(T);
foreach (SortOption sortOption in sortOptions)
{
PropertyInfo? property = type.GetProperty(sortOption.FieldName);
if (property == null)
continue;
switch (sortOption.Direction)
{
case ListSortDirection.Ascending:
orderedQuery = orderedQuery.ThenBy(x => property.GetValue(x));
break;
case ListSortDirection.Descending:
orderedQuery = orderedQuery.ThenByDescending(x => property.GetValue(x));
break;
}
}
return orderedQuery;
}
}

View File

@@ -1,4 +1,7 @@
namespace Harmonia.Core.Models;
using Harmonia.Core.Events;
using Harmonia.Core.Extensions;
namespace Harmonia.Core.Models;
public class Playlist(string name)
{
@@ -8,4 +11,161 @@ public class Playlist(string name)
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)
{
AddSongs([song], index);
}
public void AddSongs(Song[] songs, int? index)
{
PlaylistSong[] playlistSongs = songs.Select(song => new PlaylistSong(song)).ToArray();
AddSongs(playlistSongs, index);
}
private void AddSongs(PlaylistSong[] playlistSongs, int? index)
{
if (IsLocked)
return;
int insertIndex = index ?? Songs.Count;
Songs.InsertRange(insertIndex, playlistSongs);
PlaylistUpdatedEventArgs eventArgs = new()
{
Action = PlaylistUpdateAction.Add,
Index = insertIndex,
Count = playlistSongs.Length,
Songs = playlistSongs
};
PlaylistUpdated?.Invoke(this, eventArgs);
}
public void MoveSong(PlaylistSong playlistSong, int newIndex)
{
int currentIndex = Songs.IndexOf(playlistSong);
MoveSong(currentIndex, newIndex);
}
public void MoveSong(int oldIndex, int newIndex)
{
if (IsLocked)
return;
if (oldIndex == newIndex)
return;
PlaylistSong playlistSong = Songs[oldIndex];
Songs.Remove(playlistSong);
Songs.Insert(newIndex, playlistSong);
PlaylistUpdatedEventArgs eventArgs = new()
{
Action = PlaylistUpdateAction.Move,
Index = oldIndex,
Count = 1,
Songs = [playlistSong],
NewIndex = newIndex
};
PlaylistUpdated?.Invoke(this, eventArgs);
}
public void SortSongs(PlaylistSong[] playlistSongs, SortOption[] sortOptions)
{
Dictionary<int, PlaylistSong> oldPlaylistSongs = playlistSongs
.OrderBy(Songs.IndexOf)
.ToDictionary(Songs.IndexOf, playlistSong => playlistSong);
Song[] songs = playlistSongs.Select(playlistSong => playlistSong.Song).ToArray();
Song[] sortedSongs = [.. songs.SortBy(sortOptions)];
int currentSortIndex = 0;
foreach (int index in oldPlaylistSongs.Keys)
{
PlaylistSong playlistSong = oldPlaylistSongs[index];
Song sortedSong = sortedSongs[currentSortIndex++];
PlaylistSong newPlaylistSong = playlistSongs.First(ps => ps.Song == sortedSong);
if (newPlaylistSong == playlistSong)
continue;
Songs.Remove(playlistSong);
Songs.Insert(index, newPlaylistSong);
}
PlaylistUpdatedEventArgs eventArgs = new()
{
Action = PlaylistUpdateAction.Reset,
Index = -1,
Count = playlistSongs.Length,
Songs = playlistSongs
};
PlaylistUpdated?.Invoke(this, eventArgs);
}
public void RemoveSong(int index)
{
RemoveSongs(index, 1);
}
public void RemoveSongs(int index, int count)
{
if (IsLocked)
return;
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);
}
public void RemoveSong(PlaylistSong playlistSong)
{
RemoveSongs([playlistSong]);
}
public void RemoveSongs(PlaylistSong[] playlistSongs)
{
List<PlaylistSong> removedSongs = [];
foreach (PlaylistSong playlistSong in playlistSongs)
{
if (Songs.Remove(playlistSong))
{
removedSongs.Add(playlistSong);
}
}
if (removedSongs.Count == 0)
return;
PlaylistUpdatedEventArgs eventArgs = new()
{
Action = PlaylistUpdateAction.Remove,
Index = -1,
Count = removedSongs.Count,
Songs = playlistSongs
};
PlaylistUpdated?.Invoke(this, eventArgs);
}
}