@@ -29,7 +29,7 @@ @interface MediaCategory : NSObject <MediaNode> {
29
29
30
30
}
31
31
@property NSInteger validCount;
32
- @property NSArray *children; // URLs?
32
+ @property NSMutableArray *children; // URLs?
33
33
@property NSString *title;
34
34
@property NSInteger index;
35
35
@@ -95,30 +95,29 @@ -(BOOL)setItemCount: (unsigned)newCount {
95
95
}
96
96
97
97
unsigned count = (unsigned )[_children count ];
98
- NSMutableArray *tmp = [NSMutableArray arrayWithArray: _children];
99
98
100
99
_validCount = newCount;
100
+ if (!_children) _children = [NSMutableArray new ];
101
101
102
102
for (unsigned i = count; i < newCount; ++i) {
103
103
MediaItem *item = [MediaItem new ];
104
104
[item setIndex: i];
105
- [tmp addObject: item];
105
+ [_children addObject: item];
106
106
}
107
107
108
108
// delete excess items, if blank. otherwise, mark invalid.
109
109
unsigned ix = 0 ;
110
- for (MediaItem *item in tmp ) {
110
+ for (MediaItem *item in _children ) {
111
111
[item setValid: ix++ < newCount];
112
112
}
113
113
114
114
for (unsigned i = newCount; i < count; ++i) {
115
- MediaItem *item = [tmp lastObject ];
115
+ MediaItem *item = [_children lastObject ];
116
116
if ([item url ]) break ;
117
117
118
- [tmp removeLastObject ];
118
+ [_children removeLastObject ];
119
119
}
120
120
121
- [self setChildren: tmp];
122
121
return YES ;
123
122
}
124
123
@@ -127,23 +126,44 @@ -(BOOL)pruneChildren {
127
126
BOOL delta = NO ;
128
127
if (_validCount == count) return NO ;
129
128
130
- NSMutableArray *tmp = [NSMutableArray arrayWithArray: _children];
131
129
for (NSInteger i = _validCount; i < count; ++i) {
132
- MediaItem *item = [tmp lastObject ];
130
+ MediaItem *item = [_children lastObject ];
133
131
if ([item url ]) break ;
134
132
135
- [tmp removeLastObject ];
133
+ [_children removeLastObject ];
136
134
delta = YES ;
137
135
}
138
136
if (delta) {
139
- [self setChildren: tmp];
140
137
return YES ;
141
138
}
142
139
return NO ;
143
140
}
144
- @end
145
141
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 ;
146
146
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
147
167
148
168
@implementation MediaItem
149
169
@@ -539,25 +559,10 @@ - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id<NSDraggingInfo>)
539
559
540
560
NSInteger oldIndex = indexes[1 ];
541
561
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 ];
558
564
559
565
[_outlineView reloadItem: cat reloadChildren: YES ];
560
- [self rebuildArgs ];
561
566
return YES ;
562
567
563
568
}
0 commit comments