FLEXHierarchyTableViewController.m 8.2 KB

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