FLEXObjectExplorerViewController.m 15 KB

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