FLEXHierarchyTableViewController.m 9.6 KB

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