FLEXLibrariesTableViewController.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // FLEXLibrariesTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-02.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXLibrariesTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXClassesTableViewController.h"
  11. #import <objc/runtime.h>
  12. @interface FLEXLibrariesTableViewController ()
  13. @property (nonatomic, strong) NSArray *imageNames;
  14. @end
  15. @implementation FLEXLibrariesTableViewController
  16. - (id)initWithStyle:(UITableViewStyle)style
  17. {
  18. self = [super initWithStyle:style];
  19. if (self) {
  20. [self loadImageNames];
  21. }
  22. return self;
  23. }
  24. - (BOOL)prefersStatusBarHidden
  25. {
  26. return YES;
  27. }
  28. #pragma mark - Binary Images
  29. - (void)loadImageNames
  30. {
  31. unsigned int imageNamesCount = 0;
  32. const char **imageNames = objc_copyImageNames(&imageNamesCount);
  33. if (imageNames) {
  34. NSMutableArray *imageNameStrings = [NSMutableArray array];
  35. NSString *appImageName = [FLEXUtility applicationImageName];
  36. for (int i = 0; i < imageNamesCount; i++) {
  37. const char *imageName = imageNames[i];
  38. NSString *imageNameString = [NSString stringWithUTF8String:imageName];
  39. // Skip the app's image. We're just showing system libraries and frameworks.
  40. if (![imageNameString isEqual:appImageName]) {
  41. [imageNameStrings addObject:imageNameString];
  42. }
  43. }
  44. // Sort alphabetically
  45. self.imageNames = [imageNameStrings sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(NSString *name1, NSString *name2) {
  46. NSString *shortName1 = [self shortNameForImageName:name1];
  47. NSString *shortName2 = [self shortNameForImageName:name2];
  48. return [shortName1 caseInsensitiveCompare:shortName2];
  49. }];
  50. free(imageNames);
  51. }
  52. }
  53. - (NSString *)shortNameForImageName:(NSString *)imageName
  54. {
  55. NSString *shortName = nil;
  56. NSArray *components = [imageName componentsSeparatedByString:@"/"];
  57. NSUInteger componentsCount = [components count];
  58. if (componentsCount >= 2) {
  59. shortName = [NSString stringWithFormat:@"%@/%@", components[componentsCount - 2], components[componentsCount - 1]];
  60. }
  61. return shortName;
  62. }
  63. #pragma mark - Table View Data Source
  64. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  65. {
  66. return 1;
  67. }
  68. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  69. {
  70. return [self.imageNames count];
  71. }
  72. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  73. {
  74. static NSString *CellIdentifier = @"Cell";
  75. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  76. if (!cell) {
  77. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  78. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  79. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  80. }
  81. NSString *fullImageName = self.imageNames[indexPath.row];
  82. cell.textLabel.text = [self shortNameForImageName:fullImageName];
  83. return cell;
  84. }
  85. #pragma mark - Table View Delegate
  86. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. FLEXClassesTableViewController *classesViewController = [[FLEXClassesTableViewController alloc] init];
  89. classesViewController.binaryImageName = self.imageNames[indexPath.row];
  90. [self.navigationController pushViewController:classesViewController animated:YES];
  91. }
  92. @end