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. #pragma mark Selection and Filtering Helpers
  67. - (void)trySelectCellForSelectedViewWithScrollPosition:(UITableViewScrollPosition)scrollPosition
  68. {
  69. NSUInteger selectedViewIndex = [self.displayedViews indexOfObject:self.selectedView];
  70. if (selectedViewIndex != NSNotFound) {
  71. NSIndexPath *selectedViewIndexPath = [NSIndexPath indexPathForRow:selectedViewIndex inSection:0];
  72. [self.tableView selectRowAtIndexPath:selectedViewIndexPath animated:YES scrollPosition:scrollPosition];
  73. }
  74. }
  75. - (void)updateDisplayedViews
  76. {
  77. NSArray *candidateViews = nil;
  78. if ([self showScopeBar]) {
  79. if (self.searchBar.selectedScopeButtonIndex == kFLEXHierarchyScopeViewsAtTapIndex) {
  80. candidateViews = self.viewsAtTap;
  81. } else if (self.searchBar.selectedScopeButtonIndex == kFLEXHierarchyScopeFullHierarchyIndex) {
  82. candidateViews = self.allViews;
  83. }
  84. } else {
  85. candidateViews = self.allViews;
  86. }
  87. if ([self.searchBar.text length] > 0) {
  88. self.displayedViews = [candidateViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIView *candidateView, NSDictionary *bindings) {
  89. NSString *title = [FLEXUtility descriptionForView:candidateView includingFrame:NO];
  90. return [title rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch].location != NSNotFound;
  91. }]];
  92. } else {
  93. self.displayedViews = candidateViews;
  94. }
  95. [self.tableView reloadData];
  96. }
  97. - (BOOL)showScopeBar
  98. {
  99. return [self.viewsAtTap count] > 0;
  100. }
  101. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
  102. {
  103. [self updateDisplayedViews];
  104. // If the search bar text field is active, don't scroll on selection because we may want to continue typing.
  105. // Otherwise, scroll so that the selected cell is visible.
  106. UITableViewScrollPosition scrollPosition = self.searchBar.isFirstResponder ? UITableViewScrollPositionNone : UITableViewScrollPositionMiddle;
  107. [self trySelectCellForSelectedViewWithScrollPosition:scrollPosition];
  108. }
  109. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  110. {
  111. [self updateDisplayedViews];
  112. [self trySelectCellForSelectedViewWithScrollPosition:UITableViewScrollPositionNone];
  113. }
  114. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  115. {
  116. [searchBar resignFirstResponder];
  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