FLEXObjectExplorerViewController.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. typedef NS_ENUM(NSUInteger, FLEXObjectExplorerSection) {
  18. FLEXObjectExplorerSectionDescription,
  19. FLEXObjectExplorerSectionCustom,
  20. FLEXObjectExplorerSectionProperties,
  21. FLEXObjectExplorerSectionIvars,
  22. FLEXObjectExplorerSectionMethods,
  23. FLEXObjectExplorerSectionClassMethods
  24. };
  25. // Convenience boxes to keep runtime properties, ivars, and methods in foundation collections.
  26. @interface FLEXPropertyBox : NSObject
  27. @property (nonatomic, assign) objc_property_t property;
  28. @end
  29. @implementation FLEXPropertyBox
  30. @end
  31. @interface FLEXIvarBox : NSObject
  32. @property (nonatomic, assign) Ivar ivar;
  33. @end
  34. @implementation FLEXIvarBox
  35. @end
  36. @interface FLEXMethodBox : NSObject
  37. @property (nonatomic, assign) Method method;
  38. @end
  39. @implementation FLEXMethodBox
  40. @end
  41. static const NSInteger kFLEXObjectExplorerScopeNoInheritanceIndex = 0;
  42. static const NSInteger kFLEXObjectExplorerScopeIncludeInheritanceIndex = 1;
  43. @interface FLEXObjectExplorerViewController () <UISearchBarDelegate>
  44. @property (nonatomic, strong) NSArray *properties;
  45. @property (nonatomic, strong) NSArray *inheritedProperties;
  46. @property (nonatomic, strong) NSArray *filteredProperties;
  47. @property (nonatomic, strong) NSArray *ivars;
  48. @property (nonatomic, strong) NSArray *inheritedIvars;
  49. @property (nonatomic, strong) NSArray *filteredIvars;
  50. @property (nonatomic, strong) NSArray *methods;
  51. @property (nonatomic, strong) NSArray *inheritedMethods;
  52. @property (nonatomic, strong) NSArray *filteredMethods;
  53. @property (nonatomic, strong) NSArray *classMethods;
  54. @property (nonatomic, strong) NSArray *inheritedClassMethods;
  55. @property (nonatomic, strong) NSArray *filteredClassMethods;
  56. @property (nonatomic, strong) NSArray *cachedCustomSectionRowCookies;
  57. @property (nonatomic, strong) NSIndexSet *customSectionVisibleIndexes;
  58. @property (nonatomic, strong) UISearchBar *searchBar;
  59. @property (nonatomic, strong) NSString *filterText;
  60. @property (nonatomic, assign) BOOL includeInheritance;
  61. @end
  62. @implementation FLEXObjectExplorerViewController
  63. - (id)initWithStyle:(UITableViewStyle)style
  64. {
  65. // Force grouped style
  66. return [super initWithStyle:UITableViewStyleGrouped];
  67. }
  68. - (void)viewDidLoad
  69. {
  70. [super viewDidLoad];
  71. self.searchBar = [[UISearchBar alloc] init];
  72. self.searchBar.placeholder = [FLEXUtility searchBarPlaceholderText];
  73. self.searchBar.delegate = self;
  74. self.searchBar.showsScopeBar = YES;
  75. self.searchBar.scopeButtonTitles = @[@"No Inheritance", @"Include Inheritance"];
  76. [self.searchBar sizeToFit];
  77. self.tableView.tableHeaderView = self.searchBar;
  78. self.refreshControl = [[UIRefreshControl alloc] init];
  79. [self.refreshControl addTarget:self action:@selector(refreshControlDidRefresh:) forControlEvents:UIControlEventValueChanged];
  80. }
  81. - (void)viewWillAppear:(BOOL)animated
  82. {
  83. NSMutableArray *visibleIndexPaths = [NSMutableArray array];
  84. for (UITableViewCell *cell in [self.tableView visibleCells]) {
  85. [visibleIndexPaths addObject:[self.tableView indexPathForCell:cell]];
  86. }
  87. [self.tableView reloadRowsAtIndexPaths:visibleIndexPaths withRowAnimation:UITableViewRowAnimationNone];
  88. }
  89. - (BOOL)prefersStatusBarHidden
  90. {
  91. return YES;
  92. }
  93. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  94. {
  95. [self.searchBar endEditing:YES];
  96. }
  97. - (void)refreshControlDidRefresh:(id)sender
  98. {
  99. [self updateTableData];
  100. [self.refreshControl endRefreshing];
  101. }
  102. #pragma mark - Search
  103. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  104. {
  105. self.filterText = searchText;
  106. }
  107. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
  108. {
  109. if (selectedScope == kFLEXObjectExplorerScopeIncludeInheritanceIndex) {
  110. self.includeInheritance = YES;
  111. } else if (selectedScope == kFLEXObjectExplorerScopeNoInheritanceIndex) {
  112. self.includeInheritance = NO;
  113. }
  114. }
  115. #pragma mark - Setter overrides
  116. - (void)setObject:(id)object
  117. {
  118. _object = object;
  119. self.title = [[object class] description];
  120. [self updateTableData];
  121. }
  122. - (void)setIncludeInheritance:(BOOL)includeInheritance
  123. {
  124. if (_includeInheritance != includeInheritance) {
  125. _includeInheritance = includeInheritance;
  126. [self updateDisplayedData];
  127. }
  128. }
  129. - (void)setFilterText:(NSString *)filterText
  130. {
  131. if (_filterText != filterText || ![_filterText isEqual:filterText]) {
  132. _filterText = filterText;
  133. [self updateDisplayedData];
  134. }
  135. }
  136. #pragma mark - Reloading
  137. - (void)updateTableData
  138. {
  139. [self updateCustomData];
  140. [self updateProperties];
  141. [self updateIvars];
  142. [self updateMethods];
  143. [self updateClassMethods];
  144. [self updateDisplayedData];
  145. }
  146. - (void)updateDisplayedData
  147. {
  148. [self updateFilteredCustomData];
  149. [self updateFilteredProperties];
  150. [self updateFilteredIvars];
  151. [self updateFilteredMethods];
  152. [self updateFilteredClassMethods];
  153. [self.tableView reloadData];
  154. }
  155. - (BOOL)objectIsClass
  156. {
  157. return self.object && [self.object class] == self.object;
  158. }
  159. - (BOOL)shouldShowDescription
  160. {
  161. return ![self objectIsClass] && [[FLEXUtility safeDescriptionForObject:self.object] length] > 0;
  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 objectIsClass]) {
  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 objectIsClass]) {
  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 - Table View Data Helpers
  383. - (NSArray *)visibleExplorerSections
  384. {
  385. NSMutableArray *visibleSections = [NSMutableArray array];
  386. NSArray *possibleSections = @[@(FLEXObjectExplorerSectionDescription),
  387. @(FLEXObjectExplorerSectionCustom),
  388. @(FLEXObjectExplorerSectionProperties),
  389. @(FLEXObjectExplorerSectionIvars),
  390. @(FLEXObjectExplorerSectionMethods),
  391. @(FLEXObjectExplorerSectionClassMethods)];
  392. for (NSNumber *possibleSection in possibleSections) {
  393. FLEXObjectExplorerSection explorerSection = [possibleSection unsignedIntegerValue];
  394. if ([self numberOfRowsForExplorerSection:explorerSection] > 0) {
  395. [visibleSections addObject:possibleSection];
  396. }
  397. }
  398. return visibleSections;
  399. }
  400. - (NSString *)sectionTitleWithBaseName:(NSString *)baseName totalCount:(NSUInteger)totalCount filteredCount:(NSUInteger)filteredCount
  401. {
  402. NSString *sectionTitle = nil;
  403. if (totalCount == filteredCount) {
  404. sectionTitle = [baseName stringByAppendingFormat:@" (%lu)", (unsigned long)totalCount];
  405. } else {
  406. sectionTitle = [baseName stringByAppendingFormat:@" (%lu of %lu)", (unsigned long)filteredCount, (unsigned long)totalCount];
  407. }
  408. return sectionTitle;
  409. }
  410. - (FLEXObjectExplorerSection)explorerSectionAtIndex:(NSInteger)sectionIndex
  411. {
  412. return [[[self visibleExplorerSections] objectAtIndex:sectionIndex] unsignedIntegerValue];
  413. }
  414. - (NSInteger)numberOfRowsForExplorerSection:(FLEXObjectExplorerSection)section
  415. {
  416. NSInteger numberOfRows = 0;
  417. switch (section) {
  418. case FLEXObjectExplorerSectionDescription:
  419. numberOfRows = [self shouldShowDescription] ? 1 : 0;
  420. break;
  421. case FLEXObjectExplorerSectionCustom:
  422. numberOfRows = [self.customSectionVisibleIndexes count];
  423. break;
  424. case FLEXObjectExplorerSectionProperties:
  425. numberOfRows = [self.filteredProperties count];
  426. break;
  427. case FLEXObjectExplorerSectionIvars:
  428. numberOfRows = [self.filteredIvars count];
  429. break;
  430. case FLEXObjectExplorerSectionMethods:
  431. numberOfRows = [self.filteredMethods count];
  432. break;
  433. case FLEXObjectExplorerSectionClassMethods:
  434. numberOfRows = [self.filteredClassMethods count];
  435. break;
  436. }
  437. return numberOfRows;
  438. }
  439. - (NSString *)titleForRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  440. {
  441. NSString *title = nil;
  442. switch (section) {
  443. case FLEXObjectExplorerSectionDescription:
  444. title = [FLEXUtility safeDescriptionForObject:self.object];
  445. break;
  446. case FLEXObjectExplorerSectionCustom:
  447. title = [self customSectionTitleForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  448. break;
  449. case FLEXObjectExplorerSectionProperties:
  450. title = [self titleForPropertyAtIndex:row];
  451. break;
  452. case FLEXObjectExplorerSectionIvars:
  453. title = [self titleForIvarAtIndex:row];
  454. break;
  455. case FLEXObjectExplorerSectionMethods:
  456. title = [self titleForMethodAtIndex:row];
  457. break;
  458. case FLEXObjectExplorerSectionClassMethods:
  459. title = [self titleForClassMethodAtIndex:row];
  460. break;
  461. }
  462. return title;
  463. }
  464. - (NSString *)subtitleForRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  465. {
  466. // No subtitles for if we're showing a class rather than an instance of the class.
  467. if ([self objectIsClass]) {
  468. return nil;
  469. }
  470. NSString *subtitle = nil;
  471. switch (section) {
  472. case FLEXObjectExplorerSectionDescription:
  473. break;
  474. case FLEXObjectExplorerSectionCustom:
  475. subtitle = [self customSectionSubtitleForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  476. break;
  477. case FLEXObjectExplorerSectionProperties:
  478. subtitle = [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[self valueForPropertyAtIndex:row]];
  479. break;
  480. case FLEXObjectExplorerSectionIvars:
  481. subtitle = [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[self valueForIvarAtIndex:row]];
  482. break;
  483. case FLEXObjectExplorerSectionMethods:
  484. break;
  485. case FLEXObjectExplorerSectionClassMethods:
  486. break;
  487. }
  488. return subtitle;
  489. }
  490. - (BOOL)canDrillInToRow:(NSInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  491. {
  492. BOOL canDrillIn = NO;
  493. BOOL objectIsClass = [self objectIsClass];
  494. switch (section) {
  495. case FLEXObjectExplorerSectionDescription:
  496. break;
  497. case FLEXObjectExplorerSectionCustom:
  498. canDrillIn = [self customSectionCanDrillIntoRowWithCookie:[self customSectionRowCookieForVisibleRow:row]];
  499. break;
  500. case FLEXObjectExplorerSectionProperties: {
  501. if (!objectIsClass) {
  502. objc_property_t property = [[self.filteredProperties objectAtIndex:row] property];
  503. id currentValue = [self valueForPropertyAtIndex:row];
  504. BOOL canEdit = [FLEXPropertyEditorViewController canEditProperty:property currentValue:currentValue];
  505. BOOL canExplore = currentValue != nil;
  506. canDrillIn = canEdit || canExplore;
  507. }
  508. } break;
  509. case FLEXObjectExplorerSectionIvars: {
  510. if (!objectIsClass) {
  511. Ivar ivar = [[self.filteredIvars objectAtIndex:row] ivar];
  512. id currentValue = [self valueForIvarAtIndex:row];
  513. BOOL canEdit = [FLEXIvarEditorViewController canEditIvar:ivar currentValue:currentValue];
  514. BOOL canExplore = currentValue != nil;
  515. canDrillIn = canEdit || canExplore;
  516. }
  517. } break;
  518. case FLEXObjectExplorerSectionMethods:
  519. canDrillIn = !objectIsClass;
  520. break;
  521. case FLEXObjectExplorerSectionClassMethods:
  522. canDrillIn = YES;
  523. break;
  524. }
  525. return canDrillIn;
  526. }
  527. - (NSString *)titleForExplorerSection:(FLEXObjectExplorerSection)section
  528. {
  529. NSString *title = nil;
  530. switch (section) {
  531. case FLEXObjectExplorerSectionDescription: {
  532. title = @"Description";
  533. } break;
  534. case FLEXObjectExplorerSectionCustom: {
  535. title = [self customSectionTitle];
  536. } break;
  537. case FLEXObjectExplorerSectionProperties: {
  538. NSUInteger totalCount = [self.properties count];
  539. if (self.includeInheritance) {
  540. totalCount += [self.inheritedProperties count];
  541. }
  542. title = [self sectionTitleWithBaseName:@"Properties" totalCount:totalCount filteredCount:[self.filteredProperties count]];
  543. } break;
  544. case FLEXObjectExplorerSectionIvars: {
  545. NSUInteger totalCount = [self.ivars count];
  546. if (self.includeInheritance) {
  547. totalCount += [self.inheritedIvars count];
  548. }
  549. title = [self sectionTitleWithBaseName:@"Ivars" totalCount:totalCount filteredCount:[self.filteredIvars count]];
  550. } break;
  551. case FLEXObjectExplorerSectionMethods: {
  552. NSUInteger totalCount = [self.methods count];
  553. if (self.includeInheritance) {
  554. totalCount += [self.inheritedMethods count];
  555. }
  556. title = [self sectionTitleWithBaseName:@"Methods" totalCount:totalCount filteredCount:[self.filteredMethods count]];
  557. } break;
  558. case FLEXObjectExplorerSectionClassMethods: {
  559. NSUInteger totalCount = [self.classMethods count];
  560. if (self.includeInheritance) {
  561. totalCount += [self.inheritedClassMethods count];
  562. }
  563. title = [self sectionTitleWithBaseName:@"Class Methods" totalCount:totalCount filteredCount:[self.filteredClassMethods count]];
  564. } break;
  565. }
  566. return title;
  567. }
  568. - (UIViewController *)drillInViewControllerForRow:(NSUInteger)row inExplorerSection:(FLEXObjectExplorerSection)section
  569. {
  570. UIViewController *viewController = nil;
  571. switch (section) {
  572. case FLEXObjectExplorerSectionDescription:
  573. break;
  574. case FLEXObjectExplorerSectionCustom:
  575. viewController = [self customSectionDrillInViewControllerForRowCookie:[self customSectionRowCookieForVisibleRow:row]];
  576. break;
  577. case FLEXObjectExplorerSectionProperties: {
  578. objc_property_t property = [[self.filteredProperties objectAtIndex:row] property];
  579. id currentValue = [self valueForPropertyAtIndex:row];
  580. if ([FLEXPropertyEditorViewController canEditProperty:property currentValue:currentValue]) {
  581. viewController = [[FLEXPropertyEditorViewController alloc] initWithTarget:self.object property:property];
  582. } else if (currentValue) {
  583. viewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:currentValue];
  584. }
  585. } break;
  586. case FLEXObjectExplorerSectionIvars: {
  587. Ivar ivar = [[self.filteredIvars objectAtIndex:row] ivar];
  588. id currentValue = [self valueForIvarAtIndex:row];
  589. if ([FLEXIvarEditorViewController canEditIvar:ivar currentValue:currentValue]) {
  590. viewController = [[FLEXIvarEditorViewController alloc] initWithTarget:self.object ivar:ivar];
  591. } else if (currentValue) {
  592. viewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:currentValue];
  593. }
  594. } break;
  595. case FLEXObjectExplorerSectionMethods: {
  596. Method method = [[self.filteredMethods objectAtIndex:row] method];
  597. viewController = [[FLEXMethodCallingViewController alloc] initWithTarget:self.object method:method];
  598. } break;
  599. case FLEXObjectExplorerSectionClassMethods: {
  600. Method method = [[self.filteredClassMethods objectAtIndex:row] method];
  601. viewController = [[FLEXMethodCallingViewController alloc] initWithTarget:[self.object class] method:method];
  602. } break;
  603. }
  604. return viewController;
  605. }
  606. #pragma mark - Table View Data Source
  607. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  608. {
  609. return [[self visibleExplorerSections] count];
  610. }
  611. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  612. {
  613. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:section];
  614. return [self numberOfRowsForExplorerSection:explorerSection];
  615. }
  616. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  617. {
  618. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:section];
  619. return [self titleForExplorerSection:explorerSection];
  620. }
  621. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  622. {
  623. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  624. BOOL useDescriptionCell = explorerSection == FLEXObjectExplorerSectionDescription;
  625. NSString *cellIdentifier = useDescriptionCell ? @"descriptionCell" : @"cell";
  626. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  627. if (!cell) {
  628. if (useDescriptionCell) {
  629. cell = [[FLEXDescriptionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  630. } else {
  631. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  632. UIFont *cellFont = [FLEXUtility defaultTableViewCellLabelFont];
  633. cell.textLabel.font = cellFont;
  634. cell.detailTextLabel.font = cellFont;
  635. cell.detailTextLabel.textColor = [UIColor grayColor];
  636. }
  637. }
  638. cell.textLabel.text = [self titleForRow:indexPath.row inExplorerSection:explorerSection];
  639. cell.detailTextLabel.text = [self subtitleForRow:indexPath.row inExplorerSection:explorerSection];
  640. cell.accessoryType = [self canDrillInToRow:indexPath.row inExplorerSection:explorerSection] ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
  641. return cell;
  642. }
  643. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  644. {
  645. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  646. CGFloat height = self.tableView.rowHeight;
  647. if (explorerSection == FLEXObjectExplorerSectionDescription) {
  648. NSString *text = [self titleForRow:indexPath.row inExplorerSection:explorerSection];
  649. CGFloat preferredHeight = [FLEXDescriptionTableViewCell preferredHeightWithText:text inTableViewWidth:self.tableView.frame.size.width];
  650. height = MAX(height, preferredHeight);
  651. }
  652. return height;
  653. }
  654. #pragma mark - Table View Delegate
  655. - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  656. {
  657. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  658. return [self canDrillInToRow:indexPath.row inExplorerSection:explorerSection];
  659. }
  660. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  661. {
  662. FLEXObjectExplorerSection explorerSection = [self explorerSectionAtIndex:indexPath.section];
  663. UIViewController *detailViewController = [self drillInViewControllerForRow:indexPath.row inExplorerSection:explorerSection];
  664. if (detailViewController) {
  665. [self.navigationController pushViewController:detailViewController animated:YES];
  666. } else {
  667. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  668. }
  669. }
  670. #pragma mark - Custom Section
  671. - (void)updateCustomData
  672. {
  673. self.cachedCustomSectionRowCookies = [self customSectionRowCookies];
  674. }
  675. - (void)updateFilteredCustomData
  676. {
  677. NSIndexSet *filteredIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self.cachedCustomSectionRowCookies count])];
  678. if ([self.filterText length] > 0) {
  679. filteredIndexSet = [filteredIndexSet indexesPassingTest:^BOOL(NSUInteger index, BOOL *stop) {
  680. BOOL matches = NO;
  681. NSString *rowTitle = [self customSectionTitleForRowCookie:[self.cachedCustomSectionRowCookies objectAtIndex:index]];
  682. if ([rowTitle rangeOfString:self.filterText options:NSCaseInsensitiveSearch].location != NSNotFound) {
  683. matches = YES;
  684. }
  685. return matches;
  686. }];
  687. }
  688. self.customSectionVisibleIndexes = filteredIndexSet;
  689. }
  690. - (id)customSectionRowCookieForVisibleRow:(NSUInteger)row
  691. {
  692. return [[self.cachedCustomSectionRowCookies objectsAtIndexes:self.customSectionVisibleIndexes] objectAtIndex:row];
  693. }
  694. #pragma mark - Subclasses Can Override
  695. - (NSString *)customSectionTitle
  696. {
  697. return nil;
  698. }
  699. - (NSArray *)customSectionRowCookies
  700. {
  701. return nil;
  702. }
  703. - (NSString *)customSectionTitleForRowCookie:(id)rowCookie
  704. {
  705. return nil;
  706. }
  707. - (NSString *)customSectionSubtitleForRowCookie:(id)rowCookie
  708. {
  709. return nil;
  710. }
  711. - (BOOL)customSectionCanDrillIntoRowWithCookie:(id)rowCookie
  712. {
  713. return NO;
  714. }
  715. - (UIViewController *)customSectionDrillInViewControllerForRowCookie:(id)rowCookie
  716. {
  717. return nil;
  718. }
  719. @end