forked from SVGKit/SVGKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVCGridOfImagesViewController.m
More file actions
151 lines (118 loc) · 4.75 KB
/
Copy pathVCGridOfImagesViewController.m
File metadata and controls
151 lines (118 loc) · 4.75 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#import "VCGridOfImagesViewController.h"
#import "SampleFileInfo.h"
#import "DetailViewController.h"
@interface VCGridOfImagesViewController ()
@property(nonatomic,strong) NSMutableArray* sectionNames;
@property(nonatomic,strong) NSMutableDictionary* itemArraysBySectionName;
@end
@implementation VCGridOfImagesViewController
-(SampleFileInfo*) sampleFileInfoFromDictionary:(NSDictionary*) license
{
NSString* sourceURL = [license objectForKey:@"Source URL"];
NSString* sourceFilename = [license objectForKey:@"Source Local File"];
SampleFileInfo* info = [SampleFileInfo sampleFileInfoWithFilename:sourceFilename URL:(sourceURL != nil) ? [NSURL URLWithString:sourceURL] : nil];
return info;
}
-(void) displayAllSectionsFromDictionary:(NSDictionary*) inputDictionary
{
self.itemArraysBySectionName = [NSMutableDictionary dictionary];
self.sectionNames = [NSMutableArray array];
for( NSString* key in inputDictionary )
{
[self.sectionNames addObject:key];
NSDictionary* licensesInSection = [inputDictionary objectForKey:key];
if( licensesInSection != nil )
{
NSMutableArray* temp = [NSMutableArray array];
for( NSString* subkey in licensesInSection )
{
NSDictionary* license = [licensesInSection objectForKey:subkey];
[temp addObject:[self sampleFileInfoFromDictionary:license]];
}
temp = [NSMutableArray arrayWithArray: [temp sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [((SampleFileInfo*)obj1).name compare:((SampleFileInfo*)obj2).name];
}]];
[self.itemArraysBySectionName setObject:temp forKey:key];
}
}
}
-(void) displayOneSectionNamed:(NSString*) sectionName fromDictionary:(NSDictionary*) licensesInSection
{
self.itemArraysBySectionName = [NSMutableDictionary dictionary];
self.sectionNames = [NSMutableArray arrayWithArray:@[sectionName]];
NSMutableArray* temp = [NSMutableArray array];
for( NSString* subkey in licensesInSection )
{
NSDictionary* license = [licensesInSection objectForKey:subkey];
[temp addObject:[self sampleFileInfoFromDictionary:license]];
}
temp = [NSMutableArray arrayWithArray: [temp sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [((SampleFileInfo*)obj1).name compare:((SampleFileInfo*)obj2).name];
}]];
[self.itemArraysBySectionName setObject:temp forKey:sectionName];
self.title = sectionName;
}
-(void)viewDidLoad
{
[super viewDidLoad];
if( self.sectionNames == nil
|| self.itemArraysBySectionName == nil )
{
NSLog(@"Probable mistake; you should call displayOneSectionNamed:fromDictionary: or displayAllSectionsFromDictionary: before displaying this class");
NSString* path = [[NSBundle mainBundle] pathForResource:@"Licenses" ofType:@"plist"];
NSDictionary* allLicenses = [NSDictionary dictionaryWithContentsOfFile:path];
[self displayAllSectionsFromDictionary:allLicenses];
}
}
-(NSArray*) sectionAtIndex:(NSInteger) index
{
NSString* sectionName = [self.sectionNames objectAtIndex:index];
NSArray* section = [self.itemArraysBySectionName objectForKey:sectionName];
return section;
}
-(SampleFileInfo*) itemAtIndexPath:(NSIndexPath*) indexPath
{
NSArray* section = [self sectionAtIndex:indexPath.section];
SampleFileInfo* item = [section objectAtIndex:indexPath.row];
return item;
}
#pragma mark - UICollectionView
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
SampleFileInfo* item = [self itemAtIndexPath:indexPath];
UILabel* l = (UILabel*) [cell viewWithTag:1];
l.text = item.name;
UIImageView* iv = (UIImageView*) [cell viewWithTag:2];
UIImage* savedImage = [UIImage imageNamed: item.savedBitmapFilename];
if( savedImage != nil )
{
iv.image = savedImage;
}
else
iv.image = nil;
return cell;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [self.sectionNames count];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self sectionAtIndex:section].count;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//SampleFileInfo* item = [self itemAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"ViewSVG" sender:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if( [segue.destinationViewController isKindOfClass:[DetailViewController class]])
{
DetailViewController* nextVC = (DetailViewController*) segue.destinationViewController;
SampleFileInfo* item = [self itemAtIndexPath:[self.collectionView indexPathsForSelectedItems][0]];
nextVC.detailItem = item.source;
}
}
@end