FLEXHierarchyTableViewController.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // FLEXHierarchyTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-01.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXHierarchyTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXHierarchyTableViewCell.h"
  11. #import "FLEXObjectExplorerViewController.h"
  12. #import "FLEXObjectExplorerFactory.h"
  13. static const NSInteger kFLEXHierarchyScopeViewsAtTapIndex = 0;
  14. static const NSInteger kFLEXHierarchyScopeFullHierarchyIndex = 1;
  15. @interface FLEXHierarchyTableViewController () <UISearchBarDelegate>
  16. @property (nonatomic, strong) NSArray *allViews;
  17. @property (nonatomic, strong) NSDictionary *depthsForViews;
  18. @property (nonatomic, strong) NSArray *viewsAtTap;
  19. @property (nonatomic, strong) UIView *selectedView;
  20. @property (nonatomic, strong) NSArray *displayedViews;
  21. @property (nonatomic, strong) UISearchBar *searchBar;
  22. @end
  23. @implementation FLEXHierarchyTableViewController
  24. - (id)initWithViews:(NSArray *)allViews viewsAtTap:(NSArray *)viewsAtTap selectedView:(UIView *)selectedView depths:(NSDictionary *)depthsForViews
  25. {
  26. self = [super initWithStyle:UITableViewStylePlain];
  27. if (self) {
  28. self.allViews = allViews;
  29. self.depthsForViews = depthsForViews;
  30. self.viewsAtTap = viewsAtTap;
  31. self.selectedView = selectedView;
  32. self.title = @"View Hierarchy";
  33. }
  34. return self;
  35. }
  36. - (void)viewDidLoad
  37. {
  38. [super viewDidLoad];
  39. // Preserve selection between presentations.
  40. self.clearsSelectionOnViewWillAppear = NO;
  41. // Done button.
  42. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
  43. // A little more breathing room.
  44. self.tableView.rowHeight = 50.0;
  45. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  46. // Separator inset clashes with persistent cell selection.
  47. if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
  48. [self.tableView setSeparatorInset:UIEdgeInsetsZero];
  49. }
  50. self.searchBar = [[UISearchBar alloc] init];
  51. self.searchBar.placeholder = [FLEXUtility searchBarPlaceholderText];
  52. self.searchBar.delegate = self;
  53. if ([self showScopeBar]) {
  54. self.searchBar.showsScopeBar = YES;
  55. self.searchBar.scopeButtonTitles = @[@"Views at Tap", @"Full Hierarchy"];
  56. }
  57. [self.searchBar sizeToFit];
  58. self.tableView.tableHeaderView = self.searchBar;
  59. [self updateDisplayedViews];
  60. }
  61. - (void)viewDidAppear:(BOOL)animated
  62. {
  63. [super viewDidAppear:animated];
  64. [self trySelectCellForSelectedViewWithScrollPosition:UITableViewScrollPositionMiddle];
  65. }
  66. - (BOOL)prefersStatusBarHidden
  67. {
  68. return YES;
  69. }
  70. #pragma mark Selection and Filtering Helpers
  71. - (void)trySelectCellForSelectedViewWithScrollPosition:(UITableViewScrollPosition)scrollPosition
  72. {
  73. NSUInteger selectedViewIndex = [self.displayedViews indexOfObject:self.selectedView];
  74. if (selectedViewIndex != NSNotFound) {
  75. NSIndexPath *selectedViewIndexPath = [NSIndexPath indexPathForRow:selectedViewIndex inSection:0];
  76. [self.tableView selectRowAtIndexPath:selectedViewIndexPath animated:YES scrollPosition:scrollPosition];
  77. }
  78. }
  79. - (void)updateDisplayedViews
  80. {
  81. NSArray *candidateViews = nil;
  82. if ([self showScopeBar]) {
  83. if (self.searchBar.selectedScopeButtonIndex == kFLEXHierarchyScopeViewsAtTapIndex) {
  84. candidateViews = self.viewsAtTap;
  85. } else if (self.searchBar.selectedScopeButtonIndex == kFLEXHierarchyScopeFullHierarchyIndex) {
  86. candidateViews = self.allViews;
  87. }
  88. } else {
  89. candidateViews = self.allViews;
  90. }
  91. if ([self.searchBar.text length] > 0) {
  92. self.displayedViews = [candidateViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary *bindings) {
  93. NSString *title = [FLEXUtility descriptionForView:candidateView includingFrame:NO];
  94. return [title rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch].location != NSNotFound;
  95. }]];
  96. } else {
  97. self.displayedViews = candidateViews;
  98. }
  99. [self.tableView reloadData];
  100. }
  101. - (BOOL)showScopeBar
  102. {
  103. return [self.viewsAtTap count] > 0;
  104. }
  105. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
  106. {
  107. [self updateDisplayedViews];
  108. // If the search bar text field is active, don't scroll on selection because we may want to continue typing.
  109. // Otherwise, scroll so that the selected cell is visible.
  110. UITableViewScrollPosition scrollPosition = self.searchBar.isFirstResponder ? UITableViewScrollPositionNone : UITableViewScrollPositionMiddle;
  111. [self trySelectCellForSelectedViewWithScrollPosition:scrollPosition];
  112. }
  113. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  114. {
  115. [self updateDisplayedViews];
  116. [self trySelectCellForSelectedViewWithScrollPosition:UITableViewScrollPositionNone];
  117. }
  118. #pragma mark - Table View Data Source
  119. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  120. {
  121. return 1;
  122. }
  123. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  124. {
  125. return [self.displayedViews count];
  126. }
  127. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. static NSString *CellIdentifier = @"Cell";
  130. FLEXHierarchyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  131. if (!cell) {
  132. cell = [[FLEXHierarchyTableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  133. }
  134. UIView *view = [self.displayedViews objectAtIndex:indexPath.row];
  135. NSNumber *depth = [self.depthsForViews objectForKey:[NSValue valueWithNonretainedObject:view]];
  136. UIColor *viewColor = [FLEXUtility consistentRandomColorForObject:view];
  137. cell.textLabel.text = [FLEXUtility descriptionForView:view includingFrame:NO];
  138. cell.detailTextLabel.text = [FLEXUtility detailDescriptionForView:view];
  139. cell.viewColor = viewColor;
  140. cell.viewDepth = [depth integerValue];
  141. if (view.isHidden || view.alpha < 0.01) {
  142. cell.textLabel.textColor = [UIColor lightGrayColor];
  143. cell.detailTextLabel.textColor = [UIColor lightGrayColor];
  144. } else {
  145. cell.textLabel.textColor = [UIColor blackColor];
  146. cell.detailTextLabel.textColor = [UIColor blackColor];
  147. }
  148. return cell;
  149. }
  150. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  151. {
  152. self.selectedView = [self.displayedViews objectAtIndex:indexPath.row];
  153. [self.delegate hierarchyViewController:self didFinishWithSelectedView:self.selectedView];
  154. }
  155. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  156. {
  157. UIView *drillInView = [self.displayedViews objectAtIndex:indexPath.row];
  158. FLEXObjectExplorerViewController *viewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:drillInView];
  159. [self.navigationController pushViewController:viewExplorer animated:YES];
  160. }
  161. #pragma mark - Button Actions
  162. - (void)donePressed:(id)sender
  163. {
  164. [self.delegate hierarchyViewController:self didFinishWithSelectedView:self.selectedView];
  165. }
  166. @end