FLEXObjectExplorerViewController.m 13 KB

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