mirror of
https://github.com/ksherlock/ample.git
synced 2025-01-10 07:29:37 +00:00
clean up item moves a bit.
also switch media to be a mutable array (which it secretly was) and modify in-place to be cleaner.
This commit is contained in:
parent
498fd337eb
commit
f2b08c45a3
@ -29,7 +29,7 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
@property NSInteger validCount;
|
@property NSInteger validCount;
|
||||||
@property NSArray *children; // URLs?
|
@property NSMutableArray *children; // URLs?
|
||||||
@property NSString *title;
|
@property NSString *title;
|
||||||
@property NSInteger index;
|
@property NSInteger index;
|
||||||
|
|
||||||
@ -95,30 +95,29 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned count = (unsigned)[_children count];
|
unsigned count = (unsigned)[_children count];
|
||||||
NSMutableArray *tmp = [NSMutableArray arrayWithArray: _children];
|
|
||||||
|
|
||||||
_validCount = newCount;
|
_validCount = newCount;
|
||||||
|
if (!_children) _children = [NSMutableArray new];
|
||||||
|
|
||||||
for (unsigned i = count; i < newCount; ++i) {
|
for (unsigned i = count; i < newCount; ++i) {
|
||||||
MediaItem *item = [MediaItem new];
|
MediaItem *item = [MediaItem new];
|
||||||
[item setIndex: i];
|
[item setIndex: i];
|
||||||
[tmp addObject: item];
|
[_children addObject: item];
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete excess items, if blank. otherwise, mark invalid.
|
// delete excess items, if blank. otherwise, mark invalid.
|
||||||
unsigned ix = 0;
|
unsigned ix = 0;
|
||||||
for(MediaItem *item in tmp) {
|
for(MediaItem *item in _children) {
|
||||||
[item setValid: ix++ < newCount];
|
[item setValid: ix++ < newCount];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned i = newCount; i < count; ++i) {
|
for (unsigned i = newCount; i < count; ++i) {
|
||||||
MediaItem *item = [tmp lastObject];
|
MediaItem *item = [_children lastObject];
|
||||||
if ([item url]) break;
|
if ([item url]) break;
|
||||||
|
|
||||||
[tmp removeLastObject];
|
[_children removeLastObject];
|
||||||
}
|
}
|
||||||
|
|
||||||
[self setChildren: tmp];
|
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,24 +126,45 @@
|
|||||||
BOOL delta = NO;
|
BOOL delta = NO;
|
||||||
if (_validCount == count) return NO;
|
if (_validCount == count) return NO;
|
||||||
|
|
||||||
NSMutableArray *tmp = [NSMutableArray arrayWithArray: _children];
|
|
||||||
for (NSInteger i = _validCount; i < count; ++i) {
|
for (NSInteger i = _validCount; i < count; ++i) {
|
||||||
MediaItem *item = [tmp lastObject];
|
MediaItem *item = [_children lastObject];
|
||||||
if ([item url]) break;
|
if ([item url]) break;
|
||||||
|
|
||||||
[tmp removeLastObject];
|
[_children removeLastObject];
|
||||||
delta = YES;
|
delta = YES;
|
||||||
}
|
}
|
||||||
if (delta) {
|
if (delta) {
|
||||||
[self setChildren: tmp];
|
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-(BOOL)moveItemFrom: (NSInteger)oldIndex to: (NSInteger)newIndex {
|
||||||
|
if (newIndex == oldIndex) return NO;
|
||||||
|
NSUInteger count = [_children count];
|
||||||
|
if (oldIndex >= count) return NO;
|
||||||
|
|
||||||
|
MediaItem *item = [_children objectAtIndex: oldIndex];
|
||||||
|
[_children removeObjectAtIndex: oldIndex];
|
||||||
|
if (newIndex > oldIndex) newIndex--;
|
||||||
|
if (newIndex >= count) {
|
||||||
|
[_children addObject: item];
|
||||||
|
} else {
|
||||||
|
[_children insertObject: item atIndex: newIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
// re-index and re-validate.
|
||||||
|
unsigned ix = 0;
|
||||||
|
for (MediaItem *item in _children) {
|
||||||
|
[item setIndex: ix];
|
||||||
|
[item setValid: ix < _validCount];
|
||||||
|
++ix;
|
||||||
|
}
|
||||||
|
[self pruneChildren];
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@implementation MediaItem
|
@implementation MediaItem
|
||||||
|
|
||||||
-(instancetype)initWithURL: (NSURL *)url {
|
-(instancetype)initWithURL: (NSURL *)url {
|
||||||
@ -539,25 +559,10 @@ static NSString *kDragType = @"private.ample.media";
|
|||||||
|
|
||||||
NSInteger oldIndex = indexes[1];
|
NSInteger oldIndex = indexes[1];
|
||||||
|
|
||||||
NSMutableArray *array = [[cat children] mutableCopy];
|
[cat moveItemFrom: oldIndex to: index];
|
||||||
MediaItem *it = [array objectAtIndex: oldIndex];
|
[self rebuildArgs];
|
||||||
|
|
||||||
[array removeObjectAtIndex: oldIndex];
|
|
||||||
if (index > [array count]) {
|
|
||||||
[array addObject: it];
|
|
||||||
} else if (index < oldIndex) {
|
|
||||||
[array insertObject: it atIndex: index];
|
|
||||||
} else {
|
|
||||||
[array insertObject: it atIndex: index-1]; //?
|
|
||||||
}
|
|
||||||
unsigned ix = 0;
|
|
||||||
for (MediaItem *it in array) {
|
|
||||||
[it setIndex: ix++];
|
|
||||||
}
|
|
||||||
[cat setChildren: array];
|
|
||||||
|
|
||||||
[_outlineView reloadItem: cat reloadChildren: YES];
|
[_outlineView reloadItem: cat reloadChildren: YES];
|
||||||
[self rebuildArgs];
|
|
||||||
return YES;
|
return YES;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user