FLEXObjectExplorerViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // FLEXObjectExplorerViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2014-05-03.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXObjectExplorerViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "UIBarButtonItem+FLEX.h"
  12. #import "FLEXMultilineTableViewCell.h"
  13. #import "FLEXObjectExplorerFactory.h"
  14. #import "FLEXFieldEditorViewController.h"
  15. #import "FLEXMethodCallingViewController.h"
  16. #import "FLEXObjectListViewController.h"
  17. #import "FLEXTabsViewController.h"
  18. #import "FLEXBookmarkManager.h"
  19. #import "FLEXTableView.h"
  20. #import "FLEXTableViewCell.h"
  21. #import "FLEXScopeCarousel.h"
  22. #import "FLEXMetadataSection.h"
  23. #import "FLEXSingleRowSection.h"
  24. #import "FLEXShortcutsSection.h"
  25. #import <objc/runtime.h>
  26. #pragma mark - Private properties
  27. @interface FLEXObjectExplorerViewController () <UIGestureRecognizerDelegate>
  28. @property (nonatomic, readonly) FLEXSingleRowSection *descriptionSection;
  29. @property (nonatomic, readonly) FLEXTableViewSection *customSection;
  30. @property (nonatomic) NSIndexSet *customSectionVisibleIndexes;
  31. @end
  32. @implementation FLEXObjectExplorerViewController
  33. #pragma mark - Initialization
  34. + (instancetype)exploringObject:(id)target {
  35. return [self exploringObject:target customSection:[FLEXShortcutsSection forObject:target]];
  36. }
  37. + (instancetype)exploringObject:(id)target customSection:(FLEXTableViewSection *)section {
  38. return [[self alloc]
  39. initWithObject:target
  40. explorer:[FLEXObjectExplorer forObject:target]
  41. customSection:section
  42. ];
  43. }
  44. - (id)initWithObject:(id)target
  45. explorer:(__kindof FLEXObjectExplorer *)explorer
  46. customSection:(FLEXTableViewSection *)customSection {
  47. NSParameterAssert(target);
  48. self = [super initWithStyle:UITableViewStyleGrouped];
  49. if (self) {
  50. _object = target;
  51. _explorer = explorer;
  52. _customSection = customSection;
  53. }
  54. return self;
  55. }
  56. #pragma mark - View controller lifecycle
  57. - (void)viewDidLoad {
  58. [super viewDidLoad];
  59. self.showsShareToolbarItem = YES;
  60. self.wantsSectionIndexTitles = YES;
  61. // Use [object class] here rather than object_getClass
  62. // to avoid the KVO prefix for observed objects
  63. self.title = [[self.object class] description];
  64. // Search
  65. self.showsSearchBar = YES;
  66. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  67. self.showsCarousel = YES;
  68. // Carousel scope bar
  69. [self.explorer reloadClassHierarchy];
  70. self.carousel.items = [self.explorer.classHierarchyClasses flex_mapped:^id(Class cls, NSUInteger idx) {
  71. return NSStringFromClass(cls);
  72. }];
  73. // Swipe gestures to swipe between classes in the hierarchy
  74. UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]
  75. initWithTarget:self action:@selector(handleSwipeGesture:)
  76. ];
  77. UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]
  78. initWithTarget:self action:@selector(handleSwipeGesture:)
  79. ];
  80. leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
  81. rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
  82. leftSwipe.delegate = self;
  83. rightSwipe.delegate = self;
  84. [self.tableView addGestureRecognizer:leftSwipe];
  85. [self.tableView addGestureRecognizer:rightSwipe];
  86. }
  87. #pragma mark - Overrides
  88. /// Override to hide the description section when searching
  89. - (NSArray<FLEXTableViewSection *> *)nonemptySections {
  90. if (self.shouldShowDescription) {
  91. return super.nonemptySections;
  92. }
  93. return [super.nonemptySections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
  94. return section != self.descriptionSection;
  95. }];
  96. }
  97. - (NSArray<FLEXTableViewSection *> *)makeSections {
  98. FLEXObjectExplorer *explorer = self.explorer;
  99. // Description section is only for instances
  100. if (self.explorer.objectIsInstance) {
  101. _descriptionSection = [FLEXSingleRowSection
  102. title:@"Description" reuse:kFLEXMultilineCell cell:^(FLEXTableViewCell *cell) {
  103. cell.titleLabel.font = UIFont.flex_defaultTableCellFont;
  104. cell.titleLabel.text = explorer.objectDescription;
  105. }
  106. ];
  107. self.descriptionSection.filterMatcher = ^BOOL(NSString *filterText) {
  108. return [explorer.objectDescription localizedCaseInsensitiveContainsString:filterText];
  109. };
  110. }
  111. // Object graph section
  112. FLEXSingleRowSection *referencesSection = [FLEXSingleRowSection
  113. title:@"Object Graph" reuse:kFLEXDefaultCell cell:^(FLEXTableViewCell *cell) {
  114. cell.titleLabel.text = @"See Objects with References to This Object";
  115. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  116. }
  117. ];
  118. referencesSection.selectionAction = ^(UIViewController *host) {
  119. UIViewController *references = [FLEXObjectListViewController
  120. objectsWithReferencesToObject:explorer.object
  121. ];
  122. [host.navigationController pushViewController:references animated:YES];
  123. };
  124. NSMutableArray *sections = [NSMutableArray arrayWithArray:@[
  125. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProperties],
  126. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassProperties],
  127. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindIvars],
  128. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindMethods],
  129. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassMethods],
  130. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassHierarchy],
  131. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProtocols],
  132. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindOther],
  133. referencesSection
  134. ]];
  135. if (self.customSection) {
  136. [sections insertObject:self.customSection atIndex:0];
  137. }
  138. if (self.descriptionSection) {
  139. [sections insertObject:self.descriptionSection atIndex:0];
  140. }
  141. return sections.copy;
  142. }
  143. - (void)reloadData {
  144. // Check to see if class scope changed, update accordingly
  145. if (self.explorer.classScope != self.selectedScope) {
  146. self.explorer.classScope = self.selectedScope;
  147. [self reloadSections];
  148. }
  149. [super reloadData];
  150. }
  151. #pragma mark - Private
  152. - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)gesture {
  153. if (gesture.state == UIGestureRecognizerStateEnded) {
  154. switch (gesture.direction) {
  155. case UISwipeGestureRecognizerDirectionRight:
  156. if (self.selectedScope > 0) {
  157. self.selectedScope -= 1;
  158. }
  159. break;
  160. case UISwipeGestureRecognizerDirectionLeft:
  161. if (self.selectedScope != self.explorer.classHierarchy.count - 1) {
  162. self.selectedScope += 1;
  163. }
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. }
  170. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)g1 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)g2 {
  171. // Prioritize important pan gestures over our swipe gesture
  172. if ([g2 isKindOfClass:[UIPanGestureRecognizer class]]) {
  173. if (g2 == self.navigationController.interactivePopGestureRecognizer ||
  174. g2 == self.navigationController.barHideOnSwipeGestureRecognizer ||
  175. g2 == self.tableView.panGestureRecognizer) {
  176. return NO;
  177. }
  178. }
  179. return YES;
  180. }
  181. - (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gesture {
  182. // Don't allow swiping from the carousel
  183. CGPoint location = [gesture locationInView:self.tableView];
  184. if ([self.carousel hitTest:location withEvent:nil]) {
  185. return NO;
  186. }
  187. return YES;
  188. }
  189. - (void)shareButtonPressed {
  190. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  191. make.button(@"Add to Bookmarks").handler(^(NSArray<NSString *> *strings) {
  192. [FLEXBookmarkManager.bookmarks addObject:self.object];
  193. });
  194. make.button(@"Copy Description").handler(^(NSArray<NSString *> *strings) {
  195. UIPasteboard.generalPasteboard.string = self.explorer.objectDescription;
  196. });
  197. make.button(@"Copy Address").handler(^(NSArray<NSString *> *strings) {
  198. UIPasteboard.generalPasteboard.string = [FLEXUtility addressOfObject:self.object];
  199. });
  200. make.button(@"Cancel").cancelStyle();
  201. } showFrom:self];
  202. }
  203. #pragma mark - Description
  204. - (BOOL)shouldShowDescription {
  205. // Hide if we have filter text; it is rarely
  206. // useful to see the description when searching
  207. // since it's already at the top of the screen
  208. if (self.filterText.length) {
  209. return NO;
  210. }
  211. return YES;
  212. }
  213. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  214. // For the description section, we want that nice slim/snug looking row.
  215. // Other rows use the automatic size.
  216. FLEXTableViewSection *section = self.filterDelegate.sections[indexPath.section];
  217. if (section == self.descriptionSection) {
  218. NSAttributedString *attributedText = [[NSAttributedString alloc]
  219. initWithString:self.explorer.objectDescription
  220. attributes:@{ NSFontAttributeName : UIFont.flex_defaultTableCellFont }
  221. ];
  222. return [FLEXMultilineTableViewCell
  223. preferredHeightWithAttributedText:attributedText
  224. maxWidth:tableView.frame.size.width - tableView.separatorInset.right
  225. style:tableView.style
  226. showsAccessory:NO
  227. ];
  228. }
  229. return UITableViewAutomaticDimension;
  230. }
  231. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  232. return self.filterDelegate.sections[indexPath.section] == self.descriptionSection;
  233. }
  234. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  235. // Only the description section has "actions"
  236. if (self.filterDelegate.sections[indexPath.section] == self.descriptionSection) {
  237. return action == @selector(copy:);
  238. }
  239. return NO;
  240. }
  241. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  242. if (action == @selector(copy:)) {
  243. UIPasteboard.generalPasteboard.string = self.explorer.objectDescription;
  244. }
  245. }
  246. @end