FLEXObjectExplorerViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 = [FLEXRuntimeUtility safeClassNameForObject:self.object];
  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. [self.navigationController setToolbarHidden:NO animated:YES];
  116. return YES;
  117. }
  118. #pragma mark - Overrides
  119. /// Override to hide the description section when searching
  120. - (NSArray<FLEXTableViewSection *> *)nonemptySections {
  121. if (self.shouldShowDescription) {
  122. return super.nonemptySections;
  123. }
  124. return [super.nonemptySections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
  125. return section != self.descriptionSection;
  126. }];
  127. }
  128. - (NSArray<FLEXTableViewSection *> *)makeSections {
  129. FLEXObjectExplorer *explorer = self.explorer;
  130. // Description section is only for instances
  131. if (self.explorer.objectIsInstance) {
  132. _descriptionSection = [FLEXSingleRowSection
  133. title:@"Description" reuse:kFLEXMultilineCell cell:^(FLEXTableViewCell *cell) {
  134. cell.titleLabel.font = UIFont.flex_defaultTableCellFont;
  135. cell.titleLabel.text = explorer.objectDescription;
  136. }
  137. ];
  138. self.descriptionSection.filterMatcher = ^BOOL(NSString *filterText) {
  139. return [explorer.objectDescription localizedCaseInsensitiveContainsString:filterText];
  140. };
  141. }
  142. // Object graph section
  143. FLEXSingleRowSection *referencesSection = [FLEXSingleRowSection
  144. title:@"Object Graph" reuse:kFLEXDefaultCell cell:^(FLEXTableViewCell *cell) {
  145. cell.titleLabel.text = @"See Objects with References to This Object";
  146. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  147. }
  148. ];
  149. referencesSection.selectionAction = ^(UIViewController *host) {
  150. UIViewController *references = [FLEXObjectListViewController
  151. objectsWithReferencesToObject:explorer.object
  152. ];
  153. [host.navigationController pushViewController:references animated:YES];
  154. };
  155. NSMutableArray *sections = [NSMutableArray arrayWithArray:@[
  156. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProperties],
  157. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassProperties],
  158. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindIvars],
  159. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindMethods],
  160. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassMethods],
  161. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassHierarchy],
  162. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProtocols],
  163. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindOther],
  164. referencesSection
  165. ]];
  166. if (self.customSection) {
  167. [sections insertObject:self.customSection atIndex:0];
  168. }
  169. if (self.descriptionSection) {
  170. [sections insertObject:self.descriptionSection atIndex:0];
  171. }
  172. return sections.copy;
  173. }
  174. /// In our case, all this does is reload the table view,
  175. /// or reload the sections' data if we changed places
  176. /// in the class hierarchy. Doesn't refresh \c self.explorer
  177. - (void)reloadData {
  178. // Check to see if class scope changed, update accordingly
  179. if (self.explorer.classScope != self.selectedScope) {
  180. self.explorer.classScope = self.selectedScope;
  181. [self reloadSections];
  182. }
  183. [super reloadData];
  184. }
  185. - (void)shareButtonPressed:(UIBarButtonItem *)sender {
  186. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  187. make.button(@"Add to Bookmarks").handler(^(NSArray<NSString *> *strings) {
  188. [FLEXBookmarkManager.bookmarks addObject:self.object];
  189. });
  190. make.button(@"Copy Description").handler(^(NSArray<NSString *> *strings) {
  191. UIPasteboard.generalPasteboard.string = self.explorer.objectDescription;
  192. });
  193. make.button(@"Copy Address").handler(^(NSArray<NSString *> *strings) {
  194. UIPasteboard.generalPasteboard.string = [FLEXUtility addressOfObject:self.object];
  195. });
  196. make.button(@"Cancel").cancelStyle();
  197. } showFrom:self source:sender];
  198. }
  199. #pragma mark - Private
  200. /// Unlike \c -reloadData, this refreshes everything, including the explorer.
  201. - (void)fullyReloadData {
  202. [self.explorer reloadMetadata];
  203. [self reloadSections];
  204. [self reloadData];
  205. }
  206. - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)gesture {
  207. if (gesture.state == UIGestureRecognizerStateEnded) {
  208. switch (gesture.direction) {
  209. case UISwipeGestureRecognizerDirectionRight:
  210. if (self.selectedScope > 0) {
  211. self.selectedScope -= 1;
  212. }
  213. break;
  214. case UISwipeGestureRecognizerDirectionLeft:
  215. if (self.selectedScope != self.explorer.classHierarchy.count - 1) {
  216. self.selectedScope += 1;
  217. }
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. }
  224. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)g1 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)g2 {
  225. // Prioritize important pan gestures over our swipe gesture
  226. if ([g2 isKindOfClass:[UIPanGestureRecognizer class]]) {
  227. if (g2 == self.navigationController.interactivePopGestureRecognizer ||
  228. g2 == self.navigationController.barHideOnSwipeGestureRecognizer ||
  229. g2 == self.tableView.panGestureRecognizer) {
  230. return NO;
  231. }
  232. }
  233. return YES;
  234. }
  235. - (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gesture {
  236. // Don't allow swiping from the carousel
  237. CGPoint location = [gesture locationInView:self.tableView];
  238. if ([self.carousel hitTest:location withEvent:nil]) {
  239. return NO;
  240. }
  241. return YES;
  242. }
  243. - (void)moreButtonPressed:(UIBarButtonItem *)sender {
  244. NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
  245. // Maps preference keys to a description of what they affect
  246. NSDictionary<NSString *, NSString *> *explorerToggles = @{
  247. kFLEXDefaultsHidePropertyIvarsKey: @"Property-Backing Ivars",
  248. kFLEXDefaultsHidePropertyMethodsKey: @"Property-Backing Methods",
  249. kFLEXDefaultsHideMethodOverridesKey: @"Method Overrides",
  250. kFLEXDefaultsHideVariablePreviewsKey: @"Variable Previews"
  251. };
  252. // Maps the key of the action itself to a map of a description
  253. // of the action ("hide X") mapped to the current state.
  254. //
  255. // So keys that are hidden by default have NO mapped to "Show"
  256. NSDictionary<NSString *, NSDictionary *> *nextStateDescriptions = @{
  257. kFLEXDefaultsHidePropertyIvarsKey: @{ @NO: @"Hide ", @YES: @"Show " },
  258. kFLEXDefaultsHidePropertyMethodsKey: @{ @NO: @"Hide ", @YES: @"Show " },
  259. kFLEXDefaultsHideMethodOverridesKey: @{ @NO: @"Show ", @YES: @"Hide " },
  260. kFLEXDefaultsHideVariablePreviewsKey: @{ @NO: @"Hide ", @YES: @"Show " },
  261. };
  262. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  263. make.title(@"Options");
  264. for (NSString *option in explorerToggles.allKeys) {
  265. BOOL current = [defaults boolForKey:option];
  266. NSString *title = [nextStateDescriptions[option][@(current)]
  267. stringByAppendingString:explorerToggles[option]
  268. ];
  269. make.button(title).handler(^(NSArray<NSString *> *strings) {
  270. [NSUserDefaults.standardUserDefaults flex_toggleBoolForKey:option];
  271. [self fullyReloadData];
  272. });
  273. }
  274. make.button(@"Cancel").cancelStyle();
  275. } showFrom:self source:sender];
  276. }
  277. #pragma mark - Description
  278. - (BOOL)shouldShowDescription {
  279. // Hide if we have filter text; it is rarely
  280. // useful to see the description when searching
  281. // since it's already at the top of the screen
  282. if (self.filterText.length) {
  283. return NO;
  284. }
  285. return YES;
  286. }
  287. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  288. // For the description section, we want that nice slim/snug looking row.
  289. // Other rows use the automatic size.
  290. FLEXTableViewSection *section = self.filterDelegate.sections[indexPath.section];
  291. if (section == self.descriptionSection) {
  292. NSAttributedString *attributedText = [[NSAttributedString alloc]
  293. initWithString:self.explorer.objectDescription
  294. attributes:@{ NSFontAttributeName : UIFont.flex_defaultTableCellFont }
  295. ];
  296. return [FLEXMultilineTableViewCell
  297. preferredHeightWithAttributedText:attributedText
  298. maxWidth:tableView.frame.size.width - tableView.separatorInset.right
  299. style:tableView.style
  300. showsAccessory:NO
  301. ];
  302. }
  303. return UITableViewAutomaticDimension;
  304. }
  305. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  306. return self.filterDelegate.sections[indexPath.section] == self.descriptionSection;
  307. }
  308. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  309. // Only the description section has "actions"
  310. if (self.filterDelegate.sections[indexPath.section] == self.descriptionSection) {
  311. return action == @selector(copy:);
  312. }
  313. return NO;
  314. }
  315. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  316. if (action == @selector(copy:)) {
  317. UIPasteboard.generalPasteboard.string = self.explorer.objectDescription;
  318. }
  319. }
  320. @end