FLEXLibrariesTableViewController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 () <UISearchBarDelegate>
  13. @property (nonatomic, strong) NSArray *imageNames;
  14. @property (nonatomic, strong) NSArray *filteredImageNames;
  15. @property (nonatomic, strong) UISearchBar *searchBar;
  16. @end
  17. @implementation FLEXLibrariesTableViewController
  18. - (id)initWithStyle:(UITableViewStyle)style
  19. {
  20. self = [super initWithStyle:style];
  21. if (self) {
  22. [self loadImageNames];
  23. }
  24. return self;
  25. }
  26. - (void)viewDidLoad
  27. {
  28. [super viewDidLoad];
  29. self.searchBar = [[UISearchBar alloc] init];
  30. self.searchBar.delegate = self;
  31. self.searchBar.placeholder = [FLEXUtility searchBarPlaceholderText];
  32. [self.searchBar sizeToFit];
  33. self.tableView.tableHeaderView = self.searchBar;
  34. }
  35. - (BOOL)prefersStatusBarHidden
  36. {
  37. return YES;
  38. }
  39. #pragma mark - Binary Images
  40. - (void)loadImageNames
  41. {
  42. unsigned int imageNamesCount = 0;
  43. const char **imageNames = objc_copyImageNames(&imageNamesCount);
  44. if (imageNames) {
  45. NSMutableArray *imageNameStrings = [NSMutableArray array];
  46. NSString *appImageName = [FLEXUtility applicationImageName];
  47. for (int i = 0; i < imageNamesCount; i++) {
  48. const char *imageName = imageNames[i];
  49. NSString *imageNameString = [NSString stringWithUTF8String:imageName];
  50. // Skip the app's image. We're just showing system libraries and frameworks.
  51. if (![imageNameString isEqual:appImageName]) {
  52. [imageNameStrings addObject:imageNameString];
  53. }
  54. }
  55. // Sort alphabetically
  56. self.imageNames = [imageNameStrings sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(NSString *name1, NSString *name2) {
  57. NSString *shortName1 = [self shortNameForImageName:name1];
  58. NSString *shortName2 = [self shortNameForImageName:name2];
  59. return [shortName1 caseInsensitiveCompare:shortName2];
  60. }];
  61. free(imageNames);
  62. }
  63. }
  64. - (NSString *)shortNameForImageName:(NSString *)imageName
  65. {
  66. NSString *shortName = nil;
  67. NSArray *components = [imageName componentsSeparatedByString:@"/"];
  68. NSUInteger componentsCount = [components count];
  69. if (componentsCount >= 2) {
  70. shortName = [NSString stringWithFormat:@"%@/%@", components[componentsCount - 2], components[componentsCount - 1]];
  71. }
  72. return shortName;
  73. }
  74. - (void)setImageNames:(NSArray *)imageNames
  75. {
  76. if (![_imageNames isEqual:imageNames]) {
  77. _imageNames = imageNames;
  78. self.filteredImageNames = imageNames;
  79. }
  80. }
  81. #pragma mark - Filtering
  82. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  83. {
  84. if ([searchText length] > 0) {
  85. NSPredicate *searchPreidcate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
  86. BOOL matches = NO;
  87. NSString *shortName = [self shortNameForImageName:evaluatedObject];
  88. if ([shortName rangeOfString:searchText options:NSCaseInsensitiveSearch].length > 0) {
  89. matches = YES;
  90. }
  91. return matches;
  92. }];
  93. self.filteredImageNames = [self.imageNames filteredArrayUsingPredicate:searchPreidcate];
  94. } else {
  95. self.filteredImageNames = self.imageNames;
  96. }
  97. [self.tableView reloadData];
  98. }
  99. #pragma mark - Table View Data Source
  100. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  101. {
  102. return 1;
  103. }
  104. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  105. {
  106. return [self.filteredImageNames count];
  107. }
  108. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  109. {
  110. static NSString *CellIdentifier = @"Cell";
  111. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  112. if (!cell) {
  113. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  114. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  115. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  116. }
  117. NSString *fullImageName = self.filteredImageNames[indexPath.row];
  118. cell.textLabel.text = [self shortNameForImageName:fullImageName];
  119. return cell;
  120. }
  121. #pragma mark - Table View Delegate
  122. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  123. {
  124. FLEXClassesTableViewController *classesViewController = [[FLEXClassesTableViewController alloc] init];
  125. classesViewController.binaryImageName = self.filteredImageNames[indexPath.row];
  126. [self.navigationController pushViewController:classesViewController animated:YES];
  127. }
  128. @end