FLEXObjectExplorerViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 "FLEXInstancesViewController.h"
  17. #import "FLEXTabsViewController.h"
  18. #import "FLEXBookmarkManager.h"
  19. #import "FLEXTableView.h"
  20. #import "FLEXTableViewCell.h"
  21. #import "FLEXScopeCarousel.h"
  22. #import "FLEXMetadataSection.h"
  23. #import "FLEXSingleRowSection.h"
  24. #import "FLEXShortcutsSection.h"
  25. #import <objc/runtime.h>
  26. #pragma mark - Private properties
  27. @interface FLEXObjectExplorerViewController () <UIGestureRecognizerDelegate>
  28. @property (nonatomic, copy) NSString *filterText;
  29. /// Every section in the table view, regardless of whether or not a section is empty.
  30. @property (nonatomic, readonly) NSArray<FLEXTableViewSection *> *allSections;
  31. /// Only displayed sections of the table view; empty sections are purged from this array.
  32. @property (nonatomic) NSArray<FLEXTableViewSection *> *sections;
  33. @property (nonatomic, readonly) FLEXSingleRowSection *descriptionSection;
  34. @property (nonatomic, readonly) FLEXTableViewSection *customSection;
  35. @property (nonatomic) NSIndexSet *customSectionVisibleIndexes;
  36. @end
  37. @implementation FLEXObjectExplorerViewController
  38. #pragma mark - Initialization
  39. + (instancetype)exploringObject:(id)target {
  40. return [self exploringObject:target customSection:[FLEXShortcutsSection forObject:target]];
  41. }
  42. + (instancetype)exploringObject:(id)target customSection:(FLEXTableViewSection *)section {
  43. return [[self alloc]
  44. initWithObject:target
  45. explorer:[FLEXObjectExplorer forObject:target]
  46. customSection:section
  47. ];
  48. }
  49. - (id)initWithObject:(id)target
  50. explorer:(__kindof FLEXObjectExplorer *)explorer
  51. customSection:(FLEXTableViewSection *)customSection {
  52. NSParameterAssert(target);
  53. self = [super init];
  54. if (self) {
  55. _object = target;
  56. _explorer = explorer;
  57. _customSection = customSection;
  58. _allSections = [self makeSections];
  59. }
  60. return self;
  61. }
  62. #pragma mark - View controller lifecycle
  63. - (void)loadView {
  64. FLEXTableView *tableView = [[FLEXTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  65. self.tableView = tableView;
  66. // Register cell classes
  67. for (FLEXTableViewSection *section in self.allSections) {
  68. if (section.cellRegistrationMapping) {
  69. [tableView registerCells:section.cellRegistrationMapping];
  70. }
  71. }
  72. }
  73. - (void)viewDidLoad {
  74. [super viewDidLoad];
  75. self.showsShareToolbarItem = YES;
  76. // Use [object class] here rather than object_getClass
  77. // to avoid the KVO prefix for observed objects
  78. self.title = [[self.object class] description];
  79. // Refresh
  80. self.refreshControl = [UIRefreshControl new];
  81. [self.refreshControl
  82. addTarget:self
  83. action:@selector(refreshControlDidRefresh:)
  84. forControlEvents:UIControlEventValueChanged
  85. ];
  86. // Search
  87. self.showsSearchBar = YES;
  88. self.searchBarDebounceInterval = kFLEXDebounceInstant;
  89. self.showsCarousel = YES;
  90. // Carousel scope bar
  91. [self.explorer reloadClassHierarchy];
  92. self.carousel.items = [self.explorer.classHierarchyClasses flex_mapped:^id(Class cls, NSUInteger idx) {
  93. return NSStringFromClass(cls);
  94. }];
  95. // Swipe gestures to swipe between classes in the hierarchy
  96. UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]
  97. initWithTarget:self action:@selector(handleSwipeGesture:)
  98. ];
  99. leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
  100. UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]
  101. initWithTarget:self action:@selector(handleSwipeGesture:)
  102. ];
  103. rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
  104. leftSwipe.delegate = self;
  105. rightSwipe.delegate = self;
  106. [self.tableView addGestureRecognizer:leftSwipe];
  107. [self.tableView addGestureRecognizer:rightSwipe];
  108. }
  109. - (void)viewWillAppear:(BOOL)animated {
  110. [super viewWillAppear:animated];
  111. // Reload the entire table view rather than just the visible cells, because the filtered rows
  112. // may have changed (i.e. a change in the description row that causes it to get filtered out).
  113. [self reloadData];
  114. }
  115. #pragma mark - Private
  116. - (void)refreshControlDidRefresh:(id)sender {
  117. [self reloadData];
  118. [self.refreshControl endRefreshing];
  119. }
  120. - (NSArray<FLEXTableViewSection *> *)makeSections {
  121. FLEXObjectExplorer *explorer = self.explorer;
  122. // Description section is only for instances
  123. if (self.explorer.objectIsInstance) {
  124. _descriptionSection = [FLEXSingleRowSection
  125. title:@"Description" reuse:kFLEXMultilineCell cell:^(FLEXTableViewCell *cell) {
  126. cell.titleLabel.font = UIFont.flex_defaultTableCellFont;
  127. cell.titleLabel.text = explorer.objectDescription;
  128. }
  129. ];
  130. self.descriptionSection.filterMatcher = ^BOOL(NSString *filterText) {
  131. return [explorer.objectDescription localizedCaseInsensitiveContainsString:filterText];
  132. };
  133. }
  134. // Object graph section
  135. FLEXSingleRowSection *referencesSection = [FLEXSingleRowSection
  136. title:@"Object Graph" reuse:kFLEXDefaultCell cell:^(FLEXTableViewCell *cell) {
  137. cell.titleLabel.text = @"See Objects with References to This Object";
  138. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  139. }
  140. ];
  141. referencesSection.selectionAction = ^(UIViewController *host) {
  142. UIViewController *references = [FLEXInstancesViewController
  143. instancesTableViewControllerForInstancesReferencingObject:explorer.object
  144. ];
  145. [host.navigationController pushViewController:references animated:YES];
  146. };
  147. NSMutableArray *sections = [NSMutableArray arrayWithArray:@[
  148. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProperties],
  149. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassProperties],
  150. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindIvars],
  151. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindMethods],
  152. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassMethods],
  153. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassHierarchy],
  154. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProtocols],
  155. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindOther],
  156. referencesSection
  157. ]];
  158. if (self.customSection) {
  159. [sections insertObject:self.customSection atIndex:0];
  160. }
  161. if (self.descriptionSection) {
  162. [sections insertObject:self.descriptionSection atIndex:0];
  163. }
  164. return sections.copy;
  165. }
  166. - (NSArray<FLEXTableViewSection *> *)nonemptySections {
  167. return [self.allSections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
  168. return section.numberOfRows > 0;
  169. }];
  170. }
  171. - (BOOL)sectionHasActions:(NSInteger)section {
  172. return self.sections[section] == self.descriptionSection;
  173. }
  174. - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)gesture {
  175. if (gesture.state == UIGestureRecognizerStateEnded) {
  176. switch (gesture.direction) {
  177. case UISwipeGestureRecognizerDirectionRight:
  178. if (self.selectedScope > 0) {
  179. self.selectedScope -= 1;
  180. }
  181. break;
  182. case UISwipeGestureRecognizerDirectionLeft:
  183. if (self.selectedScope != self.explorer.classHierarchy.count - 1) {
  184. self.selectedScope += 1;
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. }
  192. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)g1 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)g2 {
  193. return [g2 isKindOfClass:[UIPanGestureRecognizer class]] && g2 != self.navigationController.interactivePopGestureRecognizer;
  194. }
  195. - (void)shareButtonPressed {
  196. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  197. make.button(@"Add to Bookmarks").handler(^(NSArray<NSString *> *strings) {
  198. [FLEXBookmarkManager.bookmarks addObject:self.object];
  199. });
  200. make.button(@"Copy Description").handler(^(NSArray<NSString *> *strings) {
  201. UIPasteboard.generalPasteboard.string = self.explorer.objectDescription;
  202. });
  203. make.button(@"Copy Address").handler(^(NSArray<NSString *> *strings) {
  204. [self copyObjectAddress:nil];
  205. });
  206. make.button(@"Cancel").cancelStyle();
  207. } showFrom:self];
  208. }
  209. #pragma mark - Description
  210. - (BOOL)shouldShowDescription {
  211. // Hide if we have filter text; it is rarely
  212. // useful to see the description when searching
  213. // since it's already at the top of the screen
  214. if (self.filterText.length) {
  215. return NO;
  216. }
  217. return YES;
  218. }
  219. #pragma mark - Search
  220. - (void)updateSearchResults:(NSString *)newText; {
  221. self.filterText = newText;
  222. // Sections will adjust data based on this property
  223. for (FLEXTableViewSection *section in self.allSections) {
  224. section.filterText = newText;
  225. }
  226. // Check to see if class scope changed, update accordingly
  227. if (self.explorer.classScope != self.selectedScope) {
  228. self.explorer.classScope = self.selectedScope;
  229. for (FLEXTableViewSection *section in self.allSections) {
  230. [section reloadData];
  231. }
  232. }
  233. // Recalculate empty sections
  234. self.sections = [self nonemptySections];
  235. // Refresh table view
  236. if (self.isViewLoaded) {
  237. [self.tableView reloadData];
  238. }
  239. }
  240. #pragma mark - Reloading
  241. - (void)reloadData {
  242. // Reload explorer
  243. [self.explorer reloadMetadata];
  244. // Reload sections
  245. for (FLEXTableViewSection *section in self.allSections) {
  246. [section reloadData];
  247. }
  248. // Recalculate displayed sections
  249. self.sections = [self nonemptySections];
  250. // Refresh table view
  251. if (self.isViewLoaded) {
  252. [self.tableView reloadData];
  253. }
  254. }
  255. #pragma mark - UITableViewDataSource
  256. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  257. return self.sections.count;
  258. }
  259. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  260. return self.sections[section].numberOfRows;
  261. }
  262. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  263. return self.sections[section].title;
  264. }
  265. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  266. NSString *reuse = [self.sections[indexPath.section] reuseIdentifierForRow:indexPath.row];
  267. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse forIndexPath:indexPath];
  268. [self.sections[indexPath.section] configureCell:cell forRow:indexPath.row];
  269. return cell;
  270. }
  271. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  272. // For the description section, we want that nice slim/snug looking row.
  273. // Other rows use the automatic size.
  274. FLEXTableViewSection *section = self.sections[indexPath.section];
  275. if (section == self.descriptionSection) {
  276. NSAttributedString *attributedText = [[NSAttributedString alloc]
  277. initWithString:self.explorer.objectDescription
  278. attributes:@{ NSFontAttributeName : UIFont.flex_defaultTableCellFont }
  279. ];
  280. return [FLEXMultilineTableViewCell
  281. preferredHeightWithAttributedText:attributedText
  282. maxWidth:tableView.frame.size.width - tableView.separatorInset.right
  283. style:tableView.style
  284. showsAccessory:NO
  285. ];
  286. }
  287. return UITableViewAutomaticDimension;
  288. }
  289. - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  290. return [self.sections flex_mapped:^id(FLEXTableViewSection *obj, NSUInteger idx) {
  291. return @"⦁";
  292. }];
  293. }
  294. #pragma mark - UITableViewDelegate
  295. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  296. return [self.sections[indexPath.section] canSelectRow:indexPath.row];
  297. }
  298. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  299. FLEXTableViewSection *section = self.sections[indexPath.section];
  300. void (^action)(UIViewController *) = [section didSelectRowAction:indexPath.row];
  301. UIViewController *details = [section viewControllerToPushForRow:indexPath.row];
  302. if (action) {
  303. action(self);
  304. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  305. } else if (details) {
  306. [self.navigationController pushViewController:details animated:YES];
  307. } else {
  308. [NSException raise:NSInternalInconsistencyException
  309. format:@"Row is selectable but has no action or view controller"];
  310. }
  311. }
  312. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  313. return [self sectionHasActions:indexPath.section];
  314. }
  315. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  316. // Only the description section has "actions"
  317. if (self.sections[indexPath.section] == self.descriptionSection) {
  318. return action == @selector(copy:) || action == @selector(copyObjectAddress:);
  319. }
  320. return NO;
  321. }
  322. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  323. #pragma clang diagnostic push
  324. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  325. [self performSelector:action withObject:indexPath];
  326. #pragma clang diagnostic pop
  327. }
  328. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
  329. [self.sections[indexPath.section] didPressInfoButtonAction:indexPath.row](self);
  330. }
  331. #if FLEX_AT_LEAST_IOS13_SDK
  332. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  333. FLEXTableViewSection *section = self.sections[indexPath.section];
  334. NSString *title = [section menuTitleForRow:indexPath.row];
  335. NSArray<UIMenuElement *> *menuItems = [section menuItemsForRow:indexPath.row sender:self];
  336. if (menuItems.count) {
  337. return [UIContextMenuConfiguration
  338. configurationWithIdentifier:nil
  339. previewProvider:nil
  340. actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
  341. return [UIMenu menuWithTitle:title children:menuItems];
  342. }
  343. ];
  344. }
  345. return nil;
  346. }
  347. #endif
  348. #pragma mark - UIMenuController
  349. /// Prevent the search bar from trying to use us as a responder
  350. ///
  351. /// Our table cells will use the UITableViewDelegate methods
  352. /// to make sure we can perform the actions we want to
  353. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  354. return NO;
  355. }
  356. - (void)copy:(NSIndexPath *)indexPath {
  357. FLEXTableViewSection *section = self.sections[indexPath.section];
  358. UIPasteboard.generalPasteboard.string = ({
  359. NSString *copy = [section titleForRow:indexPath.row];
  360. NSString *subtitle = [section subtitleForRow:indexPath.row];
  361. if (subtitle.length) {
  362. copy = [NSString stringWithFormat:@"%@\n\n%@", copy, subtitle];
  363. }
  364. // If no string was provided, don't overwrite the pasteboard
  365. copy.length > 2 ? copy : UIPasteboard.generalPasteboard.string;
  366. });
  367. }
  368. - (void)copyObjectAddress:(NSIndexPath *)indexPath {
  369. UIPasteboard.generalPasteboard.string = [FLEXUtility addressOfObject:self.object];
  370. }
  371. @end