FLEXLibrariesTableViewController.m 5.0 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. #pragma mark - Binary Images
  36. - (void)loadImageNames
  37. {
  38. unsigned int imageNamesCount = 0;
  39. const char **imageNames = objc_copyImageNames(&imageNamesCount);
  40. if (imageNames) {
  41. NSMutableArray *imageNameStrings = [NSMutableArray array];
  42. NSString *appImageName = [FLEXUtility applicationImageName];
  43. for (unsigned int i = 0; i < imageNamesCount; i++) {
  44. const char *imageName = imageNames[i];
  45. NSString *imageNameString = [NSString stringWithUTF8String:imageName];
  46. // Skip the app's image. We're just showing system libraries and frameworks.
  47. if (![imageNameString isEqual:appImageName]) {
  48. [imageNameStrings addObject:imageNameString];
  49. }
  50. }
  51. // Sort alphabetically
  52. self.imageNames = [imageNameStrings sortedArrayWithOptions:0 usingComparator:^NSComparisonResult(NSString *name1, NSString *name2) {
  53. NSString *shortName1 = [self shortNameForImageName:name1];
  54. NSString *shortName2 = [self shortNameForImageName:name2];
  55. return [shortName1 caseInsensitiveCompare:shortName2];
  56. }];
  57. free(imageNames);
  58. }
  59. }
  60. - (NSString *)shortNameForImageName:(NSString *)imageName
  61. {
  62. NSString *shortName = nil;
  63. NSArray *components = [imageName componentsSeparatedByString:@"/"];
  64. NSUInteger componentsCount = [components count];
  65. if (componentsCount >= 2) {
  66. shortName = [NSString stringWithFormat:@"%@/%@", components[componentsCount - 2], components[componentsCount - 1]];
  67. }
  68. return shortName;
  69. }
  70. - (void)setImageNames:(NSArray *)imageNames
  71. {
  72. if (![_imageNames isEqual:imageNames]) {
  73. _imageNames = imageNames;
  74. self.filteredImageNames = imageNames;
  75. }
  76. }
  77. #pragma mark - Filtering
  78. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  79. {
  80. if ([searchText length] > 0) {
  81. NSPredicate *searchPreidcate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
  82. BOOL matches = NO;
  83. NSString *shortName = [self shortNameForImageName:evaluatedObject];
  84. if ([shortName rangeOfString:searchText options:NSCaseInsensitiveSearch].length > 0) {
  85. matches = YES;
  86. }
  87. return matches;
  88. }];
  89. self.filteredImageNames = [self.imageNames filteredArrayUsingPredicate:searchPreidcate];
  90. } else {
  91. self.filteredImageNames = self.imageNames;
  92. }
  93. [self.tableView reloadData];
  94. }
  95. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  96. {
  97. [searchBar resignFirstResponder];
  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