FLEXObjectExplorerViewController.m 15 KB

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