FLEXObjectExplorerViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #pragma mark - Private
  110. - (void)refreshControlDidRefresh:(id)sender {
  111. [self reloadData];
  112. [self.refreshControl endRefreshing];
  113. }
  114. - (NSArray<FLEXTableViewSection *> *)makeSections {
  115. FLEXObjectExplorer *explorer = self.explorer;
  116. // Description section is only for instances
  117. if (self.explorer.objectIsInstance) {
  118. _descriptionSection = [FLEXSingleRowSection
  119. title:@"Description" reuse:kFLEXMultilineCell cell:^(FLEXTableViewCell *cell) {
  120. cell.titleLabel.font = UIFont.flex_defaultTableCellFont;
  121. cell.titleLabel.text = explorer.objectDescription;
  122. }
  123. ];
  124. self.descriptionSection.filterMatcher = ^BOOL(NSString *filterText) {
  125. return [explorer.objectDescription localizedCaseInsensitiveContainsString:filterText];
  126. };
  127. }
  128. // Object graph section
  129. FLEXSingleRowSection *referencesSection = [FLEXSingleRowSection
  130. title:@"Object Graph" reuse:kFLEXDefaultCell cell:^(FLEXTableViewCell *cell) {
  131. cell.titleLabel.text = @"See Objects with References to This Object";
  132. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  133. }
  134. ];
  135. referencesSection.selectionAction = ^(UIViewController *host) {
  136. UIViewController *references = [FLEXInstancesViewController
  137. instancesTableViewControllerForInstancesReferencingObject:explorer.object
  138. ];
  139. [host.navigationController pushViewController:references animated:YES];
  140. };
  141. NSMutableArray *sections = [NSMutableArray arrayWithArray:@[
  142. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProperties],
  143. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassProperties],
  144. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindIvars],
  145. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindMethods],
  146. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassMethods],
  147. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindClassHierarchy],
  148. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindProtocols],
  149. [FLEXMetadataSection explorer:self.explorer kind:FLEXMetadataKindOther],
  150. referencesSection
  151. ]];
  152. if (self.customSection) {
  153. [sections insertObject:self.customSection atIndex:0];
  154. }
  155. if (self.descriptionSection) {
  156. [sections insertObject:self.descriptionSection atIndex:0];
  157. }
  158. return sections.copy;
  159. }
  160. - (NSArray<FLEXTableViewSection *> *)nonemptySections {
  161. return [self.allSections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
  162. return section.numberOfRows > 0;
  163. }];
  164. }
  165. - (BOOL)sectionHasActions:(NSInteger)section {
  166. return self.sections[section] == self.descriptionSection;
  167. }
  168. - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)gesture {
  169. if (gesture.state == UIGestureRecognizerStateEnded) {
  170. switch (gesture.direction) {
  171. case UISwipeGestureRecognizerDirectionRight:
  172. if (self.selectedScope > 0) {
  173. self.selectedScope -= 1;
  174. }
  175. break;
  176. case UISwipeGestureRecognizerDirectionLeft:
  177. if (self.selectedScope != self.explorer.classHierarchy.count - 1) {
  178. self.selectedScope += 1;
  179. }
  180. break;
  181. default:
  182. break;
  183. }
  184. }
  185. }
  186. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)g1 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)g2 {
  187. return [g2 isKindOfClass:[UIPanGestureRecognizer class]] && g2 != self.navigationController.interactivePopGestureRecognizer;
  188. }
  189. - (void)shareButtonPressed {
  190. [FLEXAlert makeSheet:^(FLEXAlert *make) {
  191. make.button(@"Add to Bookmarks").handler(^(NSArray<NSString *> *strings) {
  192. [FLEXBookmarkManager.bookmarks addObject:self.object];
  193. });
  194. make.button(@"Copy Description").handler(^(NSArray<NSString *> *strings) {
  195. UIPasteboard.generalPasteboard.string = self.explorer.objectDescription;
  196. });
  197. make.button(@"Copy Address").handler(^(NSArray<NSString *> *strings) {
  198. [self copyObjectAddress:nil];
  199. });
  200. make.button(@"Cancel").cancelStyle();
  201. } showFrom:self];
  202. }
  203. #pragma mark - Description
  204. - (BOOL)shouldShowDescription {
  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. self.filterText = newText;
  216. // Sections will adjust data based on this property
  217. for (FLEXTableViewSection *section in self.allSections) {
  218. section.filterText = newText;
  219. }
  220. // Check to see if class scope changed, update accordingly
  221. if (self.explorer.classScope != self.selectedScope) {
  222. self.explorer.classScope = self.selectedScope;
  223. for (FLEXTableViewSection *section in self.allSections) {
  224. [section reloadData];
  225. }
  226. }
  227. // Recalculate empty sections
  228. self.sections = [self nonemptySections];
  229. // Refresh table view
  230. if (self.isViewLoaded) {
  231. [self.tableView reloadData];
  232. }
  233. }
  234. #pragma mark - Reloading
  235. - (void)reloadData {
  236. // Reload explorer
  237. [self.explorer reloadMetadata];
  238. // Reload sections
  239. for (FLEXTableViewSection *section in self.allSections) {
  240. [section reloadData];
  241. }
  242. // Recalculate displayed sections
  243. self.sections = [self nonemptySections];
  244. // Refresh table view
  245. if (self.isViewLoaded) {
  246. [self.tableView reloadData];
  247. }
  248. }
  249. #pragma mark - UITableViewDataSource
  250. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  251. return self.sections.count;
  252. }
  253. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  254. return self.sections[section].numberOfRows;
  255. }
  256. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  257. return self.sections[section].title;
  258. }
  259. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  260. NSString *reuse = [self.sections[indexPath.section] reuseIdentifierForRow:indexPath.row];
  261. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse forIndexPath:indexPath];
  262. [self.sections[indexPath.section] configureCell:cell forRow:indexPath.row];
  263. return cell;
  264. }
  265. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  266. // For the description section, we want that nice slim/snug looking row.
  267. // Other rows use the automatic size.
  268. FLEXTableViewSection *section = self.sections[indexPath.section];
  269. if (section == self.descriptionSection) {
  270. NSAttributedString *attributedText = [[NSAttributedString alloc]
  271. initWithString:self.explorer.objectDescription
  272. attributes:@{ NSFontAttributeName : UIFont.flex_defaultTableCellFont }
  273. ];
  274. return [FLEXMultilineTableViewCell
  275. preferredHeightWithAttributedText:attributedText
  276. maxWidth:tableView.frame.size.width - tableView.separatorInset.right
  277. style:tableView.style
  278. showsAccessory:NO
  279. ];
  280. }
  281. return UITableViewAutomaticDimension;
  282. }
  283. - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  284. return [self.sections flex_mapped:^id(FLEXTableViewSection *obj, NSUInteger idx) {
  285. return @"⦁";
  286. }];
  287. }
  288. #pragma mark - UITableViewDelegate
  289. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  290. return [self.sections[indexPath.section] canSelectRow:indexPath.row];
  291. }
  292. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  293. FLEXTableViewSection *section = self.sections[indexPath.section];
  294. void (^action)(UIViewController *) = [section didSelectRowAction:indexPath.row];
  295. UIViewController *details = [section viewControllerToPushForRow:indexPath.row];
  296. if (action) {
  297. action(self);
  298. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  299. } else if (details) {
  300. [self.navigationController pushViewController:details animated:YES];
  301. } else {
  302. [NSException raise:NSInternalInconsistencyException
  303. format:@"Row is selectable but has no action or view controller"];
  304. }
  305. }
  306. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  307. return [self sectionHasActions:indexPath.section];
  308. }
  309. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  310. // Only the description section has "actions"
  311. if (self.sections[indexPath.section] == self.descriptionSection) {
  312. return action == @selector(copy:) || action == @selector(copyObjectAddress:);
  313. }
  314. return NO;
  315. }
  316. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  317. #pragma clang diagnostic push
  318. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  319. [self performSelector:action withObject:indexPath];
  320. #pragma clang diagnostic pop
  321. }
  322. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
  323. [self.sections[indexPath.section] didPressInfoButtonAction:indexPath.row](self);
  324. }
  325. #if FLEX_AT_LEAST_IOS13_SDK
  326. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  327. FLEXTableViewSection *section = self.sections[indexPath.section];
  328. NSString *title = [section menuTitleForRow:indexPath.row];
  329. NSArray<UIMenuElement *> *menuItems = [section menuItemsForRow:indexPath.row sender:self];
  330. if (menuItems.count) {
  331. return [UIContextMenuConfiguration
  332. configurationWithIdentifier:nil
  333. previewProvider:nil
  334. actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
  335. return [UIMenu menuWithTitle:title children:menuItems];
  336. }
  337. ];
  338. }
  339. return nil;
  340. }
  341. #endif
  342. #pragma mark - UIMenuController
  343. /// Prevent the search bar from trying to use us as a responder
  344. ///
  345. /// Our table cells will use the UITableViewDelegate methods
  346. /// to make sure we can perform the actions we want to
  347. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  348. return NO;
  349. }
  350. - (void)copy:(NSIndexPath *)indexPath {
  351. FLEXTableViewSection *section = self.sections[indexPath.section];
  352. UIPasteboard.generalPasteboard.string = ({
  353. NSString *copy = [section titleForRow:indexPath.row];
  354. NSString *subtitle = [section subtitleForRow:indexPath.row];
  355. if (subtitle.length) {
  356. copy = [NSString stringWithFormat:@"%@\n\n%@", copy, subtitle];
  357. }
  358. // If no string was provided, don't overwrite the pasteboard
  359. copy.length > 2 ? copy : UIPasteboard.generalPasteboard.string;
  360. });
  361. }
  362. - (void)copyObjectAddress:(NSIndexPath *)indexPath {
  363. UIPasteboard.generalPasteboard.string = [FLEXUtility addressOfObject:self.object];
  364. }
  365. @end