2020-09-24 21:04:21 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using MediaBrowser.Model.SyncPlay;
|
|
|
|
|
2020-11-14 22:40:01 +00:00
|
|
|
namespace MediaBrowser.Controller.SyncPlay.Queue
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class PlayQueueManager.
|
|
|
|
/// </summary>
|
2020-10-22 13:51:58 +00:00
|
|
|
public class PlayQueueManager
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Placeholder index for when no item is playing.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// <value>The no-playing item index.</value>
|
|
|
|
private const int NoPlayingItemIndex = -1;
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Random number generator used to shuffle lists.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// <value>The random number generator.</value>
|
2020-11-16 16:40:19 +00:00
|
|
|
private readonly Random _randomNumberGenerator = new Random();
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Initializes a new instance of the <see cref="PlayQueueManager" /> class.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
public PlayQueueManager()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Gets the playing item index.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// <value>The playing item index.</value>
|
|
|
|
public int PlayingItemIndex { get; private set; }
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Gets the last time the queue has been changed.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The last time the queue has been changed.</value>
|
|
|
|
public DateTime LastChange { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the shuffle mode.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <value>The shuffle mode.</value>
|
|
|
|
public GroupShuffleMode ShuffleMode { get; private set; } = GroupShuffleMode.Sorted;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Gets the repeat mode.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <value>The repeat mode.</value>
|
|
|
|
public GroupRepeatMode RepeatMode { get; private set; } = GroupRepeatMode.RepeatNone;
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Gets or sets the sorted playlist.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// <value>The sorted playlist, or play queue of the group.</value>
|
|
|
|
private List<QueueItem> SortedPlaylist { get; set; } = new List<QueueItem>();
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Gets or sets the shuffled playlist.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// <value>The shuffled playlist, or play queue of the group.</value>
|
|
|
|
private List<QueueItem> ShuffledPlaylist { get; set; } = new List<QueueItem>();
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Checks if an item is playing.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// <returns><c>true</c> if an item is playing; <c>false</c> otherwise.</returns>
|
|
|
|
public bool IsItemPlaying()
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
return PlayingItemIndex != NoPlayingItemIndex;
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Gets the current playlist considering the shuffle mode.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <returns>The playlist.</returns>
|
2020-11-13 14:13:32 +00:00
|
|
|
public IReadOnlyList<QueueItem> GetPlaylist()
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
return GetPlaylistInternal();
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-22 13:49:37 +00:00
|
|
|
/// Sets a new playlist. Playing item is reset.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="items">The new items of the playlist.</param>
|
2020-11-15 16:03:27 +00:00
|
|
|
public void SetPlaylist(IReadOnlyList<Guid> items)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
SortedPlaylist.Clear();
|
|
|
|
ShuffledPlaylist.Clear();
|
|
|
|
|
2020-09-24 21:04:21 +00:00
|
|
|
SortedPlaylist = CreateQueueItemsFromArray(items);
|
2020-10-22 13:49:37 +00:00
|
|
|
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
|
|
|
{
|
2020-11-16 16:40:19 +00:00
|
|
|
ShuffledPlaylist = new List<QueueItem>(SortedPlaylist);
|
2020-11-13 14:13:32 +00:00
|
|
|
Shuffle(ShuffledPlaylist);
|
2020-10-22 13:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PlayingItemIndex = NoPlayingItemIndex;
|
2020-09-24 21:04:21 +00:00
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-22 13:49:37 +00:00
|
|
|
/// Appends new items to the playlist. The specified order is mantained.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="items">The items to add to the playlist.</param>
|
2020-11-15 16:03:27 +00:00
|
|
|
public void Queue(IReadOnlyList<Guid> items)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
var newItems = CreateQueueItemsFromArray(items);
|
|
|
|
|
2020-10-22 13:49:37 +00:00
|
|
|
SortedPlaylist.AddRange(newItems);
|
2020-09-24 21:04:21 +00:00
|
|
|
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
|
|
|
{
|
|
|
|
ShuffledPlaylist.AddRange(newItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-13 14:13:32 +00:00
|
|
|
/// Shuffles the playlist. Shuffle mode is changed. The playlist gets re-shuffled if already shuffled.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
|
|
|
public void ShufflePlaylist()
|
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
if (PlayingItemIndex == NoPlayingItemIndex)
|
|
|
|
{
|
2020-11-16 16:40:19 +00:00
|
|
|
ShuffledPlaylist = new List<QueueItem>(SortedPlaylist);
|
2020-11-13 14:13:32 +00:00
|
|
|
Shuffle(ShuffledPlaylist);
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
2020-10-22 13:49:37 +00:00
|
|
|
else if (ShuffleMode.Equals(GroupShuffleMode.Sorted))
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
// First time shuffle.
|
2020-09-24 21:04:21 +00:00
|
|
|
var playingItem = SortedPlaylist[PlayingItemIndex];
|
2020-11-16 16:40:19 +00:00
|
|
|
ShuffledPlaylist = new List<QueueItem>(SortedPlaylist);
|
2020-09-24 21:04:21 +00:00
|
|
|
ShuffledPlaylist.RemoveAt(PlayingItemIndex);
|
2020-11-13 14:13:32 +00:00
|
|
|
Shuffle(ShuffledPlaylist);
|
2020-11-16 16:40:19 +00:00
|
|
|
ShuffledPlaylist.Insert(0, playingItem);
|
2020-09-24 21:04:21 +00:00
|
|
|
PlayingItemIndex = 0;
|
|
|
|
}
|
2020-10-22 13:49:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Re-shuffle playlist.
|
|
|
|
var playingItem = ShuffledPlaylist[PlayingItemIndex];
|
|
|
|
ShuffledPlaylist.RemoveAt(PlayingItemIndex);
|
2020-11-13 14:13:32 +00:00
|
|
|
Shuffle(ShuffledPlaylist);
|
2020-11-16 16:40:19 +00:00
|
|
|
ShuffledPlaylist.Insert(0, playingItem);
|
2020-10-22 13:49:37 +00:00
|
|
|
PlayingItemIndex = 0;
|
|
|
|
}
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
ShuffleMode = GroupShuffleMode.Shuffle;
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resets the playlist to sorted mode. Shuffle mode is changed.
|
|
|
|
/// </summary>
|
2020-10-22 13:49:37 +00:00
|
|
|
public void RestoreSortedPlaylist()
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
if (PlayingItemIndex != NoPlayingItemIndex)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
var playingItem = ShuffledPlaylist[PlayingItemIndex];
|
|
|
|
PlayingItemIndex = SortedPlaylist.IndexOf(playingItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
ShuffledPlaylist.Clear();
|
|
|
|
|
|
|
|
ShuffleMode = GroupShuffleMode.Sorted;
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-22 13:49:37 +00:00
|
|
|
/// Clears the playlist. Shuffle mode is preserved.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="clearPlayingItem">Whether to remove the playing item as well.</param>
|
|
|
|
public void ClearPlaylist(bool clearPlayingItem)
|
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
var playingItem = GetPlayingItem();
|
2020-09-24 21:04:21 +00:00
|
|
|
SortedPlaylist.Clear();
|
|
|
|
ShuffledPlaylist.Clear();
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
|
|
|
|
if (!clearPlayingItem && playingItem != null)
|
|
|
|
{
|
|
|
|
SortedPlaylist.Add(playingItem);
|
|
|
|
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
ShuffledPlaylist.Add(playingItem);
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
2020-11-13 14:13:32 +00:00
|
|
|
|
2020-10-22 13:49:37 +00:00
|
|
|
PlayingItemIndex = 0;
|
2020-10-22 13:51:58 +00:00
|
|
|
}
|
2020-10-22 13:49:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
PlayingItemIndex = NoPlayingItemIndex;
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-22 13:49:37 +00:00
|
|
|
/// Adds new items to the playlist right after the playing item. The specified order is mantained.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="items">The items to add to the playlist.</param>
|
2020-11-15 16:03:27 +00:00
|
|
|
public void QueueNext(IReadOnlyList<Guid> items)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
var newItems = CreateQueueItemsFromArray(items);
|
|
|
|
|
|
|
|
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
var playingItem = GetPlayingItem();
|
|
|
|
var sortedPlayingItemIndex = SortedPlaylist.IndexOf(playingItem);
|
|
|
|
// Append items to sorted and shuffled playlist as they are.
|
|
|
|
SortedPlaylist.InsertRange(sortedPlayingItemIndex + 1, newItems);
|
2020-09-24 21:04:21 +00:00
|
|
|
ShuffledPlaylist.InsertRange(PlayingItemIndex + 1, newItems);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SortedPlaylist.InsertRange(PlayingItemIndex + 1, newItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-21 14:42:57 +00:00
|
|
|
/// Gets playlist identifier of the playing item, if any.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-10-21 14:42:57 +00:00
|
|
|
/// <returns>The playlist identifier of the playing item.</returns>
|
2020-12-04 19:15:16 +00:00
|
|
|
public Guid GetPlayingItemPlaylistId()
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
var playingItem = GetPlayingItem();
|
2020-12-04 19:15:16 +00:00
|
|
|
return playingItem?.PlaylistItemId ?? Guid.Empty;
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-21 14:42:57 +00:00
|
|
|
/// Gets the playing item identifier, if any.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-10-21 14:42:57 +00:00
|
|
|
/// <returns>The playing item identifier.</returns>
|
2020-09-24 21:04:21 +00:00
|
|
|
public Guid GetPlayingItemId()
|
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
var playingItem = GetPlayingItem();
|
2020-11-16 16:40:19 +00:00
|
|
|
return playingItem?.ItemId ?? Guid.Empty;
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-21 14:42:57 +00:00
|
|
|
/// Sets the playing item using its identifier. If not in the playlist, the playing item is reset.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-10-21 14:42:57 +00:00
|
|
|
/// <param name="itemId">The new playing item identifier.</param>
|
2020-09-24 21:04:21 +00:00
|
|
|
public void SetPlayingItemById(Guid itemId)
|
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
var playlist = GetPlaylistInternal();
|
2020-11-14 16:07:52 +00:00
|
|
|
PlayingItemIndex = playlist.FindIndex(item => item.ItemId.Equals(itemId));
|
2020-09-24 21:04:21 +00:00
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-10-21 14:42:57 +00:00
|
|
|
/// Sets the playing item using its playlist identifier. If not in the playlist, the playing item is reset.
|
2020-09-24 21:04:21 +00:00
|
|
|
/// </summary>
|
2020-10-21 14:42:57 +00:00
|
|
|
/// <param name="playlistItemId">The new playing item identifier.</param>
|
2020-09-24 21:04:21 +00:00
|
|
|
/// <returns><c>true</c> if playing item has been set; <c>false</c> if item is not in the playlist.</returns>
|
2020-12-04 19:15:16 +00:00
|
|
|
public bool SetPlayingItemByPlaylistId(Guid playlistItemId)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
var playlist = GetPlaylistInternal();
|
2020-12-04 19:15:16 +00:00
|
|
|
PlayingItemIndex = playlist.FindIndex(item => item.PlaylistItemId.Equals(playlistItemId));
|
2020-09-24 21:04:21 +00:00
|
|
|
LastChange = DateTime.UtcNow;
|
2020-11-14 16:07:52 +00:00
|
|
|
|
2020-10-22 13:49:37 +00:00
|
|
|
return PlayingItemIndex != NoPlayingItemIndex;
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the playing item using its position. If not in range, the playing item is reset.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playlistIndex">The new playing item index.</param>
|
|
|
|
public void SetPlayingItemByIndex(int playlistIndex)
|
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
var playlist = GetPlaylistInternal();
|
|
|
|
if (playlistIndex < 0 || playlistIndex > playlist.Count)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
PlayingItemIndex = NoPlayingItemIndex;
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PlayingItemIndex = playlistIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes items from the playlist. If not removed, the playing item is preserved.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playlistItemIds">The items to remove.</param>
|
|
|
|
/// <returns><c>true</c> if playing item got removed; <c>false</c> otherwise.</returns>
|
2020-12-04 19:15:16 +00:00
|
|
|
public bool RemoveFromPlaylist(IReadOnlyList<Guid> playlistItemIds)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 13:49:37 +00:00
|
|
|
var playingItem = GetPlayingItem();
|
|
|
|
|
2020-11-15 16:03:27 +00:00
|
|
|
SortedPlaylist.RemoveAll(item => playlistItemIds.Contains(item.PlaylistItemId));
|
|
|
|
ShuffledPlaylist.RemoveAll(item => playlistItemIds.Contains(item.PlaylistItemId));
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
|
|
|
|
if (playingItem != null)
|
|
|
|
{
|
|
|
|
if (playlistItemIds.Contains(playingItem.PlaylistItemId))
|
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Playing item has been removed, picking previous item.
|
2020-09-24 21:04:21 +00:00
|
|
|
PlayingItemIndex--;
|
|
|
|
if (PlayingItemIndex < 0)
|
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Was first element, picking next if available.
|
2020-10-22 13:49:37 +00:00
|
|
|
// Default to no playing item otherwise.
|
2020-11-13 14:13:32 +00:00
|
|
|
PlayingItemIndex = SortedPlaylist.Count > 0 ? 0 : NoPlayingItemIndex;
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-21 13:46:50 +00:00
|
|
|
// Restoring playing item.
|
2020-09-24 21:04:21 +00:00
|
|
|
SetPlayingItemByPlaylistId(playingItem.PlaylistItemId);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Moves an item in the playlist to another position.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playlistItemId">The item to move.</param>
|
|
|
|
/// <param name="newIndex">The new position.</param>
|
|
|
|
/// <returns><c>true</c> if the item has been moved; <c>false</c> otherwise.</returns>
|
2020-12-04 19:15:16 +00:00
|
|
|
public bool MovePlaylistItem(Guid playlistItemId, int newIndex)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
var playlist = GetPlaylistInternal();
|
2020-10-22 13:49:37 +00:00
|
|
|
var playingItem = GetPlayingItem();
|
2020-09-24 21:04:21 +00:00
|
|
|
|
2020-12-04 19:15:16 +00:00
|
|
|
var oldIndex = playlist.FindIndex(item => item.PlaylistItemId.Equals(playlistItemId));
|
2020-10-22 13:49:37 +00:00
|
|
|
if (oldIndex < 0)
|
|
|
|
{
|
2020-09-24 21:04:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:13:32 +00:00
|
|
|
var queueItem = playlist[oldIndex];
|
|
|
|
playlist.RemoveAt(oldIndex);
|
2020-11-15 16:03:27 +00:00
|
|
|
newIndex = Math.Clamp(newIndex, 0, playlist.Count);
|
2020-11-13 14:13:32 +00:00
|
|
|
playlist.Insert(newIndex, queueItem);
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
LastChange = DateTime.UtcNow;
|
2020-11-13 14:13:32 +00:00
|
|
|
PlayingItemIndex = playlist.IndexOf(playingItem);
|
2020-09-24 21:04:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resets the playlist to its initial state.
|
|
|
|
/// </summary>
|
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
SortedPlaylist.Clear();
|
|
|
|
ShuffledPlaylist.Clear();
|
2020-10-22 13:49:37 +00:00
|
|
|
PlayingItemIndex = NoPlayingItemIndex;
|
2020-09-24 21:04:21 +00:00
|
|
|
ShuffleMode = GroupShuffleMode.Sorted;
|
|
|
|
RepeatMode = GroupRepeatMode.RepeatNone;
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the repeat mode.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mode">The new mode.</param>
|
2020-10-22 14:32:15 +00:00
|
|
|
public void SetRepeatMode(GroupRepeatMode mode)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 14:32:15 +00:00
|
|
|
RepeatMode = mode;
|
2020-09-24 21:04:21 +00:00
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the shuffle mode.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mode">The new mode.</param>
|
2020-10-22 14:32:15 +00:00
|
|
|
public void SetShuffleMode(GroupShuffleMode mode)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 14:32:15 +00:00
|
|
|
if (mode.Equals(GroupShuffleMode.Shuffle))
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
2020-10-22 14:32:15 +00:00
|
|
|
ShufflePlaylist();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RestoreSortedPlaylist();
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Toggles the shuffle mode between sorted and shuffled.
|
|
|
|
/// </summary>
|
|
|
|
public void ToggleShuffleMode()
|
|
|
|
{
|
2020-10-22 14:32:15 +00:00
|
|
|
if (ShuffleMode.Equals(GroupShuffleMode.Sorted))
|
|
|
|
{
|
|
|
|
ShufflePlaylist();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RestoreSortedPlaylist();
|
|
|
|
}
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the next item in the playlist considering repeat mode and shuffle mode.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The next item in the playlist.</returns>
|
|
|
|
public QueueItem GetNextItemPlaylistId()
|
|
|
|
{
|
|
|
|
int newIndex;
|
2020-11-13 14:13:32 +00:00
|
|
|
var playlist = GetPlaylistInternal();
|
2020-09-24 21:04:21 +00:00
|
|
|
|
|
|
|
switch (RepeatMode)
|
|
|
|
{
|
|
|
|
case GroupRepeatMode.RepeatOne:
|
|
|
|
newIndex = PlayingItemIndex;
|
|
|
|
break;
|
|
|
|
case GroupRepeatMode.RepeatAll:
|
|
|
|
newIndex = PlayingItemIndex + 1;
|
2020-11-13 14:13:32 +00:00
|
|
|
if (newIndex >= playlist.Count)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
newIndex = 0;
|
|
|
|
}
|
2020-11-13 14:13:32 +00:00
|
|
|
|
2020-09-24 21:04:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newIndex = PlayingItemIndex + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:13:32 +00:00
|
|
|
if (newIndex < 0 || newIndex >= playlist.Count)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return playlist[newIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the next item in the queue as playing item.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns><c>true</c> if the playing item changed; <c>false</c> otherwise.</returns>
|
|
|
|
public bool Next()
|
|
|
|
{
|
|
|
|
if (RepeatMode.Equals(GroupRepeatMode.RepeatOne))
|
|
|
|
{
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayingItemIndex++;
|
2020-11-13 14:13:32 +00:00
|
|
|
if (PlayingItemIndex >= SortedPlaylist.Count)
|
2020-09-24 21:04:21 +00:00
|
|
|
{
|
|
|
|
if (RepeatMode.Equals(GroupRepeatMode.RepeatAll))
|
|
|
|
{
|
|
|
|
PlayingItemIndex = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-15 16:03:27 +00:00
|
|
|
PlayingItemIndex = SortedPlaylist.Count - 1;
|
2020-09-24 21:04:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the previous item in the queue as playing item.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns><c>true</c> if the playing item changed; <c>false</c> otherwise.</returns>
|
|
|
|
public bool Previous()
|
|
|
|
{
|
|
|
|
if (RepeatMode.Equals(GroupRepeatMode.RepeatOne))
|
|
|
|
{
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayingItemIndex--;
|
|
|
|
if (PlayingItemIndex < 0)
|
|
|
|
{
|
|
|
|
if (RepeatMode.Equals(GroupRepeatMode.RepeatAll))
|
|
|
|
{
|
2020-11-13 14:13:32 +00:00
|
|
|
PlayingItemIndex = SortedPlaylist.Count - 1;
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-15 16:03:27 +00:00
|
|
|
PlayingItemIndex = 0;
|
2020-09-24 21:04:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LastChange = DateTime.UtcNow;
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-13 14:13:32 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Shuffles a given list.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="list">The list to shuffle.</param>
|
|
|
|
private void Shuffle<T>(IList<T> list)
|
|
|
|
{
|
|
|
|
int n = list.Count;
|
|
|
|
while (n > 1)
|
|
|
|
{
|
|
|
|
n--;
|
2020-11-16 16:40:19 +00:00
|
|
|
int k = _randomNumberGenerator.Next(n + 1);
|
2020-11-13 14:13:32 +00:00
|
|
|
T value = list[k];
|
|
|
|
list[k] = list[n];
|
|
|
|
list[n] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a list from the array of items. Each item is given an unique playlist identifier.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The list of queue items.</returns>
|
2020-11-15 16:03:27 +00:00
|
|
|
private List<QueueItem> CreateQueueItemsFromArray(IReadOnlyList<Guid> items)
|
2020-11-13 14:13:32 +00:00
|
|
|
{
|
|
|
|
var list = new List<QueueItem>();
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
2020-12-04 19:15:16 +00:00
|
|
|
var queueItem = new QueueItem(item);
|
2020-11-14 22:40:01 +00:00
|
|
|
list.Add(queueItem);
|
2020-11-13 14:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the current playlist considering the shuffle mode.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The playlist.</returns>
|
|
|
|
private List<QueueItem> GetPlaylistInternal()
|
|
|
|
{
|
|
|
|
if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
|
|
|
{
|
|
|
|
return ShuffledPlaylist;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return SortedPlaylist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the current playing item, depending on the shuffle mode.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The playing item.</returns>
|
|
|
|
private QueueItem GetPlayingItem()
|
|
|
|
{
|
|
|
|
if (PlayingItemIndex == NoPlayingItemIndex)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else if (ShuffleMode.Equals(GroupShuffleMode.Shuffle))
|
|
|
|
{
|
|
|
|
return ShuffledPlaylist[PlayingItemIndex];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return SortedPlaylist[PlayingItemIndex];
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 21:04:21 +00:00
|
|
|
}
|
|
|
|
}
|