FLEXObjectExplorerViewController.m 12 KB

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