From 2ad9584fb27b1dc5ecf04a862f815e989cae8307 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Thu, 24 Oct 2019 17:18:51 -0700 Subject: [PATCH] Rejigger shuffle list generator for albums mode, to insert the current album at the start of the list, starting from the current track --- Playlist/PlaylistController.m | 68 ++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/Playlist/PlaylistController.m b/Playlist/PlaylistController.m index f2c129cd4..460142f68 100644 --- a/Playlist/PlaylistController.m +++ b/Playlist/PlaylistController.m @@ -809,23 +809,57 @@ if (currentEntry && currentEntry.index >= 0) { - [shuffleList insertObject:currentEntry atIndex:0]; - [currentEntry setShuffleIndex:0]; - - //Need to rejigger so the current entry is at the start now... - int i; - BOOL found = NO; - for (i = 1; i < [shuffleList count] && !found; i++) - { - if ([shuffleList objectAtIndex:i] == currentEntry) - { - found = YES; - [shuffleList removeObjectAtIndex:i]; - } - else { - [[shuffleList objectAtIndex:i] setShuffleIndex: i]; - } - } + if ([self shuffle] == ShuffleAlbums) { + NSString * currentAlbum = currentEntry.album; + if (!currentAlbum) + currentAlbum = @""; + + NSArray * wholeAlbum = [self filterPlaylistOnAlbum:currentAlbum]; + + // First prune the shuffle list of the currently playing album + int i, j; + for (i = 0; i < [shuffleList count];) { + if ([wholeAlbum containsObject:[shuffleList objectAtIndex:i]]) { + [shuffleList removeObjectAtIndex:i]; + } + else { + ++i; + } + } + + // Then insert the playing album at the start, beginning with + // the current track + BOOL found = NO; + for (i = 0, j = 0; i < [wholeAlbum count]; i++) { + if (!found && [wholeAlbum objectAtIndex:i] == currentEntry) { + found = YES; + } + if (found) { + [shuffleList insertObject:[wholeAlbum objectAtIndex:i] atIndex:j]; + ++j; + } + } + + } + else { + [shuffleList insertObject:currentEntry atIndex:0]; + [currentEntry setShuffleIndex:0]; + + //Need to rejigger so the current entry is at the start now... + int i; + BOOL found = NO; + for (i = 1; i < [shuffleList count] && !found; i++) + { + if ([shuffleList objectAtIndex:i] == currentEntry) + { + found = YES; + [shuffleList removeObjectAtIndex:i]; + } + else { + [[shuffleList objectAtIndex:i] setShuffleIndex: i]; + } + } + } } }