Added logic for tags and audio engine.
This commit is contained in:
13
Harmonia.Core/Playlists/IPlaylistManager.cs
Normal file
13
Harmonia.Core/Playlists/IPlaylistManager.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
public interface IPlaylistManager
|
||||
{
|
||||
Playlist? CurrentPlaylist { get; set; }
|
||||
|
||||
void AddPlaylist();
|
||||
void RemovePlaylist(Playlist playlist);
|
||||
|
||||
event EventHandler? CurrentPlaylistChanged;
|
||||
event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
|
||||
event EventHandler<PlaylistRemovedEventArgs> PlaylistRemoved;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
6
Harmonia.Core/Playlists/PlaylistAddedEventArgs.cs
Normal file
6
Harmonia.Core/Playlists/PlaylistAddedEventArgs.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
public class PlaylistAddedEventArgs(Playlist playlist) : EventArgs
|
||||
{
|
||||
public readonly Playlist Playlist = playlist;
|
||||
}
|
||||
43
Harmonia.Core/Playlists/PlaylistManager.cs
Normal file
43
Harmonia.Core/Playlists/PlaylistManager.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Harmonia.Core.Data;
|
||||
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
public class PlaylistManager(IRepository<Playlist> playlistRepository) : IPlaylistManager
|
||||
{
|
||||
private Playlist? _currentPlaylist;
|
||||
public Playlist? CurrentPlaylist
|
||||
{
|
||||
get
|
||||
{
|
||||
return _currentPlaylist;
|
||||
}
|
||||
set
|
||||
{
|
||||
_currentPlaylist = value;
|
||||
CurrentPlaylistChanged?.Invoke(this, new());
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler? CurrentPlaylistChanged;
|
||||
public event EventHandler<PlaylistAddedEventArgs>? PlaylistAdded;
|
||||
public event EventHandler<PlaylistRemovedEventArgs>? PlaylistRemoved;
|
||||
|
||||
public void AddPlaylist()
|
||||
{
|
||||
Playlist playlist = new()
|
||||
{
|
||||
Name = "New Playlist"
|
||||
};
|
||||
|
||||
playlistRepository.Save(playlist);
|
||||
|
||||
PlaylistAdded?.Invoke(this, new(playlist));
|
||||
}
|
||||
|
||||
public void RemovePlaylist(Playlist playlist)
|
||||
{
|
||||
playlistRepository.Delete(playlist);
|
||||
|
||||
PlaylistRemoved?.Invoke(this, new(playlist));
|
||||
}
|
||||
}
|
||||
6
Harmonia.Core/Playlists/PlaylistRemovedEventArgs.cs
Normal file
6
Harmonia.Core/Playlists/PlaylistRemovedEventArgs.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
public class PlaylistRemovedEventArgs(Playlist playlist) : EventArgs
|
||||
{
|
||||
public readonly Playlist Playlist = playlist;
|
||||
}
|
||||
22
Harmonia.Core/Playlists/PlaylistRepository.cs
Normal file
22
Harmonia.Core/Playlists/PlaylistRepository.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Harmonia.Core.Data;
|
||||
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
public class PlaylistRepository : JsonFileRepository<Playlist>
|
||||
{
|
||||
protected override string DirectoryName => string.Empty;
|
||||
|
||||
protected override string GetNewFileName()
|
||||
{
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
string shortFileName = $"Playlist{i.ToString().PadLeft(3, '0')}.{Extension}";
|
||||
string filePath = Path.Combine(DirectoryName, shortFileName);
|
||||
|
||||
if (File.Exists(filePath) == false)
|
||||
return shortFileName;
|
||||
}
|
||||
|
||||
throw new Exception("Unable to determine new fileName");
|
||||
}
|
||||
}
|
||||
9
Harmonia.Core/Playlists/PlaylistSong.cs
Normal file
9
Harmonia.Core/Playlists/PlaylistSong.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Harmonia.Core.Models;
|
||||
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
public class PlaylistSong(Song song)
|
||||
{
|
||||
public string UID { get; } = Guid.NewGuid().ToString();
|
||||
public Song Song { get; } = song;
|
||||
}
|
||||
21
Harmonia.Core/Playlists/PlaylistUpdateAction.cs
Normal file
21
Harmonia.Core/Playlists/PlaylistUpdateAction.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
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
|
||||
}
|
||||
10
Harmonia.Core/Playlists/PlaylistUpdatedEventArgs.cs
Normal file
10
Harmonia.Core/Playlists/PlaylistUpdatedEventArgs.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
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; } = [];
|
||||
}
|
||||
Reference in New Issue
Block a user