Added randomize/reverse songs logic. Added initial UI-side locking logic.
This commit is contained in:
@@ -14,6 +14,36 @@ public class Playlist
|
||||
|
||||
public event EventHandler<PlaylistUpdatedEventArgs>? PlaylistUpdated;
|
||||
|
||||
public void Lock()
|
||||
{
|
||||
IsLocked = true;
|
||||
|
||||
PlaylistUpdatedEventArgs eventArgs = new()
|
||||
{
|
||||
Action = PlaylistUpdateAction.Lock,
|
||||
Index = -1,
|
||||
Count = 0,
|
||||
Songs = []
|
||||
};
|
||||
|
||||
PlaylistUpdated?.Invoke(this, eventArgs);
|
||||
}
|
||||
|
||||
public void Unlock()
|
||||
{
|
||||
IsLocked = false;
|
||||
|
||||
PlaylistUpdatedEventArgs eventArgs = new()
|
||||
{
|
||||
Action = PlaylistUpdateAction.Unlock,
|
||||
Index = -1,
|
||||
Count = 0,
|
||||
Songs = []
|
||||
};
|
||||
|
||||
PlaylistUpdated?.Invoke(this, eventArgs);
|
||||
}
|
||||
|
||||
public void AddSong(Song song, int? index = null)
|
||||
{
|
||||
AddSongs([song], index);
|
||||
@@ -83,6 +113,9 @@ public class Playlist
|
||||
|
||||
public void SortSongs(PlaylistSong[] playlistSongs, SortOption[] sortOptions)
|
||||
{
|
||||
if (IsLocked)
|
||||
return;
|
||||
|
||||
Dictionary<int, PlaylistSong> oldPlaylistSongs = playlistSongs
|
||||
.OrderBy(Songs.IndexOf)
|
||||
.ToDictionary(Songs.IndexOf, playlistSong => playlistSong);
|
||||
@@ -116,6 +149,50 @@ public class Playlist
|
||||
PlaylistUpdated?.Invoke(this, eventArgs);
|
||||
}
|
||||
|
||||
public void Randomize(PlaylistSong[] playlistSongs)
|
||||
{
|
||||
if (IsLocked)
|
||||
return;
|
||||
|
||||
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];
|
||||
|
||||
PlaylistUpdatedEventArgs eventArgs = new()
|
||||
{
|
||||
Action = PlaylistUpdateAction.Reset,
|
||||
Index = -1,
|
||||
Count = playlistSongs.Length,
|
||||
Songs = playlistSongs
|
||||
};
|
||||
|
||||
PlaylistUpdated?.Invoke(this, eventArgs);
|
||||
}
|
||||
|
||||
public void Reverse(PlaylistSong[] playlistSongs)
|
||||
{
|
||||
if (IsLocked)
|
||||
return;
|
||||
|
||||
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];
|
||||
|
||||
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);
|
||||
|
||||
@@ -21,5 +21,7 @@ public enum PlaylistUpdateAction
|
||||
/// <summary>
|
||||
/// The tags of the songs in the collection were refreshed.
|
||||
/// </summary>
|
||||
Refresh
|
||||
Refresh,
|
||||
Lock,
|
||||
Unlock
|
||||
}
|
||||
Reference in New Issue
Block a user