VCGridOfImagesViewController.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #import "VCGridOfImagesViewController.h"
  2. #import "SampleFileInfo.h"
  3. #import "DetailViewController.h"
  4. @interface VCGridOfImagesViewController ()
  5. @property(nonatomic,strong) NSMutableArray* sectionNames;
  6. @property(nonatomic,strong) NSMutableDictionary* itemArraysBySectionName;
  7. @end
  8. @implementation VCGridOfImagesViewController
  9. -(SampleFileInfo*) sampleFileInfoFromDictionary:(NSDictionary*) license
  10. {
  11. NSString* sourceURL = [license objectForKey:@"Source URL"];
  12. NSString* sourceFilename = [license objectForKey:@"Source Local File"];
  13. SampleFileInfo* info = [SampleFileInfo sampleFileInfoWithFilename:sourceFilename URL:(sourceURL != nil) ? [NSURL URLWithString:sourceURL] : nil];
  14. return info;
  15. }
  16. -(void) displayAllSectionsFromDictionary:(NSDictionary*) inputDictionary
  17. {
  18. self.itemArraysBySectionName = [NSMutableDictionary dictionary];
  19. self.sectionNames = [NSMutableArray array];
  20. for( NSString* key in inputDictionary )
  21. {
  22. [self.sectionNames addObject:key];
  23. NSDictionary* licensesInSection = [inputDictionary objectForKey:key];
  24. if( licensesInSection != nil )
  25. {
  26. NSMutableArray* temp = [NSMutableArray array];
  27. for( NSString* subkey in licensesInSection )
  28. {
  29. NSDictionary* license = [licensesInSection objectForKey:subkey];
  30. [temp addObject:[self sampleFileInfoFromDictionary:license]];
  31. }
  32. temp = [NSMutableArray arrayWithArray: [temp sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
  33. return [((SampleFileInfo*)obj1).name compare:((SampleFileInfo*)obj2).name];
  34. }]];
  35. [self.itemArraysBySectionName setObject:temp forKey:key];
  36. }
  37. }
  38. }
  39. -(void) displayOneSectionNamed:(NSString*) sectionName fromDictionary:(NSDictionary*) licensesInSection
  40. {
  41. self.itemArraysBySectionName = [NSMutableDictionary dictionary];
  42. self.sectionNames = [NSMutableArray arrayWithArray:@[sectionName]];
  43. NSMutableArray* temp = [NSMutableArray array];
  44. for( NSString* subkey in licensesInSection )
  45. {
  46. NSDictionary* license = [licensesInSection objectForKey:subkey];
  47. [temp addObject:[self sampleFileInfoFromDictionary:license]];
  48. }
  49. temp = [NSMutableArray arrayWithArray: [temp sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(id obj1, id obj2) {
  50. return [((SampleFileInfo*)obj1).name compare:((SampleFileInfo*)obj2).name];
  51. }]];
  52. [self.itemArraysBySectionName setObject:temp forKey:sectionName];
  53. self.title = sectionName;
  54. }
  55. -(void)viewDidLoad
  56. {
  57. [super viewDidLoad];
  58. if( self.sectionNames == nil
  59. || self.itemArraysBySectionName == nil )
  60. {
  61. NSLog(@"Probable mistake; you should call displayOneSectionNamed:fromDictionary: or displayAllSectionsFromDictionary: before displaying this class");
  62. NSString* path = [[NSBundle mainBundle] pathForResource:@"Licenses" ofType:@"plist"];
  63. NSDictionary* allLicenses = [NSDictionary dictionaryWithContentsOfFile:path];
  64. [self displayAllSectionsFromDictionary:allLicenses];
  65. }
  66. }
  67. - (void)viewDidDisappear:(BOOL)animated
  68. {
  69. [super viewDidDisappear:animated];
  70. [self.collectionView selectItemAtIndexPath:nil animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  71. }
  72. -(NSArray*) sectionAtIndex:(NSInteger) index
  73. {
  74. NSString* sectionName = [self.sectionNames objectAtIndex:index];
  75. NSArray* section = [self.itemArraysBySectionName objectForKey:sectionName];
  76. return section;
  77. }
  78. -(SampleFileInfo*) itemAtIndexPath:(NSIndexPath*) indexPath
  79. {
  80. NSArray* section = [self sectionAtIndex:indexPath.section];
  81. SampleFileInfo* item = [section objectAtIndex:indexPath.row];
  82. return item;
  83. }
  84. #pragma mark - UICollectionView
  85. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  88. SampleFileInfo* item = [self itemAtIndexPath:indexPath];
  89. UILabel* l = (UILabel*) [cell viewWithTag:1];
  90. l.text = item.name;
  91. UIImageView* iv = (UIImageView*) [cell viewWithTag:2];
  92. UIImage* savedImage = [UIImage imageNamed: item.savedBitmapFilename];
  93. if( savedImage != nil )
  94. {
  95. iv.image = savedImage;
  96. }
  97. else
  98. iv.image = nil;
  99. return cell;
  100. }
  101. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  102. {
  103. return [self.sectionNames count];
  104. }
  105. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  106. {
  107. return [self sectionAtIndex:section].count;
  108. }
  109. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  110. {
  111. //SampleFileInfo* item = [self itemAtIndexPath:indexPath];
  112. [self performSegueWithIdentifier:@"ViewSVG" sender:nil];
  113. }
  114. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  115. {
  116. if( [segue.destinationViewController isKindOfClass:[DetailViewController class]])
  117. {
  118. DetailViewController* nextVC = (DetailViewController*) segue.destinationViewController;
  119. SampleFileInfo* item = [self itemAtIndexPath:[self.collectionView indexPathsForSelectedItems][0]];
  120. nextVC.detailItem = item.source;
  121. }
  122. }
  123. @end