FLEXObjectExplorerViewController.m 14 KB

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