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.

209 lines
7.7 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 "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 ()
  18. @property (nonatomic) NSArray<UIView *> *allViews;
  19. @property (nonatomic) NSDictionary<NSValue *, NSNumber *> *depthsForViews;
  20. @property (nonatomic) NSArray<UIView *> *viewsAtTap;
  21. @property (nonatomic) UIView *selectedView;
  22. @property (nonatomic) NSArray<UIView *> *displayedViews;
  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.showsSearchBar = YES;
  50. self.pinSearchBar = YES;
  51. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  52. self.automaticallyShowsSearchBarCancelButton = NO;
  53. if ([self showScopeBar]) {
  54. self.searchController.searchBar.showsScopeBar = YES;
  55. self.searchController.searchBar.scopeButtonTitles = @[@"Views at Tap", @"Full Hierarchy"];
  56. }
  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<UIView *> *candidateViews = nil;
  76. if ([self showScopeBar]) {
  77. if (self.selectedScope == kFLEXHierarchyScopeViewsAtTapIndex) {
  78. candidateViews = self.viewsAtTap;
  79. } else if (self.selectedScope == kFLEXHierarchyScopeFullHierarchyIndex) {
  80. candidateViews = self.allViews;
  81. }
  82. } else {
  83. candidateViews = self.allViews;
  84. }
  85. if (self.searchText.length) {
  86. self.displayedViews = [candidateViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary<NSString *, id> *bindings) {
  87. NSString *title = [FLEXUtility descriptionForView:candidateView includingFrame:NO];
  88. NSString *candidateViewPointerAddress = [NSString stringWithFormat:@"%p", candidateView];
  89. BOOL matchedViewPointerAddress = [candidateViewPointerAddress rangeOfString:self.searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
  90. BOOL matchedViewTitle = [title rangeOfString:self.searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
  91. return matchedViewPointerAddress || matchedViewTitle;
  92. }]];
  93. } else {
  94. self.displayedViews = candidateViews;
  95. }
  96. [self.tableView reloadData];
  97. }
  98. #pragma mark - Search Bar
  99. - (BOOL)showScopeBar
  100. {
  101. return self.viewsAtTap.count > 0;
  102. }
  103. - (void)updateSearchResults:(NSString *)newText
  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.searchController.searchBar.isFirstResponder ? UITableViewScrollPositionNone : UITableViewScrollPositionMiddle;
  109. [self trySelectCellForSelectedViewWithScrollPosition:scrollPosition];
  110. }
  111. #pragma mark - Table View Data Source
  112. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  113. {
  114. return 1;
  115. }
  116. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  117. {
  118. return self.displayedViews.count;
  119. }
  120. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. static NSString *CellIdentifier = @"Cell";
  123. FLEXHierarchyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  124. if (!cell) {
  125. cell = [[FLEXHierarchyTableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  126. }
  127. UIView *view = self.displayedViews[indexPath.row];
  128. NSNumber *depth = [self.depthsForViews objectForKey:[NSValue valueWithNonretainedObject:view]];
  129. UIColor *viewColor = [FLEXUtility consistentRandomColorForObject:view];
  130. cell.textLabel.text = [FLEXUtility descriptionForView:view includingFrame:NO];
  131. cell.detailTextLabel.text = [FLEXUtility detailDescriptionForView:view];
  132. cell.viewColor = viewColor;
  133. cell.viewDepth = [depth integerValue];
  134. if (view.isHidden || view.alpha < 0.01) {
  135. cell.textLabel.textColor = [FLEXColor deemphasizedTextColor];
  136. cell.detailTextLabel.textColor = [FLEXColor deemphasizedTextColor];
  137. } else {
  138. cell.textLabel.textColor = [FLEXColor primaryTextColor];
  139. cell.detailTextLabel.textColor = [FLEXColor primaryTextColor];
  140. }
  141. // Use a pattern-based colour to simplify application of the checker pattern.
  142. static UIColor *checkerPatternColour = nil;
  143. static dispatch_once_t once;
  144. dispatch_once(&once, ^{
  145. checkerPatternColour = [UIColor colorWithPatternImage:[FLEXResources checkerPattern]];
  146. });
  147. UIColor *viewColour = view.backgroundColor;
  148. if (!viewColour || [viewColour isEqual:UIColor.clearColor]) {
  149. cell.viewBackgroundColorView.backgroundColor = checkerPatternColour;
  150. } else {
  151. cell.viewBackgroundColorView.backgroundColor = viewColour;
  152. }
  153. return cell;
  154. }
  155. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157. self.selectedView = self.displayedViews[indexPath.row];
  158. [self.delegate hierarchyViewController:self didFinishWithSelectedView:self.selectedView];
  159. }
  160. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  161. {
  162. UIView *drillInView = self.displayedViews[indexPath.row];
  163. FLEXObjectExplorerViewController *viewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:drillInView];
  164. [self.navigationController pushViewController:viewExplorer animated:YES];
  165. }
  166. #pragma mark - Button Actions
  167. - (void)donePressed:(id)sender
  168. {
  169. [self.delegate hierarchyViewController:self didFinishWithSelectedView:self.selectedView];
  170. }
  171. @end