FLEXHierarchyTableViewController.m 8.7 KB

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