FLEXObjectExplorerViewController.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 "FLEXDescriptionTableViewCell.h"
  12. #import "FLEXObjectExplorerFactory.h"
  13. #import "FLEXPropertyEditorViewController.h"
  14. #import "FLEXIvarEditorViewController.h"
  15. #import "FLEXMethodCallingViewController.h"
  16. #import "FLEXInstancesTableViewController.h"
  17. #import <objc/runtime.h>
  18. // Convenience boxes to keep runtime properties, ivars, and methods in foundation collections.
  19. @interface FLEXPropertyBox : NSObject
  20. @property (nonatomic, assign) objc_property_t property;
  21. @end
  22. @implementation FLEXPropertyBox
  23. @end
  24. @interface FLEXIvarBox : NSObject
  25. @property (nonatomic, assign) Ivar ivar;
  26. @end
  27. @implementation FLEXIvarBox
  28. @end
  29. @interface FLEXMethodBox : NSObject
  30. @property (nonatomic, assign) Method method;
  31. @end
  32. @implementation FLEXMethodBox
  33. @end
  34. static const NSInteger kFLEXObjectExplorerScopeNoInheritanceIndex = 0;
  35. static const NSInteger kFLEXObjectExplorerScopeIncludeInheritanceIndex = 1;
  36. @interface FLEXObjectExplorerViewController () <UISearchBarDelegate>
  37. @property (nonatomic, strong) NSArray *properties;
  38. @property (nonatomic, strong) NSArray *inheritedProperties;
  39. @property (nonatomic, strong) NSArray *filteredProperties;
  40. @property (nonatomic, strong) NSArray *ivars;
  41. @property (nonatomic, strong) NSArray *inheritedIvars;
  42. @property (nonatomic, strong) NSArray *filteredIvars;
  43. @property (nonatomic, strong) NSArray *methods;
  44. @property (nonatomic, strong) NSArray *inheritedMethods;
  45. @property (nonatomic, strong) NSArray *filteredMethods;
  46. @property (nonatomic, strong) NSArray *classMethods;
  47. @property (nonatomic, strong) NSArray *inheritedClassMethods;
  48. @property (nonatomic, strong) NSArray *filteredClassMethods;
  49. @property (nonatomic, strong) NSArray *superclasses;
  50. @property (nonatomic, strong) NSArray *filteredSuperclasses;
  51. @property (nonatomic, strong) NSArray *cachedCustomSectionRowCookies;
  52. @property (nonatomic, strong) NSIndexSet *customSectionVisibleIndexes;
  53. @property (nonatomic, strong) UISearchBar *searchBar;
  54. @property (nonatomic, strong) NSString *filterText;
  55. @property (nonatomic, assign) BOOL includeInheritance;
  56. @end
  57. @implementation FLEXObjectExplorerViewController
  58. - (id)initWithStyle:(UITableViewStyle)style
  59. {
  60. // Force grouped style
  61. return [super initWithStyle:UITableViewStyleGrouped];
  62. }
  63. - (void)viewDidLoad
  64. {
  65. [super viewDidLoad];
  66. self.searchBar = [[UISearchBar alloc] init];
  67. self.searchBar.placeholder = [FLEXUtility searchBarPlaceholderText];
  68. self.searchBar.delegate = self;
  69. self.searchBar.showsScopeBar = YES;
  70. self.searchBar.scopeButtonTitles = @[@"No Inheritance", @"Include Inheritance"];
  71. [self.searchBar sizeToFit];
  72. self.tableView.tableHeaderView = self.searchBar;
  73. self.refreshControl = [[UIRefreshControl alloc] init];
  74. [self.refreshControl addTarget:self action:@selector(refreshControlDidRefresh:) forControlEvents:UIControlEventValueChanged];
  75. }
  76. - (void)viewWillAppear:(BOOL)animated
  77. {
  78. // Reload the entire table view rather than just the visible cells because the filtered rows
  79. // may have changed (i.e. a change in the description row that causes it to get filtered out).
  80. [self updateTableData];
  81. }
  82. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  83. {
  84. [self.searchBar endEditing:YES];
  85. }
  86. - (void)refreshControlDidRefresh:(id)sender
  87. {
  88. [self updateTableData];
  89. [self.refreshControl endRefreshing];
  90. }
  91. #pragma mark - Search
  92. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  93. {
  94. self.filterText = searchText;
  95. }
  96. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
  97. {
  98. if (selectedScope == kFLEXObjectExplorerScopeIncludeInheritanceIndex) {
  99. self.includeInheritance = YES;
  100. } else if (selectedScope == kFLEXObjectExplorerScopeNoInheritanceIndex) {
  101. self.includeInheritance = NO;
  102. }
  103. }
  104. #pragma mark - Setter overrides
  105. - (void)setObject:(id)object
  106. {
  107. _object = object;
  108. self.title = [[object class] description];
  109. [self updateTableData];
  110. }
  111. - (void)setIncludeInheritance:(BOOL)includeInheritance
  112. {
  113. if (_includeInheritance != includeInheritance) {
  114. _includeInheritance = includeInheritance;
  115. [self updateDisplayedData];
  116. }
  117. }
  118. - (void)setFilterText:(NSString *)filterText
  119. {
  120. if (_filterText != filterText || ![_filterText isEqual:filterText]) {
  121. _filterText = filterText;
  122. [self updateDisplayedData];
  123. }
  124. }
  125. #pragma mark - Reloading
  126. - (void)updateTableData
  127. {
  128. [self updateCustomData];
  129. [self updateProperties];
  130. [self updateIvars];
  131. [self updateMethods];
  132. [self updateClassMethods];
  133. [self updateSuperclasses];
  134. [self updateDisplayedData];
  135. }
  136. - (void)updateDisplayedData
  137. {
  138. [self updateFilteredCustomData];
  139. [self updateFilteredProperties];
  140. [self updateFilteredIvars];
  141. [self updateFilteredMethods];
  142. [self updateFilteredClassMethods];
  143. [self updateFilteredSuperclasses];
  144. if (self.isViewLoaded) {
  145. [self.tableView reloadData];
  146. }
  147. }
  148. - (BOOL)shouldShowDescription
  149. {
  150. BOOL showDescription = YES;
  151. // Not if it's empty or nil.
  152. NSString *descripition = [FLEXUtility safeDescriptionForObject:self.object];
  153. if (showDescription) {
  154. showDescription = [descripition length] > 0;
  155. }
  156. // Not if we have filter text that doesn't match the desctiption.
  157. if (showDescription && [self.filterText length] > 0) {
  158. showDescription = [descripition rangeOfString:self.filterText options:NSCaseInsensitiveSearch].length > 0;
  159. }
  160. return showDescription;
  161. }
  162. #pragma mark - Properties
  163. - (void)updateProperties
  164. {
  165. Class class = [self.object class];
  166. self.properties = [[self class] propertiesForClass:class];
  167. self.inheritedProperties = [[self class] inheritedPropertiesForClass:class];
  168. }
  169. + (NSArray *)propertiesForClass:(Class)class
  170. {
  171. NSMutableArray *boxedProperties = [NSMutableArray array];
  172. unsigned int propertyCount = 0;
  173. objc_property_t *propertyList = class_copyPropertyList(class, &propertyCount);
  174. if (propertyList) {
  175. for (int i = 0; i < propertyCount; i++) {
  176. FLEXPropertyBox *propertyBox = [[FLEXPropertyBox alloc] init];
  177. propertyBox.property = propertyList[i];
  178. [boxedProperties addObject:propertyBox];
  179. }
  180. free(propertyList);
  181. }
  182. return boxedProperties;
  183. }
  184. + (NSArray *)inheritedPropertiesForClass:(Class)class
  185. {
  186. NSMutableArray *inheritedProperties = [NSMutableArray array];
  187. while ((class = [class superclass])) {
  188. [inheritedProperties addObjectsFromArray:[self propertiesForClass:class]];
  189. }
  190. return inheritedProperties;
  191. }
  192. - (void)updateFilteredProperties
  193. {
  194. NSArray *candidateProperties = self.properties;
  195. if (self.includeInheritance) {
  196. candidateProperties = [candidateProperties arrayByAddingObjectsFromArray:self.inheritedProperties];
  197. }
  198. NSArray *unsortedFilteredProperties = nil;
  199. if ([self.filterText length] > 0) {
  200. NSMutableArray *mutableUnsortedFilteredProperties = [NSMutableArray array];
  201. for (FLEXPropertyBox *propertyBox in candidateProperties) {
  202. NSString *prettyName = [FLEXRuntimeUtility prettyNameForProperty:propertyBox.property];
  203. if ([prettyName rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  204. [mutableUnsortedFilteredProperties addObject:propertyBox];
  205. }
  206. }
  207. unsortedFilteredProperties = mutableUnsortedFilteredProperties;
  208. } else {
  209. unsortedFilteredProperties = candidateProperties;
  210. }
  211. self.filteredProperties = [unsortedFilteredProperties sortedArrayUsingComparator:^NSComparisonResult(FLEXPropertyBox *propertyBox1, FLEXPropertyBox *propertyBox2) {
  212. NSString *name1 = [NSString stringWithUTF8String:property_getName(propertyBox1.property)];
  213. NSString *name2 = [NSString stringWithUTF8String:property_getName(propertyBox2.property)];
  214. return [name1 caseInsensitiveCompare:name2];
  215. }];
  216. }
  217. - (NSString *)titleForPropertyAtIndex:(NSInteger)index
  218. {
  219. FLEXPropertyBox *propertyBox = [self.filteredProperties objectAtIndex:index];
  220. return [FLEXRuntimeUtility prettyNameForProperty:propertyBox.property];
  221. }
  222. - (id)valueForPropertyAtIndex:(NSInteger)index
  223. {
  224. id value = nil;
  225. if ([self canHaveInstanceState]) {
  226. FLEXPropertyBox *propertyBox = [self.filteredProperties objectAtIndex:index];
  227. value = [FLEXRuntimeUtility valueForProperty:propertyBox.property onObject:self.object];
  228. }
  229. return value;
  230. }
  231. #pragma mark - Ivars
  232. - (void)updateIvars
  233. {
  234. Class class = [self.object class];
  235. self.ivars = [[self class] ivarsForClass:class];
  236. self.inheritedIvars = [[self class] inheritedIvarsForClass:class];
  237. }
  238. + (NSArray *)ivarsForClass:(Class)class
  239. {
  240. NSMutableArray *boxedIvars = [NSMutableArray array];
  241. unsigned int ivarCount = 0;
  242. Ivar *ivarList = class_copyIvarList(class, &ivarCount);
  243. if (ivarList) {
  244. for (int i = 0; i < ivarCount; i++) {
  245. FLEXIvarBox *ivarBox = [[FLEXIvarBox alloc] init];
  246. ivarBox.ivar = ivarList[i];
  247. [boxedIvars addObject:ivarBox];
  248. }
  249. free(ivarList);
  250. }
  251. return boxedIvars;
  252. }
  253. + (NSArray *)inheritedIvarsForClass:(Class)class
  254. {
  255. NSMutableArray *inheritedIvars = [NSMutableArray array];
  256. while ((class = [class superclass])) {
  257. [inheritedIvars addObjectsFromArray:[self ivarsForClass:class]];
  258. }
  259. return inheritedIvars;
  260. }
  261. - (void)updateFilteredIvars
  262. {
  263. NSArray *candidateIvars = self.ivars;
  264. if (self.includeInheritance) {
  265. candidateIvars = [candidateIvars arrayByAddingObjectsFromArray:self.inheritedIvars];
  266. }
  267. NSArray *unsortedFilteredIvars = nil;
  268. if ([self.filterText length] > 0) {
  269. NSMutableArray *mutableUnsortedFilteredIvars = [NSMutableArray array];
  270. for (FLEXIvarBox *ivarBox in candidateIvars) {
  271. NSString *prettyName = [FLEXRuntimeUtility prettyNameForIvar:ivarBox.ivar];
  272. if ([prettyName rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  273. [mutableUnsortedFilteredIvars addObject:ivarBox];
  274. }
  275. }
  276. unsortedFilteredIvars = mutableUnsortedFilteredIvars;
  277. } else {
  278. unsortedFilteredIvars = candidateIvars;
  279. }
  280. self.filteredIvars = [unsortedFilteredIvars sortedArrayUsingComparator:^NSComparisonResult(FLEXIvarBox *ivarBox1, FLEXIvarBox *ivarBox2) {
  281. NSString *name1 = [NSString stringWithUTF8String:ivar_getName(ivarBox1.ivar)];
  282. NSString *name2 = [NSString stringWithUTF8String:ivar_getName(ivarBox2.ivar)];
  283. return [name1 caseInsensitiveCompare:name2];
  284. }];
  285. }
  286. - (NSString *)titleForIvarAtIndex:(NSInteger)index
  287. {
  288. FLEXIvarBox *ivarBox = [self.filteredIvars objectAtIndex:index];
  289. return [FLEXRuntimeUtility prettyNameForIvar:ivarBox.ivar];
  290. }
  291. - (id)valueForIvarAtIndex:(NSInteger)index
  292. {
  293. id value = nil;
  294. if (![self canHaveInstanceState]) {
  295. FLEXIvarBox *ivarBox = [self.filteredIvars objectAtIndex:index];
  296. value = [FLEXRuntimeUtility valueForIvar:ivarBox.ivar onObject:self.object];
  297. }
  298. return value;
  299. }
  300. #pragma mark - Methods
  301. - (void)updateMethods
  302. {
  303. Class class = [self.object class];
  304. self.methods = [[self class] methodsForClass:class];
  305. self.inheritedMethods = [[self class] inheritedMethodsForClass:class];
  306. }
  307. - (void)updateFilteredMethods
  308. {
  309. self.filteredMethods = [self filteredMethodsFromMethods:self.methods inheritedMethods:self.inheritedMethods areClassMethods:NO];
  310. }
  311. - (void)updateClassMethods
  312. {
  313. const char *className = [NSStringFromClass([self.object class]) UTF8String];
  314. Class metaClass = objc_getMetaClass(className);
  315. self.classMethods = [[self class] methodsForClass:metaClass];
  316. self.inheritedClassMethods = [[self class] inheritedMethodsForClass:metaClass];
  317. }
  318. - (void)updateFilteredClassMethods
  319. {
  320. self.filteredClassMethods = [self filteredMethodsFromMethods:self.classMethods inheritedMethods:self.inheritedClassMethods areClassMethods:YES];
  321. }
  322. + (NSArray *)methodsForClass:(Class)class
  323. {
  324. NSMutableArray *boxedMethods = [NSMutableArray array];
  325. unsigned int methodCount = 0;
  326. Method *methodList = class_copyMethodList(class, &methodCount);
  327. if (methodList) {
  328. for (int i = 0; i < methodCount; i++) {
  329. FLEXMethodBox *methodBox = [[FLEXMethodBox alloc] init];
  330. methodBox.method = methodList[i];
  331. [boxedMethods addObject:methodBox];
  332. }
  333. free(methodList);
  334. }
  335. return boxedMethods;
  336. }
  337. + (NSArray *)inheritedMethodsForClass:(Class)class
  338. {
  339. NSMutableArray *inheritedMethods = [NSMutableArray array];
  340. while ((class = [class superclass])) {
  341. [inheritedMethods addObjectsFromArray:[self methodsForClass:class]];
  342. }
  343. return inheritedMethods;
  344. }
  345. - (NSArray *)filteredMethodsFromMethods:(NSArray *)methods inheritedMethods:(NSArray *)inheritedMethods areClassMethods:(BOOL)areClassMethods
  346. {
  347. NSArray *candidateMethods = methods;
  348. if (self.includeInheritance) {
  349. candidateMethods = [candidateMethods arrayByAddingObjectsFromArray:inheritedMethods];
  350. }
  351. NSArray *unsortedFilteredMethods = nil;
  352. if ([self.filterText length] > 0) {
  353. NSMutableArray *mutableUnsortedFilteredMethods = [NSMutableArray array];
  354. for (FLEXMethodBox *methodBox in candidateMethods) {
  355. NSString *prettyName = [FLEXRuntimeUtility prettyNameForMethod:methodBox.method isClassMethod:areClassMethods];
  356. if ([prettyName rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  357. [mutableUnsortedFilteredMethods addObject:methodBox];
  358. }
  359. }
  360. unsortedFilteredMethods = mutableUnsortedFilteredMethods;
  361. } else {
  362. unsortedFilteredMethods = candidateMethods;
  363. }
  364. NSArray *sortedFilteredMethods = [unsortedFilteredMethods sortedArrayUsingComparator:^NSComparisonResult(FLEXMethodBox *methodBox1, FLEXMethodBox *methodBox2) {
  365. NSString *name1 = NSStringFromSelector(method_getName(methodBox1.method));
  366. NSString *name2 = NSStringFromSelector(method_getName(methodBox2.method));
  367. return [name1 caseInsensitiveCompare:name2];
  368. }];
  369. return sortedFilteredMethods;
  370. }
  371. - (NSString *)titleForMethodAtIndex:(NSInteger)index
  372. {
  373. FLEXMethodBox *methodBox = [self.filteredMethods objectAtIndex:index];
  374. return [FLEXRuntimeUtility prettyNameForMethod:methodBox.method isClassMethod:NO];
  375. }
  376. - (NSString *)titleForClassMethodAtIndex:(NSInteger)index
  377. {
  378. FLEXMethodBox *classMethodBox = [self.filteredClassMethods objectAtIndex:index];
  379. return [FLEXRuntimeUtility prettyNameForMethod:classMethodBox.method isClassMethod:YES];
  380. }
  381. #pragma mark - Superclasses
  382. + (NSArray *)superclassesForClass:(Class)class
  383. {
  384. NSMutableArray *superClasses = [NSMutableArray array];
  385. while ((class = [class superclass])) {
  386. [superClasses addObject:class];
  387. }
  388. return superClasses;
  389. }
  390. - (void)updateSuperclasses
  391. {
  392. self.superclasses = [[self class] superclassesForClass:[self.object class]];
  393. }
  394. - (void)updateFilteredSuperclasses
  395. {
  396. if ([self.filterText length] > 0) {
  397. NSMutableArray *filteredSuperclasses = [NSMutableArray array];
  398. for (Class superclass in self.superclasses) {
  399. if ([NSStringFromClass(superclass) rangeOfString:self.filterText options:NSCaseInsensitiveSearch].length > 0) {
  400. [filteredSuperclasses addObject:superclass];
  401. }
  402. }
  403. self.filteredSuperclasses = filteredSuperclasses;
  404. } else {
  405. self.filteredSuperclasses = self.superclasses;
  406. }
  407. }
  408. #pragma mark - Table View Data Helpers
  409. - (NSArray *)possibleExplorerSections
  410. {
  411. static NSArray *possibleSections = nil;
  412. static dispatch_once_t onceToken;
  413. dispatch_once(&onceToken, ^{
  414. possibleSections = @[@(FLEXObjectExplorerSectionDescription),
  415. @(FLEXObjectExplorerSectionCustom),
  416. @(FLEXObjectExplorerSectionProperties),
  417. @(FLEXObjectExplorerSectionIvars),
  418. @(FLEXObjectExplorerSectionMethods),
  419. @(FLEXObjectExplorerSectionClassMethods),
  420. @(FLEXObjectExplorerSectionSuperclasses),
  421. @(FLEXObjectExplorerSectionReferencingInstances)];
  422. });
  423. return possibleSections;
  424. }
  425. - (NSArray *)visibleExplorerSections
  426. {
  427. NSMutableArray *visibleSections = [NSMutableArray array];
  428. for (NSNumber *possibleSection in [self possibleExplorerSections]) {
  429. FLEXObjectExplorerSection explorerSection = [possibleSection unsignedIntegerValue];
  430. if ([self numberOfRowsForExplorerSection:explorerSection] > 0) {
  431. [visibleSections addObject:possibleSection];
  432. }
  433. }
  434. return visibleSections;
  435. }
  436. - (NSString *)sectionTitleWithBaseName:(NSString *)baseName totalCount:(NSUInteger)totalCount filteredCount:(NSUInteger)filteredCount
  437. {
  438. NSString *sectionTitle = nil;
  439. if (totalCount == filteredCount) {
  440. sectionTitle = [baseName stringByAppendingFormat:@" (%lu)", (unsigned long)totalCount];
  441. } else {
  442. sectionTitle = [baseName stringByAppendingFormat:@" (%lu of %lu)", (unsigned long)filteredCount, (unsigned long)totalCount];
  443. }
  444. return sectionTitle;
  445. }
  446. - (FLEXObjectExplorerSection)explorerSectionAtIndex:(NSInteger)sectionIndex
  447. {
  448. return [[[self visibleExplorerSections] objectAtIndex:sectionIndex] unsignedIntegerValue];
  449. }
  450. - (NSInteger)numberOfRowsForExplorerSection:(FLEXObjectExplorerSection)section
  451. {
  452. NSInteger numberOfRows = 0;
  453. switch (section) {
  454. case FLEXObjectExplorerSectionDescription:
  455. numberOfRows = [self shouldShowDescription] ? 1 : 0;
  456. break;
  457. case FLEXObjectExplorerSectionCustom:
  458. numberOfRows = [self.customSectionVisibleIndexes count];
  459. break;
  460. case FLEXObjectExplorerSectionProperties:
  461. numberOfRows = [self.filteredProperties count];
  462. break;
  463. case FLEXObjectExplorerSectionIvars:
  464. numberOfRows = [self.filteredIvars count];
  465. break;
  466. case FLEXObjectExplorerSectionMethods:
  467. numberOfRows = [self.filteredMethods count];
  468. break;
  469. case FLEXObjectExplorerSectionClassMethods:
  470. numberOfRows = [self.filteredClassMethods count];
  471. break;
  472. case FLEXObjectExplorerSectionSuperclasses:
  473. numberOfRows = [self.filteredSuperclasses count];
  474. break;
  475. case FLEXObjectExplorerSectionReferencingInstances:
  476. // Hide this section if there is fliter text since there's nothing searchable (only 1 row, always the same).
  477. numberOfRows = [self.filterText length] == 0 ? 1 : 0;
  478. break;
  479. }
  480. return numberOfRows;
  481. }
  482. - (NSString *)titleForRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  483. {
  484. NSString *title = nil;
  485. switch (section) {
  486. case FLEXObjectExplorerSectionDescription:
  487. title = [FLEXUtility safeDescriptionForObject:self.object];
  488. break;
  489. case FLEXObjectExplorerSectionCustom:
  490. title = [self customSectionTitleForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  491. break;
  492. case FLEXObjectExplorerSectionProperties:
  493. title = [self titleForPropertyAtIndex:row];
  494. break;
  495. case FLEXObjectExplorerSectionIvars:
  496. title = [self titleForIvarAtIndex:row];
  497. break;
  498. case FLEXObjectExplorerSectionMethods:
  499. title = [self titleForMethodAtIndex:row];
  500. break;
  501. case FLEXObjectExplorerSectionClassMethods:
  502. title = [self titleForClassMethodAtIndex:row];
  503. break;
  504. case FLEXObjectExplorerSectionSuperclasses:
  505. title = NSStringFromClass([self.filteredSuperclasses objectAtIndex:row]);
  506. break;
  507. case FLEXObjectExplorerSectionReferencingInstances:
  508. title = @"Other objects with ivars referencing this object";
  509. break;
  510. }
  511. return title;
  512. }
  513. - (NSString *)subtitleForRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  514. {
  515. NSString *subtitle = nil;
  516. switch (section) {
  517. case FLEXObjectExplorerSectionDescription:
  518. break;
  519. case FLEXObjectExplorerSectionCustom:
  520. subtitle = [self customSectionSubtitleForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  521. break;
  522. case FLEXObjectExplorerSectionProperties:
  523. subtitle = [self canHaveInstanceState] ? [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[self valueForPropertyAtIndex:row]] : nil;
  524. break;
  525. case FLEXObjectExplorerSectionIvars:
  526. subtitle = [self canHaveInstanceState] ? [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[self valueForIvarAtIndex:row]] : nil;
  527. break;
  528. case FLEXObjectExplorerSectionMethods:
  529. break;
  530. case FLEXObjectExplorerSectionClassMethods:
  531. break;
  532. case FLEXObjectExplorerSectionSuperclasses:
  533. break;
  534. case FLEXObjectExplorerSectionReferencingInstances:
  535. break;
  536. }
  537. return subtitle;
  538. }
  539. - (BOOL)canDrillInToRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  540. {
  541. BOOL canDrillIn = NO;
  542. switch (section) {
  543. case FLEXObjectExplorerSectionDescription:
  544. break;
  545. case FLEXObjectExplorerSectionCustom:
  546. canDrillIn = [self customSectionCanDrillIntoRowWithCookie:[self customSectionRowCookieForVisibleRow:row]];
  547. break;
  548. case FLEXObjectExplorerSectionProperties: {
  549. if ([self canHaveInstanceState]) {
  550. objc_property_t property = [[self.filteredProperties objectAtIndex:row] property];
  551. id currentValue = [self valueForPropertyAtIndex:row];
  552. BOOL canEdit = [FLEXPropertyEditorViewController canEditProperty:property currentValue:currentValue];
  553. BOOL canExplore = currentValue != nil;
  554. canDrillIn = canEdit || canExplore;
  555. }
  556. } break;
  557. case FLEXObjectExplorerSectionIvars: {
  558. if ([self canHaveInstanceState]) {
  559. Ivar ivar = [[self.filteredIvars objectAtIndex:row] ivar];
  560. id currentValue = [self valueForIvarAtIndex:row];
  561. BOOL canEdit = [FLEXIvarEditorViewController canEditIvar:ivar currentValue:currentValue];
  562. BOOL canExplore = currentValue != nil;
  563. canDrillIn = canEdit || canExplore;
  564. }
  565. } break;
  566. case FLEXObjectExplorerSectionMethods:
  567. canDrillIn = [self canCallInstanceMethods];
  568. break;
  569. case FLEXObjectExplorerSectionClassMethods:
  570. canDrillIn = YES;
  571. break;
  572. case FLEXObjectExplorerSectionSuperclasses:
  573. canDrillIn = YES;
  574. break;
  575. case FLEXObjectExplorerSectionReferencingInstances:
  576. canDrillIn = YES;
  577. break;
  578. }
  579. return canDrillIn;
  580. }
  581. - (NSString *)titleForExplorerSection:(FLEXObjectExplorerSection)section
  582. {
  583. NSString *title = nil;
  584. switch (section) {
  585. case FLEXObjectExplorerSectionDescription: {
  586. title = @"Description";
  587. } break;
  588. case FLEXObjectExplorerSectionCustom: {
  589. title = [self customSectionTitle];
  590. } break;
  591. case FLEXObjectExplorerSectionProperties: {
  592. NSUInteger totalCount = [self.properties count];
  593. if (self.includeInheritance) {
  594. totalCount += [self.inheritedProperties count];
  595. }
  596. title = [self sectionTitleWithBaseName:@"Properties" totalCount:totalCount filteredCount:[self.filteredProperties count]];
  597. } break;
  598. case FLEXObjectExplorerSectionIvars: {
  599. NSUInteger totalCount = [self.ivars count];
  600. if (self.includeInheritance) {
  601. totalCount += [self.inheritedIvars count];
  602. }
  603. title = [self sectionTitleWithBaseName:@"Ivars" totalCount:totalCount filteredCount:[self.filteredIvars count]];
  604. } break;
  605. case FLEXObjectExplorerSectionMethods: {
  606. NSUInteger totalCount = [self.methods count];
  607. if (self.includeInheritance) {
  608. totalCount += [self.inheritedMethods count];
  609. }
  610. title = [self sectionTitleWithBaseName:@"Methods" totalCount:totalCount filteredCount:[self.filteredMethods count]];
  611. } break;
  612. case FLEXObjectExplorerSectionClassMethods: {
  613. NSUInteger totalCount = [self.classMethods count];
  614. if (self.includeInheritance) {
  615. totalCount += [self.inheritedClassMethods count];
  616. }
  617. title = [self sectionTitleWithBaseName:@"Class Methods" totalCount:totalCount filteredCount:[self.filteredClassMethods count]];
  618. } break;
  619. case FLEXObjectExplorerSectionSuperclasses: {
  620. title = [self sectionTitleWithBaseName:@"Superclasses" totalCount:[self.superclasses count] filteredCount:[self.filteredSuperclasses count]];
  621. } break;
  622. case FLEXObjectExplorerSectionReferencingInstances: {
  623. title = @"Object Graph";
  624. } break;
  625. }
  626. return title;
  627. }
  628. - (UIViewController *)drillInViewControllerForRow:(NSUInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  629. {
  630. UIViewController *viewController = nil;
  631. switch (section) {
  632. case FLEXObjectExplorerSectionDescription:
  633. break;
  634. case FLEXObjectExplorerSectionCustom:
  635. viewController = [self customSectionDrillInViewControllerForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  636. break;
  637. case FLEXObjectExplorerSectionProperties: {
  638. objc_property_t property = [[self.filteredProperties objectAtIndex:row] property];
  639. id currentValue = [self valueForPropertyAtIndex:row];
  640. if ([FLEXPropertyEditorViewController canEditProperty:property currentValue:currentValue]) {
  641. viewController = [[FLEXPropertyEditorViewController alloc] initWithTarget:self.object property:property];
  642. } else if (currentValue) {
  643. viewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:currentValue];
  644. }
  645. } break;
  646. case FLEXObjectExplorerSectionIvars: {
  647. Ivar ivar = [[self.filteredIvars objectAtIndex:row] ivar];
  648. id currentValue = [self valueForIvarAtIndex:row];
  649. if ([FLEXIvarEditorViewController canEditIvar:ivar currentValue:currentValue]) {
  650. viewController = [[FLEXIvarEditorViewController alloc] initWithTarget:self.object ivar:ivar];
  651. } else if (currentValue) {
  652. viewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:currentValue];
  653. }
  654. } break;
  655. case FLEXObjectExplorerSectionMethods: {
  656. Method method = [[self.filteredMethods objectAtIndex:row] method];
  657. viewController = [[FLEXMethodCallingViewController alloc] initWithTarget:self.object method:method];
  658. } break;
  659. case FLEXObjectExplorerSectionClassMethods: {
  660. Method method = [[self.filteredClassMethods objectAtIndex:row] method];
  661. viewController = [[FLEXMethodCallingViewController alloc] initWithTarget:[self.object class] method:method];
  662. } break;
  663. case FLEXObjectExplorerSectionSuperclasses: {
  664. Class superclass = [self.filteredSuperclasses objectAtIndex:row];
  665. viewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:superclass];
  666. } break;
  667. case FLEXObjectExplorerSectionReferencingInstances: {
  668. viewController = [FLEXInstancesTableViewController instancesTableViewControllerForInstancesReferencingObject:self.object];
  669. } break;
  670. }
  671. return viewController;
  672. }
  673. #pragma mark - Table View Data Source
  674. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  675. {
  676. return [[self visibleExplorerSections] count];
  677. }
  678. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  679. {
  680. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:section];
  681. return [self numberOfRowsForExplorerSection:explorerSection];
  682. }
  683. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  684. {
  685. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:section];
  686. return [self titleForExplorerSection:explorerSection];
  687. }
  688. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  689. {
  690. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  691. BOOL useDescriptionCell = explorerSection == FLEXObjectExplorerSectionDescription;
  692. NSString *cellIdentifier = useDescriptionCell ? @"descriptionCell" : @"cell";
  693. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  694. if (!cell) {
  695. if (useDescriptionCell) {
  696. cell = [[FLEXDescriptionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  697. } else {
  698. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  699. UIFont *cellFont = [FLEXUtility defaultTableViewCellLabelFont];
  700. cell.textLabel.font = cellFont;
  701. cell.detailTextLabel.font = cellFont;
  702. cell.detailTextLabel.textColor = [UIColor grayColor];
  703. }
  704. }
  705. cell.textLabel.text = [self titleForRow:indexPath.row inExplorerSection:explorerSection];
  706. cell.detailTextLabel.text = [self subtitleForRow:indexPath.row inExplorerSection:explorerSection];
  707. cell.accessoryType = [self canDrillInToRow:indexPath.row inExplorerSection:explorerSection] ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
  708. return cell;
  709. }
  710. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  711. {
  712. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  713. CGFloat height = self.tableView.rowHeight;
  714. if (explorerSection == FLEXObjectExplorerSectionDescription) {
  715. NSString *text = [self titleForRow:indexPath.row inExplorerSection:explorerSection];
  716. CGFloat preferredHeight = [FLEXDescriptionTableViewCell preferredHeightWithText:text inTableViewWidth:self.tableView.frame.size.width];
  717. height = MAX(height, preferredHeight);
  718. }
  719. return height;
  720. }
  721. #pragma mark - Table View Delegate
  722. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  723. {
  724. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  725. return [self canDrillInToRow:indexPath.row inExplorerSection:explorerSection];
  726. }
  727. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  728. {
  729. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  730. UIViewController *detailViewController = [self drillInViewControllerForRow:indexPath.row inExplorerSection:explorerSection];
  731. if (detailViewController) {
  732. [self.navigationController pushViewController:detailViewController animated:YES];
  733. } else {
  734. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  735. }
  736. }
  737. #pragma mark - Custom Section
  738. - (void)updateCustomData
  739. {
  740. self.cachedCustomSectionRowCookies = [self customSectionRowCookies];
  741. }
  742. - (void)updateFilteredCustomData
  743. {
  744. NSIndexSet *filteredIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self.cachedCustomSectionRowCookies count])];
  745. if ([self.filterText length] > 0) {
  746. filteredIndexSet = [filteredIndexSet indexesPassingTest:^BOOL(NSUInteger index, BOOL *stop) {
  747. BOOL matches = NO;
  748. NSString *rowTitle = [self customSectionTitleForRowCookie:[self.cachedCustomSectionRowCookies objectAtIndex:index]];
  749. if ([rowTitle rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  750. matches = YES;
  751. }
  752. return matches;
  753. }];
  754. }
  755. self.customSectionVisibleIndexes = filteredIndexSet;
  756. }
  757. - (id)customSectionRowCookieForVisibleRow:(NSUInteger)row
  758. {
  759. return [[self.cachedCustomSectionRowCookies objectsAtIndexes:self.customSectionVisibleIndexes] objectAtIndex:row];
  760. }
  761. #pragma mark - Subclasses Can Override
  762. - (NSString *)customSectionTitle
  763. {
  764. return nil;
  765. }
  766. - (NSArray *)customSectionRowCookies
  767. {
  768. return nil;
  769. }
  770. - (NSString *)customSectionTitleForRowCookie:(id)rowCookie
  771. {
  772. return nil;
  773. }
  774. - (NSString *)customSectionSubtitleForRowCookie:(id)rowCookie
  775. {
  776. return nil;
  777. }
  778. - (BOOL)customSectionCanDrillIntoRowWithCookie:(id)rowCookie
  779. {
  780. return NO;
  781. }
  782. - (UIViewController *)customSectionDrillInViewControllerForRowCookie:(id)rowCookie
  783. {
  784. return nil;
  785. }
  786. - (BOOL)canHaveInstanceState
  787. {
  788. return YES;
  789. }
  790. - (BOOL)canCallInstanceMethods
  791. {
  792. return YES;
  793. }
  794. @end