FLEXHierarchyTableViewController.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #import "FLEXWindow.h"
  16. typedef NS_ENUM(NSUInteger, FLEXHierarchyScope) {
  17. FLEXHierarchyScopeFullHierarchy,
  18. FLEXHierarchyScopeViewsAtTap
  19. };
  20. @interface FLEXHierarchyTableViewController ()
  21. @property (nonatomic) NSArray<UIView *> *allViews;
  22. @property (nonatomic) NSMapTable<UIView *, NSNumber *> *depthsForViews;
  23. @property (nonatomic) NSArray<UIView *> *viewsAtTap;
  24. @property (nonatomic) NSArray<UIView *> *displayedViews;
  25. @property (nonatomic, readonly) BOOL showScopeBar;
  26. @end
  27. @implementation FLEXHierarchyTableViewController
  28. + (instancetype)windows:(NSArray<UIWindow *> *)allWindows
  29. viewsAtTap:(NSArray<UIView *> *)viewsAtTap
  30. selectedView:(UIView *)selected {
  31. NSParameterAssert(allWindows.count);
  32. NSArray *allViews = [self allViewsInHierarchy:allWindows];
  33. NSMapTable *depths = [self hierarchyDepthsForViews:allViews];
  34. return [[self alloc] initWithViews:allViews viewsAtTap:viewsAtTap selectedView:selected depths:depths];
  35. }
  36. - (instancetype)initWithViews:(NSArray<UIView *> *)allViews
  37. viewsAtTap:(NSArray<UIView *> *)viewsAtTap
  38. selectedView:(UIView *)selectedView
  39. depths:(NSMapTable<UIView *, NSNumber *> *)depthsForViews {
  40. NSParameterAssert(allViews);
  41. NSParameterAssert(depthsForViews.count == allViews.count);
  42. self = [super initWithStyle:UITableViewStylePlain];
  43. if (self) {
  44. self.allViews = allViews;
  45. self.depthsForViews = depthsForViews;
  46. self.viewsAtTap = viewsAtTap;
  47. self.selectedView = selectedView;
  48. self.title = @"View Hierarchy Tree";
  49. }
  50. return self;
  51. }
  52. - (void)viewDidLoad {
  53. [super viewDidLoad];
  54. // Preserve selection between presentations
  55. self.clearsSelectionOnViewWillAppear = NO;
  56. // A little more breathing room
  57. self.tableView.rowHeight = 50.0;
  58. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  59. // Separator inset clashes with persistent cell selection
  60. [self.tableView setSeparatorInset:UIEdgeInsetsZero];
  61. self.showsSearchBar = YES;
  62. self.showSearchBarInitially = YES;
  63. // Using pinSearchBar on this screen causes a weird visual
  64. // thing on the next view controller that gets pushed.
  65. //
  66. // self.pinSearchBar = YES;
  67. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  68. self.automaticallyShowsSearchBarCancelButton = NO;
  69. if (self.showScopeBar) {
  70. self.searchController.searchBar.showsScopeBar = YES;
  71. self.searchController.searchBar.scopeButtonTitles = @[@"Full Hierarchy", @"Views at Tap"];
  72. self.selectedScope = FLEXHierarchyScopeViewsAtTap;
  73. }
  74. [self updateDisplayedViews];
  75. }
  76. - (void)viewDidAppear:(BOOL)animated {
  77. [super viewDidAppear:animated];
  78. [self trySelectCellForSelectedView];
  79. }
  80. #pragma mark - Hierarchy helpers
  81. + (NSArray<UIView *> *)allViewsInHierarchy:(NSArray<UIWindow *> *)windows {
  82. return [windows flex_flatmapped:^id(UIWindow *window, NSUInteger idx) {
  83. if (![window isKindOfClass:[FLEXWindow class]]) {
  84. return [self viewWithRecursiveSubviews:window];
  85. }
  86. return nil;
  87. }];
  88. }
  89. + (NSArray<UIView *> *)viewWithRecursiveSubviews:(UIView *)view {
  90. NSMutableArray<UIView *> *subviews = [NSMutableArray arrayWithObject:view];
  91. for (UIView *subview in view.subviews) {
  92. [subviews addObjectsFromArray:[self viewWithRecursiveSubviews:subview]];
  93. }
  94. return subviews;
  95. }
  96. + (NSMapTable<UIView *, NSNumber *> *)hierarchyDepthsForViews:(NSArray<UIView *> *)views {
  97. NSMapTable<UIView *, NSNumber *> *depths = [NSMapTable strongToStrongObjectsMapTable];
  98. for (UIView *view in views) {
  99. NSInteger depth = 0;
  100. UIView *tryView = view;
  101. while (tryView.superview) {
  102. tryView = tryView.superview;
  103. depth++;
  104. }
  105. depths[(id)view] = @(depth);
  106. }
  107. return depths;
  108. }
  109. #pragma mark Selection and Filtering Helpers
  110. - (void)trySelectCellForSelectedView {
  111. NSUInteger selectedViewIndex = [self.displayedViews indexOfObject:self.selectedView];
  112. if (selectedViewIndex != NSNotFound) {
  113. UITableViewScrollPosition scrollPosition = UITableViewScrollPositionMiddle;
  114. NSIndexPath *selectedViewIndexPath = [NSIndexPath indexPathForRow:selectedViewIndex inSection:0];
  115. [self.tableView selectRowAtIndexPath:selectedViewIndexPath animated:YES scrollPosition:scrollPosition];
  116. }
  117. }
  118. - (void)updateDisplayedViews {
  119. NSArray<UIView *> *candidateViews = nil;
  120. if (self.showScopeBar) {
  121. if (self.selectedScope == FLEXHierarchyScopeViewsAtTap) {
  122. candidateViews = self.viewsAtTap;
  123. } else if (self.selectedScope == FLEXHierarchyScopeFullHierarchy) {
  124. candidateViews = self.allViews;
  125. }
  126. } else {
  127. candidateViews = self.allViews;
  128. }
  129. if (self.searchText.length) {
  130. self.displayedViews = [candidateViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary<NSString *, id> *bindings) {
  131. NSString *title = [FLEXUtility descriptionForView:candidateView includingFrame:NO];
  132. NSString *candidateViewPointerAddress = [NSString stringWithFormat:@"%p", candidateView];
  133. BOOL matchedViewPointerAddress = [candidateViewPointerAddress rangeOfString:self.searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
  134. BOOL matchedViewTitle = [title rangeOfString:self.searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
  135. return matchedViewPointerAddress || matchedViewTitle;
  136. }]];
  137. } else {
  138. self.displayedViews = candidateViews;
  139. }
  140. [self.tableView reloadData];
  141. }
  142. - (void)setSelectedView:(UIView *)selectedView {
  143. _selectedView = selectedView;
  144. if (self.isViewLoaded) {
  145. [self trySelectCellForSelectedView];
  146. }
  147. }
  148. #pragma mark - Search Bar / Scope Bar
  149. - (BOOL)showScopeBar {
  150. return self.viewsAtTap.count > 0;
  151. }
  152. - (void)updateSearchResults:(NSString *)newText {
  153. [self updateDisplayedViews];
  154. // If the search bar text field is active, don't scroll on selection because we may want
  155. // to continue typing. Otherwise, scroll so that the selected cell is visible.
  156. if (self.searchController.searchBar.isFirstResponder) {
  157. [self trySelectCellForSelectedView];
  158. }
  159. }
  160. #pragma mark - Table View Data Source
  161. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  162. return self.displayedViews.count;
  163. }
  164. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  165. static NSString *CellIdentifier = @"Cell";
  166. FLEXHierarchyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  167. if (!cell) {
  168. cell = [[FLEXHierarchyTableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  169. }
  170. UIView *view = self.displayedViews[indexPath.row];
  171. cell.textLabel.text = [FLEXUtility descriptionForView:view includingFrame:NO];
  172. cell.detailTextLabel.text = [FLEXUtility detailDescriptionForView:view];
  173. cell.randomColorTag = [FLEXUtility consistentRandomColorForObject:view];
  174. cell.viewDepth = self.depthsForViews[view].integerValue;
  175. cell.indicatedViewColor = view.backgroundColor;
  176. if (view.isHidden || view.alpha < 0.01) {
  177. cell.textLabel.textColor = FLEXColor.deemphasizedTextColor;
  178. cell.detailTextLabel.textColor = FLEXColor.deemphasizedTextColor;
  179. } else {
  180. cell.textLabel.textColor = FLEXColor.primaryTextColor;
  181. cell.detailTextLabel.textColor = FLEXColor.primaryTextColor;
  182. }
  183. return cell;
  184. }
  185. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  186. _selectedView = self.displayedViews[indexPath.row]; // Don't scroll, avoid setter
  187. if (self.didSelectRowAction) {
  188. self.didSelectRowAction(_selectedView);
  189. }
  190. }
  191. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
  192. UIView *drillInView = self.displayedViews[indexPath.row];
  193. FLEXObjectExplorerViewController *viewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:drillInView];
  194. [self.navigationController pushViewController:viewExplorer animated:YES];
  195. }
  196. @end