Added logic for tags and audio engine.
This commit is contained in:
163
Harmonia.Core/Playlists/Playlist.cs
Normal file
163
Harmonia.Core/Playlists/Playlist.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using Harmonia.Core.Extensions;
|
||||
using Harmonia.Core.Models;
|
||||
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
public class Playlist
|
||||
{
|
||||
private readonly List<PlaylistSong> _songs = [];
|
||||
|
||||
public string UID { get; init; } = Guid.NewGuid().ToString();
|
||||
public string? Name { 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 = null)
|
||||
{
|
||||
AddSongs([song], 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 = null)
|
||||
{
|
||||
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.RemoveAt(index);
|
||||
_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)
|
||||
{
|
||||
PlaylistSong[] playlistSongs = [.. _songs.GetRange(index, count)];
|
||||
|
||||
RemoveSongs(playlistSongs);
|
||||
}
|
||||
|
||||
public void RemoveSong(PlaylistSong playlistSong)
|
||||
{
|
||||
RemoveSongs([playlistSong]);
|
||||
}
|
||||
|
||||
public void RemoveSongs(PlaylistSong[] playlistSongs)
|
||||
{
|
||||
if (IsLocked)
|
||||
return;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user