FLEXObjectExplorerViewController.m 14 KB

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