Skip to content

Commit

Permalink
Bug Fix: Adding tracks to playlist while in search
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kode54 committed Feb 18, 2025
1 parent 2e884b9 commit 1c92c56
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions Playlist/PlaylistController.m
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,16 @@ - (NSUndoManager *)undoManager {
}

- (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;
Expand Down Expand Up @@ -895,11 +899,12 @@ - (NSIndexSet *)rearrangeIndexes:(NSIndexSet *)indexes {
}

- (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];
}
Expand All @@ -919,7 +924,7 @@ - (void)insertObjects:(NSArray *)objects atArrangedObjectIndexes:(NSIndexSet *)i
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];
Expand All @@ -944,8 +949,15 @@ - (void)insertObjects:(NSArray *)objects atArrangedObjectIndexes:(NSIndexSet *)i
[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];
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 1c92c56

Please sign in to comment.