FLEXObjectExplorerViewController.m 32 KB

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