FLEXHierarchyTableViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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)longPress:(UILongPressGestureRecognizer*)gesture {
  53. if ( gesture.state == UIGestureRecognizerStateBegan) {
  54. }
  55. else if ( gesture.state == UIGestureRecognizerStateEnded) {
  56. NSLog(@"do something different for long press!");
  57. UITableView *tv = [self tableView];
  58. //naughty naughty
  59. NSIndexPath *focus = [tv valueForKey:@"_focusedCellIndexPath"];
  60. NSLog(@"[FLEX] focusedIndexPath: %@", focus);
  61. [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:focus];
  62. }
  63. }
  64. - (void)addlongPressGestureRecognizer {
  65. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  66. longPress.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeSelect]];
  67. [self.tableView addGestureRecognizer:longPress];
  68. }
  69. - (void)viewDidLoad {
  70. [super viewDidLoad];
  71. // Preserve selection between presentations
  72. self.clearsSelectionOnViewWillAppear = NO;
  73. // A little more breathing room
  74. self.tableView.rowHeight = 50.0;
  75. #if !TARGET_OS_TV
  76. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  77. #else
  78. [self addlongPressGestureRecognizer];
  79. self.tableView.rowHeight = 70.0;
  80. #endif
  81. // Separator inset clashes with persistent cell selection
  82. [self.tableView setSeparatorInset:UIEdgeInsetsZero];
  83. self.showsSearchBar = YES;
  84. self.showSearchBarInitially = YES;
  85. // Using pinSearchBar on this screen causes a weird visual
  86. // thing on the next view controller that gets pushed.
  87. //
  88. // self.pinSearchBar = YES;
  89. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  90. self.automaticallyShowsSearchBarCancelButton = NO;
  91. if (self.showScopeBar) {
  92. self.searchController.searchBar.showsScopeBar = YES;
  93. self.searchController.searchBar.scopeButtonTitles = @[@"Full Hierarchy", @"Views at Tap"];
  94. self.selectedScope = FLEXHierarchyScopeViewsAtTap;
  95. }
  96. [self updateDisplayedViews];
  97. }
  98. - (void)viewWillAppear:(BOOL)animated {
  99. [super viewWillAppear:animated];
  100. [self disableToolbar];
  101. }
  102. - (void)viewDidAppear:(BOOL)animated {
  103. [super viewDidAppear:animated];
  104. [self trySelectCellForSelectedView];
  105. }
  106. #pragma mark - Hierarchy helpers
  107. + (NSArray<UIView *> *)allViewsInHierarchy:(NSArray<UIWindow *> *)windows {
  108. return [windows flex_flatmapped:^id(UIWindow *window, NSUInteger idx) {
  109. if (![window isKindOfClass:[FLEXWindow class]]) {
  110. return [self viewWithRecursiveSubviews:window];
  111. }
  112. return nil;
  113. }];
  114. }
  115. + (NSArray<UIView *> *)viewWithRecursiveSubviews:(UIView *)view {
  116. NSMutableArray<UIView *> *subviews = [NSMutableArray arrayWithObject:view];
  117. for (UIView *subview in view.subviews) {
  118. [subviews addObjectsFromArray:[self viewWithRecursiveSubviews:subview]];
  119. }
  120. return subviews;
  121. }
  122. + (NSMapTable<UIView *, NSNumber *> *)hierarchyDepthsForViews:(NSArray<UIView *> *)views {
  123. NSMapTable<UIView *, NSNumber *> *depths = [NSMapTable strongToStrongObjectsMapTable];
  124. for (UIView *view in views) {
  125. NSInteger depth = 0;
  126. UIView *tryView = view;
  127. while (tryView.superview) {
  128. tryView = tryView.superview;
  129. depth++;
  130. }
  131. depths[(id)view] = @(depth);
  132. }
  133. return depths;
  134. }
  135. #pragma mark Selection and Filtering Helpers
  136. - (void)trySelectCellForSelectedView {
  137. NSUInteger selectedViewIndex = [self.displayedViews indexOfObject:self.selectedView];
  138. if (selectedViewIndex != NSNotFound) {
  139. UITableViewScrollPosition scrollPosition = UITableViewScrollPositionMiddle;
  140. NSIndexPath *selectedViewIndexPath = [NSIndexPath indexPathForRow:selectedViewIndex inSection:0];
  141. [self.tableView selectRowAtIndexPath:selectedViewIndexPath animated:YES scrollPosition:scrollPosition];
  142. }
  143. }
  144. - (void)updateDisplayedViews {
  145. NSArray<UIView *> *candidateViews = nil;
  146. if (self.showScopeBar) {
  147. if (self.selectedScope == FLEXHierarchyScopeViewsAtTap) {
  148. candidateViews = self.viewsAtTap;
  149. } else if (self.selectedScope == FLEXHierarchyScopeFullHierarchy) {
  150. candidateViews = self.allViews;
  151. }
  152. } else {
  153. candidateViews = self.allViews;
  154. }
  155. if (self.searchText.length) {
  156. self.displayedViews = [candidateViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary<NSString *, id> *bindings) {
  157. NSString *title = [FLEXUtility descriptionForView:candidateView includingFrame:NO];
  158. NSString *candidateViewPointerAddress = [NSString stringWithFormat:@"%p", candidateView];
  159. BOOL matchedViewPointerAddress = [candidateViewPointerAddress rangeOfString:self.searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
  160. BOOL matchedViewTitle = [title rangeOfString:self.searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
  161. return matchedViewPointerAddress || matchedViewTitle;
  162. }]];
  163. } else {
  164. self.displayedViews = candidateViews;
  165. }
  166. [self.tableView reloadData];
  167. }
  168. - (void)setSelectedView:(UIView *)selectedView {
  169. _selectedView = selectedView;
  170. if (self.isViewLoaded) {
  171. [self trySelectCellForSelectedView];
  172. }
  173. }
  174. #pragma mark - Search Bar / Scope Bar
  175. - (BOOL)showScopeBar {
  176. return self.viewsAtTap.count > 0;
  177. }
  178. - (void)updateSearchResults:(NSString *)newText {
  179. [self updateDisplayedViews];
  180. // If the search bar text field is active, don't scroll on selection because we may want
  181. // to continue typing. Otherwise, scroll so that the selected cell is visible.
  182. if (!self.searchController.searchBar.isFirstResponder) {
  183. [self trySelectCellForSelectedView];
  184. }
  185. }
  186. #pragma mark - Table View Data Source
  187. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  188. return self.displayedViews.count;
  189. }
  190. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  191. static NSString *CellIdentifier = @"Cell";
  192. FLEXHierarchyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  193. if (!cell) {
  194. cell = [[FLEXHierarchyTableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  195. }
  196. UIView *view = self.displayedViews[indexPath.row];
  197. cell.textLabel.text = [FLEXUtility descriptionForView:view includingFrame:NO];
  198. cell.detailTextLabel.text = [FLEXUtility detailDescriptionForView:view];
  199. cell.randomColorTag = [FLEXUtility consistentRandomColorForObject:view];
  200. cell.viewDepth = self.depthsForViews[view].integerValue;
  201. cell.indicatedViewColor = view.backgroundColor;
  202. if (view.isHidden || view.alpha < 0.01) {
  203. cell.textLabel.textColor = FLEXColor.deemphasizedTextColor;
  204. cell.detailTextLabel.textColor = FLEXColor.deemphasizedTextColor;
  205. } else {
  206. cell.textLabel.textColor = FLEXColor.primaryTextColor;
  207. cell.detailTextLabel.textColor = FLEXColor.primaryTextColor;
  208. }
  209. return cell;
  210. }
  211. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  212. _selectedView = self.displayedViews[indexPath.row]; // Don't scroll, avoid setter
  213. if (self.didSelectRowAction) {
  214. self.didSelectRowAction(_selectedView);
  215. }
  216. }
  217. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
  218. UIView *drillInView = self.displayedViews[indexPath.row];
  219. FLEXObjectExplorerViewController *viewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:drillInView];
  220. [self.navigationController pushViewController:viewExplorer animated:YES];
  221. }
  222. @end