FLEXHierarchyTableViewController.m 10.0 KB

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