FLEXHierarchyTableViewController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. [self.tableView setSeparatorInset:UIEdgeInsetsZero];
  48. self.searchBar = [[UISearchBar alloc] init];
  49. self.searchBar.placeholder = [FLEXUtility searchBarPlaceholderText];
  50. self.searchBar.delegate = self;
  51. if ([self showScopeBar]) {
  52. self.searchBar.showsScopeBar = YES;
  53. self.searchBar.scopeButtonTitles = @[@"Views at Tap", @"Full Hierarchy"];
  54. }
  55. [self.searchBar sizeToFit];
  56. self.tableView.tableHeaderView = self.searchBar;
  57. [self updateDisplayedViews];
  58. }
  59. - (void)viewDidAppear:(BOOL)animated
  60. {
  61. [super viewDidAppear:animated];
  62. [self trySelectCellForSelectedViewWithScrollPosition:UITableViewScrollPositionMiddle];
  63. }
  64. #pragma mark Selection and Filtering Helpers
  65. - (void)trySelectCellForSelectedViewWithScrollPosition:(UITableViewScrollPosition)scrollPosition
  66. {
  67. NSUInteger selectedViewIndex = [self.displayedViews indexOfObject:self.selectedView];
  68. if (selectedViewIndex != NSNotFound) {
  69. NSIndexPath *selectedViewIndexPath = [NSIndexPath indexPathForRow:selectedViewIndex inSection:0];
  70. [self.tableView selectRowAtIndexPath:selectedViewIndexPath animated:YES scrollPosition:scrollPosition];
  71. }
  72. }
  73. - (void)updateDisplayedViews
  74. {
  75. NSArray *candidateViews = nil;
  76. if ([self showScopeBar]) {
  77. if (self.searchBar.selectedScopeButtonIndex == kFLEXHierarchyScopeViewsAtTapIndex) {
  78. candidateViews = self.viewsAtTap;
  79. } else if (self.searchBar.selectedScopeButtonIndex == kFLEXHierarchyScopeFullHierarchyIndex) {
  80. candidateViews = self.allViews;
  81. }
  82. } else {
  83. candidateViews = self.allViews;
  84. }
  85. if ([self.searchBar.text length] > 0) {
  86. self.displayedViews = [candidateViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary *bindings) {
  87. NSString *title = [FLEXUtility descriptionForView:candidateView includingFrame:NO];
  88. return [title rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch].location != NSNotFound;
  89. }]];
  90. } else {
  91. self.displayedViews = candidateViews;
  92. }
  93. [self.tableView reloadData];
  94. }
  95. - (BOOL)showScopeBar
  96. {
  97. return [self.viewsAtTap count] > 0;
  98. }
  99. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
  100. {
  101. [self updateDisplayedViews];
  102. // If the search bar text field is active, don't scroll on selection because we may want to continue typing.
  103. // Otherwise, scroll so that the selected cell is visible.
  104. UITableViewScrollPosition scrollPosition = self.searchBar.isFirstResponder ? UITableViewScrollPositionNone : UITableViewScrollPositionMiddle;
  105. [self trySelectCellForSelectedViewWithScrollPosition:scrollPosition];
  106. }
  107. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  108. {
  109. [self updateDisplayedViews];
  110. [self trySelectCellForSelectedViewWithScrollPosition:UITableViewScrollPositionNone];
  111. }
  112. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  113. {
  114. [searchBar resignFirstResponder];
  115. }
  116. #pragma mark - Table View Data Source
  117. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  118. {
  119. return 1;
  120. }
  121. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  122. {
  123. return [self.displayedViews count];
  124. }
  125. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. static NSString *CellIdentifier = @"Cell";
  128. FLEXHierarchyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  129. if (!cell) {
  130. cell = [[FLEXHierarchyTableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  131. }
  132. UIView *view = [self.displayedViews objectAtIndex:indexPath.row];
  133. NSNumber *depth = [self.depthsForViews objectForKey:[NSValue valueWithNonretainedObject:view]];
  134. UIColor *viewColor = [FLEXUtility consistentRandomColorForObject:view];
  135. cell.textLabel.text = [FLEXUtility descriptionForView:view includingFrame:NO];
  136. cell.detailTextLabel.text = [FLEXUtility detailDescriptionForView:view];
  137. cell.viewColor = viewColor;
  138. cell.viewDepth = [depth integerValue];
  139. if (view.isHidden || view.alpha < 0.01) {
  140. cell.textLabel.textColor = [UIColor lightGrayColor];
  141. cell.detailTextLabel.textColor = [UIColor lightGrayColor];
  142. } else {
  143. cell.textLabel.textColor = [UIColor blackColor];
  144. cell.detailTextLabel.textColor = [UIColor blackColor];
  145. }
  146. return cell;
  147. }
  148. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  149. {
  150. self.selectedView = [self.displayedViews objectAtIndex:indexPath.row];
  151. [self.delegate hierarchyViewController:self didFinishWithSelectedView:self.selectedView];
  152. }
  153. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  154. {
  155. UIView *drillInView = [self.displayedViews objectAtIndex:indexPath.row];
  156. FLEXObjectExplorerViewController *viewExplorer = [FLEXObjectExplorerFactory explorerViewControllerForObject:drillInView];
  157. [self.navigationController pushViewController:viewExplorer animated:YES];
  158. }
  159. #pragma mark - Button Actions
  160. - (void)donePressed:(id)sender
  161. {
  162. [self.delegate hierarchyViewController:self didFinishWithSelectedView:self.selectedView];
  163. }
  164. @end