FLEXObjectExplorerViewController.m 16 KB

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