FLEXObjectExplorerViewController.m 15 KB

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