FLEXHierarchyTableViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // FLEXHierarchyTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-01.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXHierarchyTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXHierarchyTableViewCell.h"
  11. #import "FLEXObjectExplorerViewController.h"
  12. #import "FLEXObjectExplorerFactory.h"
  13. static const NSInteger kFLEXHierarchyScopeViewsAtTapIndex = 0;
  14. static const NSInteger kFLEXHierarchyScopeFullHierarchyIndex = 1;
  15. @interface FLEXHierarchyTableViewController () <UISearchBarDelegate>
  16. @property (nonatomic, strong) NSArray *allViews;
  17. @property (nonatomic, strong) NSDictionary *depthsForViews;
  18. @property (nonatomic, strong) NSArray *viewsAtTap;
  19. @property (nonatomic, strong) UIView *selectedView;
  20. @property (nonatomic, strong) NSArray *displayedViews;
  21. @property (nonatomic, strong) UISearchBar *searchBar;
  22. @end
  23. @implementation FLEXHierarchyTableViewController
  24. - (id)initWithViews:(NSArray *)allViews viewsAtTap:(NSArray *)viewsAtTap selectedView:(UIView *)selectedView depths:(NSDictionary *)depthsForViews
  25. {
  26. self = [super initWithStyle:UITableViewStylePlain];
  27. if (self) {
  28. self.allViews = allViews;
  29. self.depthsForViews = depthsForViews;
  30. self.viewsAtTap = viewsAtTap;
  31. self.selectedView = selectedView;
  32. self.title = @"View Hierarchy";
  33. }
  34. return self;
  35. }
  36. - (void)viewDidLoad
  37. {
  38. [super viewDidLoad];
  39. // Preserve selection between presentations.
  40. self.clearsSelectionOnViewWillAppear = NO;
  41. // Done button.
  42. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
  43. // A little more breathing room.
  44. self.tableView.rowHeight = 50.0;
  45. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  46. // Separator inset clashes with persistent cell selection.
  47. [self.tableView setSeparatorInset:UIEdgeInsetsZero];
  48. self.searchBar = [[UISearchBar alloc] init];
  49. self.searchBar.placeholder = [FLEXUtility searchBarPlaceholderText];
  50. self.searchBar.delegate = self;
  51. if ([self showScopeBar]) {
  52. self.searchBar.showsScopeBar = YES;
  53. self.searchBar.scopeButtonTitles = @[@"Views at Tap", @"Full Hierarchy"];
  54. }
  55. [self.searchBar sizeToFit];
  56. self.tableView.tableHeaderView = self.searchBar;
  57. [self updateDisplayedViews];
  58. }
  59. - (void)viewDidAppear:(BOOL)animated
  60. {
  61. [super viewDidAppear:animated];
  62. [self trySelectCellForSelectedViewWithScrollPosition:UITableViewScrollPositionMiddle];
  63. }
  64. #pragma mark Selection and Filtering Helpers
  65. - (void)trySelectCellForSelectedViewWithScrollPosition:(UITableViewScrollPosition)scrollPosition
  66. {
  67. NSUInteger selectedViewIndex = [self.displayedViews indexOfObject:self.selectedView];
  68. if (selectedViewIndex != NSNotFound) {
  69. NSIndexPath *selectedViewIndexPath = [NSIndexPath indexPathForRow:selectedViewIndex inSection:0];
  70. [self.tableView selectRowAtIndexPath:selectedViewIndexPath animated:YES scrollPosition:scrollPosition];
  71. }
  72. }
  73. - (void)updateDisplayedViews
  74. {
  75. NSArray *candidateViews = nil;
  76. if ([self showScopeBar]) {
  77. if (self.searchBar.selectedScopeButtonIndex == kFLEXHierarchyScopeViewsAtTapIndex) {
  78. candidateViews = self.viewsAtTap;
  79. } else if (self.searchBar.selectedScopeButtonIndex == kFLEXHierarchyScopeFullHierarchyIndex) {
  80. candidateViews = self.allViews;
  81. }
  82. } else {
  83. candidateViews = self.allViews;
  84. }
  85. if ([self.searchBar.text length] > 0) {
  86. self.displayedViews = [candidateViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary *bindings) {
  87. NSString *title = [FLEXUtility descriptionForView:candidateView includingFrame:NO];
  88. NSString *candidateViewPointerAddress = [NSString stringWithFormat:@"%p", candidateView];
  89. BOOL matchedViewPointerAddress = [candidateViewPointerAddress rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch].location != NSNotFound;
  90. BOOL matchedViewTitle = [title rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch].location != NSNotFound;
  91. return matchedViewPointerAddress || matchedViewTitle;
  92. }]];
  93. } else {
  94. self.displayedViews = candidateViews;
  95. }
  96. [self.tableView reloadData];
  97. }
  98. - (BOOL)showScopeBar
  99. {
  100. return [self.viewsAtTap count] > 0;
  101. }
  102. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
  103. {
  104. [self updateDisplayedViews];
  105. // If the search bar text field is active, don't scroll on selection because we may want to continue typing.
  106. // Otherwise, scroll so that the selected cell is visible.
  107. UITableViewScrollPosition scrollPosition = self.searchBar.isFirstResponder ? UITableViewScrollPositionNone : UITableViewScrollPositionMiddle;
  108. [self trySelectCellForSelectedViewWithScrollPosition:scrollPosition];
  109. }
  110. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  111. {
  112. [self updateDisplayedViews];
  113. [self trySelectCellForSelectedViewWithScrollPosition:UITableViewScrollPositionNone];
  114. }
  115. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  116. {
  117. [searchBar resignFirstResponder];
  118. }
  119. #pragma mark - Table View Data Source
  120. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  121. {
  122. return 1;
  123. }
  124. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  125. {
  126. return [self.displayedViews count];
  127. }
  128. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  129. {
  130. static NSString *CellIdentifier = @"Cell";
  131. FLEXHierarchyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  132. if (!cell) {
  133. cell = [[FLEXHierarchyTableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  134. }
  135. UIView *view = self.displayedViews[indexPath.row];
  136. NSNumber *depth = [self.depthsForViews objectForKey:[NSValue valueWithNonretainedObject:view]];
  137. UIColor *viewColor = [FLEXUtility consistentRandomColorForObject:view];
  138. cell.textLabel.text = [FLEXUtility descriptionForView:view includingFrame:NO];
  139. cell.detailTextLabel.text = [FLEXUtility detailDescriptionForView:view];
  140. cell.viewColor = viewColor;
  141. cell.viewDepth = [depth integerValue];
  142. if (view.isHidden || view.alpha < 0.01) {
  143. cell.textLabel.textColor = [UIColor lightGrayColor];
  144. cell.detailTextLabel.textColor = [UIColor lightGrayColor];
  145. } else {
  146. cell.textLabel.textColor = [UIColor blackColor];
  147. cell.detailTextLabel.textColor = [UIColor blackColor];
  148. }
  149. return cell;
  150. }
  151. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  152. {
  153. self.selectedView = self.displayedViews[indexPath.row];
  154. [self.delegate hierarchyViewController:self didFinishWithSelectedView:self.selectedView];
  155. }
  156. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  157. {
  158. UIView *drillInView = self.displayedViews[indexPath.row];
  159. FLEXObjectExplorerViewController *viewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:drillInView];
  160. [self.navigationController pushViewController:viewExplorer animated:YES];
  161. }
  162. #pragma mark - Button Actions
  163. - (void)donePressed:(id)sender
  164. {
  165. [self.delegate hierarchyViewController:self didFinishWithSelectedView:self.selectedView];
  166. }
  167. @end