FLEXObjectExplorerViewController.m 15 KB

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