mirror of https://github.com/ksherlock/ample.git
switch media bookmark to use a keyed dictionary instead of an array.
An array is fragile in terms of future proofing if I want to re-arrange the layout.
This commit is contained in:
parent
76ac6f777a
commit
4144a6ad03
|
@ -26,7 +26,7 @@ enum {
|
||||||
@protocol MediaNode
|
@protocol MediaNode
|
||||||
-(BOOL)isGroupItem;
|
-(BOOL)isGroupItem;
|
||||||
-(BOOL)isExpandable;
|
-(BOOL)isExpandable;
|
||||||
-(NSInteger) count;
|
-(NSInteger)count;
|
||||||
-(NSInteger)category;
|
-(NSInteger)category;
|
||||||
|
|
||||||
-(NSString *)viewIdentifier;
|
-(NSString *)viewIdentifier;
|
||||||
|
@ -693,63 +693,123 @@ static NSString *kDragType = @"private.ample.media";
|
||||||
[self rebuildArgs];
|
[self rebuildArgs];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static NSString * BookmarkStrings[] = {
|
||||||
|
@"flop_525", @"flop_35", @"hard", @"cdrm", @"cass", @"disk", @"bitb",
|
||||||
|
};
|
||||||
|
static_assert(SIZEOF(BookmarkStrings) == CATEGORY_COUNT, "Missing item");
|
||||||
|
|
||||||
|
static int BookmarkIndex(NSString *str) {
|
||||||
|
if (!str) return -1;
|
||||||
|
for (int i = 0; i < SIZEOF(BookmarkStrings); ++i) {
|
||||||
|
if ([str isEqualToString: BookmarkStrings[i]]) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
-(BOOL)loadBookmark: (NSDictionary *)bookmark {
|
-(BOOL)loadBookmark: (NSDictionary *)bookmark {
|
||||||
|
|
||||||
|
// fragile - depends on order
|
||||||
|
id media = [bookmark objectForKey: @"media"];
|
||||||
|
|
||||||
// if order of indexes change, would need to do a version check.
|
if ([media isKindOfClass: [NSArray class]]) {
|
||||||
|
unsigned ix = 0;
|
||||||
NSArray *media = [bookmark objectForKey: @"media"];
|
for (NSArray *a in (NSArray *)media) {
|
||||||
unsigned ix = 0;
|
if (ix >= CATEGORY_COUNT) {
|
||||||
for (NSArray *a in media) {
|
NSLog(@"MediaViewController: too many categories.");
|
||||||
if (ix >= CATEGORY_COUNT) {
|
break;
|
||||||
NSLog(@"MediaViewController: too many categories.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
MediaCategory *cat = _data[ix];
|
|
||||||
NSInteger count = [cat count];
|
|
||||||
unsigned i = 0;
|
|
||||||
for (NSString *path in a) {
|
|
||||||
if (i >= count) {
|
|
||||||
NSLog(@"MediaViewController: too many files.");
|
|
||||||
break; //
|
|
||||||
}
|
}
|
||||||
MediaItem *item = [cat objectAtIndex: i++];
|
MediaCategory *cat = _data[ix];
|
||||||
if (![path length]) continue;
|
NSInteger count = [cat count];
|
||||||
|
unsigned i = 0;
|
||||||
|
for (NSString *path in a) {
|
||||||
|
if (i >= count) {
|
||||||
|
NSLog(@"MediaViewController: too many files.");
|
||||||
|
break; //
|
||||||
|
}
|
||||||
|
MediaItem *item = [cat objectAtIndex: i++];
|
||||||
|
if (![path length]) continue;
|
||||||
|
|
||||||
if (ix == kIndexBitBanger) {
|
if (ix == kIndexBitBanger) {
|
||||||
[item setString: path];
|
[item setString: path];
|
||||||
} else {
|
} else {
|
||||||
NSURL *url = [NSURL fileURLWithPath: path];
|
NSURL *url = [NSURL fileURLWithPath: path];
|
||||||
[item setUrl: url];
|
[item setUrl: url];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
++ix;
|
||||||
}
|
}
|
||||||
++ix;
|
return YES;
|
||||||
}
|
}
|
||||||
return YES;
|
if ([media isKindOfClass: [NSDictionary class]]) {
|
||||||
|
|
||||||
|
for (NSString *key in (NSDictionary *)media) {
|
||||||
|
NSInteger ix = BookmarkIndex(key);
|
||||||
|
if (ix < 0) {
|
||||||
|
NSLog(@"MediaViewController: unrecognized category: %@", key);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
MediaCategory *cat = _data[ix];
|
||||||
|
NSInteger count = [cat count];
|
||||||
|
NSArray *a = [(NSDictionary *)media objectForKey: key];
|
||||||
|
unsigned i = 0;
|
||||||
|
|
||||||
|
for (NSString *path in a) {
|
||||||
|
if (i >= count) {
|
||||||
|
NSLog(@"MediaViewController: too many files.");
|
||||||
|
break; //
|
||||||
|
}
|
||||||
|
MediaItem *item = [cat objectAtIndex: i++];
|
||||||
|
if (![path length]) continue;
|
||||||
|
|
||||||
|
if (ix == kIndexBitBanger) {
|
||||||
|
[item setString: path];
|
||||||
|
} else {
|
||||||
|
NSURL *url = [NSURL fileURLWithPath: path];
|
||||||
|
[item setUrl: url];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CompressArray(NSMutableArray *array) {
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
NSString *s = [array lastObject];
|
||||||
|
if (!s) return;
|
||||||
|
if ([s length]) return;
|
||||||
|
[array removeLastObject];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-(BOOL)saveBookmark: (NSMutableDictionary *)bookmark {
|
-(BOOL)saveBookmark: (NSMutableDictionary *)bookmark {
|
||||||
|
|
||||||
NSMutableArray *media = [NSMutableArray arrayWithCapacity: CATEGORY_COUNT];
|
|
||||||
|
NSMutableDictionary *media = [NSMutableDictionary new];
|
||||||
|
|
||||||
for (unsigned ix = 0; ix < CATEGORY_COUNT; ++ix) {
|
for (unsigned ix = 0; ix < CATEGORY_COUNT; ++ix) {
|
||||||
|
|
||||||
MediaCategory *cat = _data[ix];
|
MediaCategory *cat = _data[ix];
|
||||||
NSInteger count = [cat validCount];
|
NSInteger count = [cat validCount];
|
||||||
|
if (!count) continue;
|
||||||
|
|
||||||
NSMutableArray *array = [NSMutableArray new];
|
NSMutableArray *array = [NSMutableArray new];
|
||||||
for (NSInteger i = 0; i < count; ++i) {
|
for (NSInteger i = 0; i < count; ++i) {
|
||||||
|
|
||||||
MediaItem *item = [cat objectAtIndex: i];
|
MediaItem *item = [cat objectAtIndex: i];
|
||||||
// todo - bitbanger.
|
|
||||||
NSString *s = [item argument];
|
NSString *s = [item argument];
|
||||||
if (!s) s = @"";
|
if (!s) s = @"";
|
||||||
|
|
||||||
[array addObject: s];
|
[array addObject: s];
|
||||||
}
|
}
|
||||||
[media addObject: array];
|
|
||||||
|
CompressArray(array);
|
||||||
|
|
||||||
|
if ([array count])
|
||||||
|
[media setObject: array forKey: BookmarkStrings[ix]];
|
||||||
}
|
}
|
||||||
|
|
||||||
[bookmark setObject: media forKey: @"media"];
|
[bookmark setObject: media forKey: @"media"];
|
||||||
|
|
Loading…
Reference in New Issue