FLEXHierarchyTableViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // FLEXHierarchyTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-01.
  6. // Copyright (c) 2020 FLEX Team. 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)viewWillAppear:(BOOL)animated {
  77. [super viewWillAppear:animated];
  78. [self disableToolbar];
  79. }
  80. - (void)viewDidAppear:(BOOL)animated {
  81. [super viewDidAppear:animated];
  82. [self trySelectCellForSelectedView];
  83. }
  84. #pragma mark - Hierarchy helpers
  85. + (NSArray<UIView *> *)allViewsInHierarchy:(NSArray<UIWindow *> *)windows {
  86. return [windows flex_flatmapped:^id(UIWindow *window, NSUInteger idx) {
  87. if (![window isKindOfClass:[FLEXWindow class]]) {
  88. return [self viewWithRecursiveSubviews:window];
  89. }
  90. return nil;
  91. }];
  92. }
  93. + (NSArray<UIView *> *)viewWithRecursiveSubviews:(UIView *)view {
  94. NSMutableArray<UIView *> *subviews = [NSMutableArray arrayWithObject:view];
  95. for (UIView *subview in view.subviews) {
  96. [subviews addObjectsFromArray:[self viewWithRecursiveSubviews:subview]];
  97. }
  98. return subviews;
  99. }
  100. + (NSMapTable<UIView *, NSNumber *> *)hierarchyDepthsForViews:(NSArray<UIView *> *)views {
  101. NSMapTable<UIView *, NSNumber *> *depths = [NSMapTable strongToStrongObjectsMapTable];
  102. for (UIView *view in views) {
  103. NSInteger depth = 0;
  104. UIView *tryView = view;
  105. while (tryView.superview) {
  106. tryView = tryView.superview;
  107. depth++;
  108. }
  109. depths[(id)view] = @(depth);
  110. }
  111. return depths;
  112. }
  113. #pragma mark Selection and Filtering Helpers
  114. - (void)trySelectCellForSelectedView {
  115. NSUInteger selectedViewIndex = [self.displayedViews indexOfObject:self.selectedView];
  116. if (selectedViewIndex != NSNotFound) {
  117. UITableViewScrollPosition scrollPosition = UITableViewScrollPositionMiddle;
  118. NSIndexPath *selectedViewIndexPath = [NSIndexPath indexPathForRow:selectedViewIndex inSection:0];
  119. [self.tableView selectRowAtIndexPath:selectedViewIndexPath animated:YES scrollPosition:scrollPosition];
  120. }
  121. }
  122. - (void)updateDisplayedViews {
  123. NSArray<UIView *> *candidateViews = nil;
  124. if (self.showScopeBar) {
  125. if (self.selectedScope == FLEXHierarchyScopeViewsAtTap) {
  126. candidateViews = self.viewsAtTap;
  127. } else if (self.selectedScope == FLEXHierarchyScopeFullHierarchy) {
  128. candidateViews = self.allViews;
  129. }
  130. } else {
  131. candidateViews = self.allViews;
  132. }
  133. if (self.searchText.length) {
  134. self.displayedViews = [candidateViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary<NSString *, id> *bindings) {
  135. NSString *title = [FLEXUtility descriptionForView:candidateView includingFrame:NO];
  136. NSString *candidateViewPointerAddress = [NSString stringWithFormat:@"%p", candidateView];
  137. BOOL matchedViewPointerAddress = [candidateViewPointerAddress rangeOfString:self.searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
  138. BOOL matchedViewTitle = [title rangeOfString:self.searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
  139. return matchedViewPointerAddress || matchedViewTitle;
  140. }]];
  141. } else {
  142. self.displayedViews = candidateViews;
  143. }
  144. [self.tableView reloadData];
  145. }
  146. - (void)setSelectedView:(UIView *)selectedView {
  147. _selectedView = selectedView;
  148. if (self.isViewLoaded) {
  149. [self trySelectCellForSelectedView];
  150. }
  151. }
  152. #pragma mark - Search Bar / Scope Bar
  153. - (BOOL)showScopeBar {
  154. return self.viewsAtTap.count > 0;
  155. }
  156. - (void)updateSearchResults:(NSString *)newText {
  157. [self updateDisplayedViews];
  158. // If the search bar text field is active, don't scroll on selection because we may want
  159. // to continue typing. Otherwise, scroll so that the selected cell is visible.
  160. if (!self.searchController.searchBar.isFirstResponder) {
  161. [self trySelectCellForSelectedView];
  162. }
  163. }
  164. #pragma mark - Table View Data Source
  165. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  166. return self.displayedViews.count;
  167. }
  168. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  169. static NSString *CellIdentifier = @"Cell";
  170. FLEXHierarchyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  171. if (!cell) {
  172. cell = [[FLEXHierarchyTableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  173. }
  174. UIView *view = self.displayedViews[indexPath.row];
  175. cell.textLabel.text = [FLEXUtility descriptionForView:view includingFrame:NO];
  176. cell.detailTextLabel.text = [FLEXUtility detailDescriptionForView:view];
  177. cell.randomColorTag = [FLEXUtility consistentRandomColorForObject:view];
  178. cell.viewDepth = self.depthsForViews[view].integerValue;
  179. cell.indicatedViewColor = view.backgroundColor;
  180. if (view.isHidden || view.alpha < 0.01) {
  181. cell.textLabel.textColor = FLEXColor.deemphasizedTextColor;
  182. cell.detailTextLabel.textColor = FLEXColor.deemphasizedTextColor;
  183. } else {
  184. cell.textLabel.textColor = FLEXColor.primaryTextColor;
  185. cell.detailTextLabel.textColor = FLEXColor.primaryTextColor;
  186. }
  187. return cell;
  188. }
  189. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  190. _selectedView = self.displayedViews[indexPath.row]; // Don't scroll, avoid setter
  191. if (self.didSelectRowAction) {
  192. self.didSelectRowAction(_selectedView);
  193. }
  194. }
  195. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
  196. UIView *drillInView = self.displayedViews[indexPath.row];
  197. FLEXObjectExplorerViewController *viewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:drillInView];
  198. [self.navigationController pushViewController:viewExplorer animated:YES];
  199. }
  200. @end