Skip to content

Commit f2b08c4

Browse files
committed
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.
1 parent 498fd33 commit f2b08c4

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

Ample/MediaViewController.m

+34-29
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ @interface MediaCategory : NSObject <MediaNode> {
2929

3030
}
3131
@property NSInteger validCount;
32-
@property NSArray *children; // URLs?
32+
@property NSMutableArray *children; // URLs?
3333
@property NSString *title;
3434
@property NSInteger index;
3535

@@ -95,30 +95,29 @@ -(BOOL)setItemCount: (unsigned)newCount {
9595
}
9696

9797
unsigned count = (unsigned)[_children count];
98-
NSMutableArray *tmp = [NSMutableArray arrayWithArray: _children];
9998

10099
_validCount = newCount;
100+
if (!_children) _children = [NSMutableArray new];
101101

102102
for (unsigned i = count; i < newCount; ++i) {
103103
MediaItem *item = [MediaItem new];
104104
[item setIndex: i];
105-
[tmp addObject: item];
105+
[_children addObject: item];
106106
}
107107

108108
// delete excess items, if blank. otherwise, mark invalid.
109109
unsigned ix = 0;
110-
for(MediaItem *item in tmp) {
110+
for(MediaItem *item in _children) {
111111
[item setValid: ix++ < newCount];
112112
}
113113

114114
for (unsigned i = newCount; i < count; ++i) {
115-
MediaItem *item = [tmp lastObject];
115+
MediaItem *item = [_children lastObject];
116116
if ([item url]) break;
117117

118-
[tmp removeLastObject];
118+
[_children removeLastObject];
119119
}
120120

121-
[self setChildren: tmp];
122121
return YES;
123122
}
124123

@@ -127,23 +126,44 @@ -(BOOL)pruneChildren {
127126
BOOL delta = NO;
128127
if (_validCount == count) return NO;
129128

130-
NSMutableArray *tmp = [NSMutableArray arrayWithArray: _children];
131129
for (NSInteger i = _validCount; i < count; ++i) {
132-
MediaItem *item = [tmp lastObject];
130+
MediaItem *item = [_children lastObject];
133131
if ([item url]) break;
134132

135-
[tmp removeLastObject];
133+
[_children removeLastObject];
136134
delta = YES;
137135
}
138136
if (delta) {
139-
[self setChildren: tmp];
140137
return YES;
141138
}
142139
return NO;
143140
}
144-
@end
145141

142+
-(BOOL)moveItemFrom: (NSInteger)oldIndex to: (NSInteger)newIndex {
143+
if (newIndex == oldIndex) return NO;
144+
NSUInteger count = [_children count];
145+
if (oldIndex >= count) return NO;
146146

147+
MediaItem *item = [_children objectAtIndex: oldIndex];
148+
[_children removeObjectAtIndex: oldIndex];
149+
if (newIndex > oldIndex) newIndex--;
150+
if (newIndex >= count) {
151+
[_children addObject: item];
152+
} else {
153+
[_children insertObject: item atIndex: newIndex];
154+
}
155+
156+
// re-index and re-validate.
157+
unsigned ix = 0;
158+
for (MediaItem *item in _children) {
159+
[item setIndex: ix];
160+
[item setValid: ix < _validCount];
161+
++ix;
162+
}
163+
[self pruneChildren];
164+
return YES;
165+
}
166+
@end
147167

148168
@implementation MediaItem
149169

@@ -539,25 +559,10 @@ - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id<NSDraggingInfo>)
539559

540560
NSInteger oldIndex = indexes[1];
541561

542-
NSMutableArray *array = [[cat children] mutableCopy];
543-
MediaItem *it = [array objectAtIndex: oldIndex];
544-
545-
[array removeObjectAtIndex: oldIndex];
546-
if (index > [array count]) {
547-
[array addObject: it];
548-
} else if (index < oldIndex) {
549-
[array insertObject: it atIndex: index];
550-
} else {
551-
[array insertObject: it atIndex: index-1]; //?
552-
}
553-
unsigned ix = 0;
554-
for (MediaItem *it in array) {
555-
[it setIndex: ix++];
556-
}
557-
[cat setChildren: array];
562+
[cat moveItemFrom: oldIndex to: index];
563+
[self rebuildArgs];
558564

559565
[_outlineView reloadItem: cat reloadChildren: YES];
560-
[self rebuildArgs];
561566
return YES;
562567

563568
}

0 commit comments

Comments
 (0)