From 1c92c56d7594e8d0ba9c55fa6c6b42545e3ccadc Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Mon, 17 Feb 2025 16:06:08 -0800 Subject: [PATCH] Bug Fix: Adding tracks to playlist while in search When adding tracks to the playlist, clear the search filter first, so the playlist doesn't become all jumbled, or so we don't overflow the playlist indexes. Also add some bug fixes for reversing the arranged to disarranged index lists, so if an arranged index is past the end of the arranged list, as is the case for appending, we shift the indexes forward past the end of the diarranged object list. Extra exception handling was added as well, so these things will only cause a failure to add playlist items at worst, instead of crashing the player entirely. Signed-off-by: Christopher Snowhill --- Playlist/PlaylistController.m | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Playlist/PlaylistController.m b/Playlist/PlaylistController.m index 0ecc94658..09b30cc5c 100644 --- a/Playlist/PlaylistController.m +++ b/Playlist/PlaylistController.m @@ -861,12 +861,16 @@ static void *playlistControllerContext = &playlistControllerContext; } - (NSIndexSet *)disarrangeIndexes:(NSIndexSet *)indexes { - if([[self arrangedObjects] count] <= [indexes lastIndex]) return indexes; - NSMutableIndexSet *disarrangedIndexes = [[NSMutableIndexSet alloc] init]; [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *_Nonnull stop) { - [disarrangedIndexes addIndex:[[self content] indexOfObject:[[self arrangedObjects] objectAtIndex:idx]]]; + NSUInteger arrCount = [[self arrangedObjects] count]; + NSUInteger disCount = [[self content] count]; + if(idx >= arrCount) { + [disarrangedIndexes addIndex:idx - arrCount + disCount]; + } else { + [disarrangedIndexes addIndex:[[self content] indexOfObject:[[self arrangedObjects] objectAtIndex:idx]]]; + } }]; return disarrangedIndexes; @@ -895,11 +899,12 @@ static void *playlistControllerContext = &playlistControllerContext; } - (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes { + [self clearFilterPredicate:self]; [self insertObjects:objects atArrangedObjectIndexes:indexes]; - [self rearrangeObjects]; } - (void)untrashObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes { + [self clearFilterPredicate:self]; [self untrashObjects:objects atArrangedObjectIndexes:indexes]; [self rearrangeObjects]; } @@ -919,7 +924,7 @@ static void *playlistControllerContext = &playlistControllerContext; index = range.location; } }]; - NSUInteger count = [[self content] count]; + NSUInteger count = [[self arrangedObjects] count]; if(index > count) { // Ah, oops, bodge fix __block NSMutableIndexSet *replacementIndexes = [[NSMutableIndexSet alloc] init]; @@ -944,8 +949,15 @@ static void *playlistControllerContext = &playlistControllerContext; [super insertObjects:objects atArrangedObjectIndexes:indexes]; } @catch(id anException) { - indexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(count, [objects count])]; - [super insertObjects:objects atArrangedObjectIndexes:indexes]; + // Even further bodge fix + @try { + count = [[self arrangedObjects] count]; + indexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(count, [objects count])]; + [super insertObjects:objects atArrangedObjectIndexes:indexes]; + } + @catch(id anException) { + DLog(@"Exception thrown adding tracks to the playlist: %@", anException); + } } [self commitPersistentStore]; @@ -1836,6 +1848,8 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc } - (void)insertURLsInBackground:(NSDictionary *)input { + [self performSelectorOnMainThread:@selector(clearFilterPredicate:) withObject:self waitUntilDone:YES]; + NSArray *entries = [input objectForKey:@"entries"]; NSUInteger row = [[input objectForKey:@"index"] integerValue]; BOOL sort = [[input objectForKey:@"sort"] boolValue];