summaryrefslogtreecommitdiffstats
path: root/Classes/SectionDictionary.m
blob: 9124714c259e6b66561e72e9fcc9701ffd359717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
//  SectionDictionary.m
//  PicCast
//
//  Created by Matthew Handler on 4/23/11.
//  Copyright 2011 Earl Industries. All rights reserved.
//

#import "SectionDictionary.h"


@implementation SectionDictionary

//@synthesize index;

- (SectionDictionary *) init {
	index = 0;
	sortedKeys = [[[NSMutableArray alloc] init] retain];
	indexMap = [[[NSMutableDictionary alloc] init] retain];
	theRealDictionary = [[[NSMutableDictionary alloc] init] retain];
	[super init];
	
	return self;
}

- (id)forwardingTargetForSelector:(SEL)aSelector
{
    return theRealDictionary;
}

- (void) sortKeys {
	[sortedKeys sortUsingComparator:(NSComparator)^(id obj1, id obj2){
		return [obj2 compare:obj1];
    }];
}

- (void) setObject:(id)anObject forKey:(id)aKey {
//	[indexMap setObject:aKey forKey:[NSString stringWithFormat:@"%d", index]];
//	index++;
	[theRealDictionary setObject:anObject forKey:aKey];
	
	[sortedKeys addObject:aKey];
	[self sortKeys];
}

- (NSArray *) allIndices {
	NSLog(@"%@ %@", [indexMap allKeys], [indexMap allValues]);
	return [indexMap allKeys];
}

// lets you push objects into sections without worry about storage
- (void) appendObject:(id)anObject forKey:(id)aKey {
	NSMutableArray *store = [theRealDictionary objectForKey:aKey];
	if (store == nil) {
//		[indexMap setObject:aKey forKey:[NSString stringWithFormat:@"%d", index]];
//		index++;
		
		NSMutableArray *store = [[[NSMutableArray alloc] init] autorelease];
		[store addObject:anObject];
		[theRealDictionary setObject:store forKey:aKey];
		
		[sortedKeys addObject:aKey];
		[self sortKeys];
	}
	else [store addObject:anObject];
	
	//NSLog(@"number of rows: %d", [[theRealDictionary objectForKey:aKey] count]);
}

- (id) objectForIndex:(NSInteger)i {
	return [theRealDictionary objectForKey:[sortedKeys objectAtIndex:i]];
	//return [theRealDictionary objectForKey:[indexMap objectForKey:[NSString stringWithFormat:@"%d", i]]];
}

- (NSString *) keyForIndex:(NSInteger)i {
	return [sortedKeys objectAtIndex:i];
	//return [indexMap objectForKey:[NSString stringWithFormat:@"%d", i]];
}
	 
- (void) dealloc {
	[sortedKeys release];
	[theRealDictionary release];
	[indexMap release];
	[super dealloc];
}


@end