You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
8.2 KiB

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