[Playlist Insert] Add a special case for filtered

Insertions which occur when the playlist is filtered can try to add past
the end of the playlist. Let's try to dodge that.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
main
Christopher Snowhill 2022-07-12 23:47:03 -07:00
parent 8d231d34d4
commit 13fb6bb141
1 changed files with 17 additions and 0 deletions

View File

@ -847,6 +847,23 @@ static void *playlistControllerContext = &playlistControllerContext;
}
- (void)insertObjects:(NSArray *)objects atArrangedObjectIndexes:(NSIndexSet *)indexes {
// Special case, somehow someone is undoing while a search filter is in place
__block NSUInteger index = NSNotFound;
[indexes enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
if(range.location < index) {
index = range.location;
}
}];
NSUInteger count = [[self content] count];
if(index > count) {
// Ah, oops, bodge fix
__block NSMutableIndexSet *replacementIndexes = [[NSMutableIndexSet alloc] init];
[indexes enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
[replacementIndexes addIndexesInRange:NSMakeRange(range.location - index + count, range.length)];
}];
indexes = replacementIndexes;
}
[[[self undoManager] prepareWithInvocationTarget:self]
removeObjectsAtIndexes:[self disarrangeIndexes:indexes]];
NSString *actionName =