Highlight active playlist. Various optimizations and fixes.
This commit is contained in:
@@ -8,7 +8,7 @@ public interface IPlaylistManager
|
||||
Task InitializeAsync();
|
||||
Task<Playlist> AddPlaylistAsync();
|
||||
void RemovePlaylist(Playlist playlist);
|
||||
Playlist? GetPlaylist(PlaylistSong playlistSong);
|
||||
Playlist? FindPlaylistContaining(PlaylistSong playlistSong);
|
||||
|
||||
event EventHandler? CurrentPlaylistChanged;
|
||||
event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
|
||||
|
||||
@@ -4,10 +4,5 @@ namespace Harmonia.Core.Playlists;
|
||||
|
||||
public interface IPlaylistRepository : IRepository<Playlist>
|
||||
{
|
||||
Playlist? GetPlaylist(PlaylistSong playlistSong);
|
||||
//void AddPlaylist();
|
||||
//void RemovePlaylist(Playlist playlist);
|
||||
|
||||
//event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
|
||||
//event EventHandler<PlaylistRemovedEventArgs> PlaylistRemoved;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
60
Harmonia.Core/Playlists/PlaylistDto.cs
Normal file
60
Harmonia.Core/Playlists/PlaylistDto.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Harmonia.Core.Models;
|
||||
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
internal sealed record PlaylistDto
|
||||
{
|
||||
public string UID { get; init; } = Guid.NewGuid().ToString();
|
||||
public string? Name { get; init; }
|
||||
public List<PlaylistSongDto> Songs { get; init; } = [];
|
||||
public List<GroupOption> GroupOptions { get; init; } = [];
|
||||
public List<SortOption> SortOptions { get; init; } = [];
|
||||
public bool IsLocked { get; init; }
|
||||
|
||||
public static PlaylistDto FromPlaylist(Playlist playlist)
|
||||
{
|
||||
return new PlaylistDto
|
||||
{
|
||||
UID = playlist.UID,
|
||||
Name = playlist.Name,
|
||||
Songs = [.. playlist.Songs.Select(PlaylistSongDto.FromPlaylistSong)],
|
||||
GroupOptions = [.. playlist.GroupOptions],
|
||||
SortOptions = [.. playlist.SortOptions],
|
||||
IsLocked = playlist.IsLocked
|
||||
};
|
||||
}
|
||||
|
||||
public Playlist ToPlaylist()
|
||||
{
|
||||
return Playlist.Restore(
|
||||
UID,
|
||||
Name,
|
||||
Songs.Select(song => song.ToPlaylistSong()),
|
||||
GroupOptions,
|
||||
SortOptions,
|
||||
IsLocked);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record PlaylistSongDto
|
||||
{
|
||||
public string UID { get; init; } = Guid.NewGuid().ToString();
|
||||
public required Song Song { get; init; }
|
||||
|
||||
public static PlaylistSongDto FromPlaylistSong(PlaylistSong playlistSong)
|
||||
{
|
||||
return new PlaylistSongDto
|
||||
{
|
||||
UID = playlistSong.UID,
|
||||
Song = playlistSong.Song
|
||||
};
|
||||
}
|
||||
|
||||
public PlaylistSong ToPlaylistSong()
|
||||
{
|
||||
return new PlaylistSong(Song)
|
||||
{
|
||||
UID = UID
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ public class PlaylistManager : IPlaylistManager
|
||||
{
|
||||
private readonly IPlaylistRepository playlistRepository;
|
||||
private readonly List<Playlist> _playlists = [];
|
||||
private readonly Dictionary<string, Playlist> _playlistsBySongUid = [];
|
||||
|
||||
public IReadOnlyList<Playlist> Playlists => _playlists;
|
||||
|
||||
@@ -37,6 +38,11 @@ public class PlaylistManager : IPlaylistManager
|
||||
foreach (Playlist playlist in _playlists)
|
||||
{
|
||||
playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
|
||||
foreach (PlaylistSong song in playlist.Songs)
|
||||
{
|
||||
_playlistsBySongUid[song.UID] = playlist;
|
||||
}
|
||||
}
|
||||
|
||||
CurrentPlaylist = _playlists.Count > 0 ? _playlists[0] : await AddPlaylistAsync();
|
||||
@@ -44,14 +50,16 @@ public class PlaylistManager : IPlaylistManager
|
||||
|
||||
public async Task<Playlist> AddPlaylistAsync()
|
||||
{
|
||||
Playlist playlist = new()
|
||||
{
|
||||
Name = "New Playlist"
|
||||
};
|
||||
Playlist playlist = new("New Playlist");
|
||||
|
||||
playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
_playlists.Add(playlist);
|
||||
|
||||
foreach (PlaylistSong song in playlist.Songs)
|
||||
{
|
||||
_playlistsBySongUid[song.UID] = playlist;
|
||||
}
|
||||
|
||||
await playlistRepository.SaveAsync(playlist);
|
||||
|
||||
PlaylistAdded?.Invoke(this, new(playlist));
|
||||
@@ -64,14 +72,19 @@ public class PlaylistManager : IPlaylistManager
|
||||
playlist.PlaylistUpdated -= OnPlaylistUpdated;
|
||||
_playlists.Remove(playlist);
|
||||
|
||||
foreach (PlaylistSong song in playlist.Songs)
|
||||
{
|
||||
_playlistsBySongUid.Remove(song.UID);
|
||||
}
|
||||
|
||||
playlistRepository.Delete(playlist);
|
||||
|
||||
PlaylistRemoved?.Invoke(this, new(playlist));
|
||||
}
|
||||
|
||||
public Playlist? GetPlaylist(PlaylistSong playlistSong)
|
||||
public Playlist? FindPlaylistContaining(PlaylistSong playlistSong)
|
||||
{
|
||||
return _playlists.FirstOrDefault(playlist => playlist.Songs.Any(song => song.UID == playlistSong.UID));
|
||||
return _playlistsBySongUid.GetValueOrDefault(playlistSong.UID);
|
||||
}
|
||||
|
||||
private async void OnPlaylistUpdated(object? sender, PlaylistUpdatedEventArgs e)
|
||||
@@ -79,6 +92,22 @@ public class PlaylistManager : IPlaylistManager
|
||||
if (sender is not Playlist playlist)
|
||||
return;
|
||||
|
||||
switch (e.Action)
|
||||
{
|
||||
case PlaylistUpdateAction.Add:
|
||||
foreach (PlaylistSong song in e.Songs)
|
||||
{
|
||||
_playlistsBySongUid[song.UID] = playlist;
|
||||
}
|
||||
break;
|
||||
case PlaylistUpdateAction.Remove:
|
||||
foreach (PlaylistSong song in e.Songs)
|
||||
{
|
||||
_playlistsBySongUid.Remove(song.UID);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
await playlistRepository.SaveAsync(playlist);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,32 @@
|
||||
using Harmonia.Core.Data;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Harmonia.Core.Playlists;
|
||||
|
||||
public class PlaylistRepository : JsonFileRepository<Playlist>, IPlaylistRepository
|
||||
{
|
||||
private static readonly JsonSerializerOptions _options = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
IgnoreReadOnlyProperties = true,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
};
|
||||
|
||||
protected override string DirectoryName => Path.Combine("Playlists");
|
||||
|
||||
public Playlist? GetPlaylist(PlaylistSong playlistSong)
|
||||
protected override async Task<Playlist> DeserializeAsync(Stream stream)
|
||||
{
|
||||
return Get().FirstOrDefault(playlist => playlist.Songs.Contains(playlistSong));
|
||||
PlaylistDto dto = await JsonSerializer.DeserializeAsync<PlaylistDto>(stream, _options) ?? new();
|
||||
|
||||
return dto.ToPlaylist();
|
||||
}
|
||||
|
||||
protected override Task<string> SerializeAsync(Playlist playlist)
|
||||
{
|
||||
PlaylistDto dto = PlaylistDto.FromPlaylist(playlist);
|
||||
|
||||
return Task.FromResult(JsonSerializer.Serialize(dto, _options));
|
||||
}
|
||||
|
||||
protected override string GetNewFileName()
|
||||
|
||||
Reference in New Issue
Block a user