public void Shuffle ()
{
if (!Populate) {
if (current_track == null)
return;
int enabled_count = EnabledCount;
int first_view_order = (int)CurrentTrackViewOrder;
int last_view_order = first_view_order + enabled_count;
// If the current track is playing, don't shuffle it
if (ServiceManager.PlayerEngine.IsPlaying (current_track))
first_view_order++;
// Nothing to do if less than 2 tracks
if (last_view_order - first_view_order < 2)
return;
// Save the current_track index, so we can update the current track
// to be whatever one is at that position after we shuffle them -- assuming
// the current_track isn't already playing.
int current_index = TrackModel.IndexOf (current_track);
// Setup a function that will return a random ViewOrder in the range we want
var rand = new Random ();
var func_id = "play-queue-shuffle-order-" + rand.NextDouble ().ToString ();
var view_orders = Enumerable.Range (first_view_order, last_view_order)
.OrderBy (a => rand.NextDouble ())
.ToList ();
int i = 0;
BinaryFunction.Add (func_id, (f, b) => view_orders[i++]);
ServiceManager.DbConnection.Execute (
"UPDATE CorePlaylistEntries SET ViewOrder = HYENA_BINARY_FUNCTION (?, NULL, NULL) WHERE PlaylistID = ? AND ViewOrder >= ?",
func_id, DbId, first_view_order
);
BinaryFunction.Remove (func_id);
Reload ();
// Update the current track unless it was playing (and therefore wasn't moved)
if (!ServiceManager.PlayerEngine.IsPlaying (current_track)) {
SetCurrentTrack (TrackModel[current_index] as DatabaseTrackInfo);
}
}
}