FLEXHierarchyTableViewController.m 9.9 KB

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