|
@@ -1,35 +1,96 @@
|
|
|
//
|
|
|
-// FLEXInstancesViewController.m
|
|
|
+// FLEXObjectListViewController.m
|
|
|
// Flipboard
|
|
|
//
|
|
|
// Created by Ryan Olson on 5/28/14.
|
|
|
// Copyright (c) 2014 Flipboard. All rights reserved.
|
|
|
//
|
|
|
|
|
|
-#import "FLEXInstancesViewController.h"
|
|
|
+#import "FLEXObjectListViewController.h"
|
|
|
#import "FLEXObjectExplorerFactory.h"
|
|
|
#import "FLEXObjectExplorerViewController.h"
|
|
|
+#import "FLEXCollectionContentSection.h"
|
|
|
#import "FLEXRuntimeUtility.h"
|
|
|
#import "FLEXUtility.h"
|
|
|
#import "FLEXHeapEnumerator.h"
|
|
|
#import "FLEXObjectRef.h"
|
|
|
#import "NSString+FLEX.h"
|
|
|
+#import "NSObject+Reflection.h"
|
|
|
+#import "FLEXTableViewCell.h"
|
|
|
#import <malloc/malloc.h>
|
|
|
|
|
|
|
|
|
-@interface FLEXInstancesViewController ()
|
|
|
+@interface FLEXObjectListViewController ()
|
|
|
+
|
|
|
+@property (nonatomic) NSArray<FLEXCollectionContentSection *> *sections;
|
|
|
+@property (nonatomic, readonly) NSArray<FLEXCollectionContentSection *> *allSections;
|
|
|
|
|
|
/// Array of [[section], [section], ...]
|
|
|
/// where [section] is [["row title", instance], ["row title", instance], ...]
|
|
|
-@property (nonatomic) NSArray<FLEXObjectRef *> *instances;
|
|
|
-@property (nonatomic) NSArray<NSArray<FLEXObjectRef*>*> *sections;
|
|
|
-@property (nonatomic) NSArray<NSString *> *sectionTitles;
|
|
|
-@property (nonatomic) NSArray<NSPredicate *> *predicates;
|
|
|
-@property (nonatomic, readonly) NSInteger maxSections;
|
|
|
+@property (nonatomic) NSArray<FLEXObjectRef *> *references;
|
|
|
|
|
|
@end
|
|
|
|
|
|
-@implementation FLEXInstancesViewController
|
|
|
+@implementation FLEXObjectListViewController
|
|
|
+
|
|
|
+#pragma mark - Reference Grouping
|
|
|
+
|
|
|
++ (NSPredicate *)defaultPredicateForSection:(NSInteger)section {
|
|
|
+ // These are the types of references that we typically don't care about.
|
|
|
+ // We want this list of "object-ivar pairs" split into two sections.
|
|
|
+ BOOL(^isObserver)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
|
|
|
+ NSString *row = ref.reference;
|
|
|
+ return [row isEqualToString:@"__NSObserver object"] ||
|
|
|
+ [row isEqualToString:@"_CFXNotificationObjcObserverRegistration _object"];
|
|
|
+ };
|
|
|
+
|
|
|
+ /// These are common AutoLayout related references we also rarely care about.
|
|
|
+ BOOL(^isConstraintRelated)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
|
|
|
+ static NSSet *ignored = nil;
|
|
|
+ static dispatch_once_t onceToken;
|
|
|
+ dispatch_once(&onceToken, ^{
|
|
|
+ ignored = [NSSet setWithArray:@[
|
|
|
+ @"NSLayoutConstraint _container",
|
|
|
+ @"NSContentSizeLayoutConstraint _container",
|
|
|
+ @"NSAutoresizingMaskLayoutConstraint _container",
|
|
|
+ @"MASViewConstraint _installedView",
|
|
|
+ @"MASLayoutConstraint _container",
|
|
|
+ @"MASViewAttribute _view"
|
|
|
+ ]];
|
|
|
+ });
|
|
|
+
|
|
|
+ NSString *row = ref.reference;
|
|
|
+ return ([row hasPrefix:@"NSLayout"] && [row hasSuffix:@" _referenceItem"]) ||
|
|
|
+ ([row hasPrefix:@"NSIS"] && [row hasSuffix:@" _delegate"]) ||
|
|
|
+ ([row hasPrefix:@"_NSAutoresizingMask"] && [row hasSuffix:@" _referenceItem"]) ||
|
|
|
+ [ignored containsObject:row];
|
|
|
+ };
|
|
|
+
|
|
|
+ BOOL(^isEssential)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
|
|
|
+ return !(isObserver(ref, bindings) || isConstraintRelated(ref, bindings));
|
|
|
+ };
|
|
|
+
|
|
|
+ switch (section) {
|
|
|
+ case 0: return [NSPredicate predicateWithBlock:isEssential];
|
|
|
+ case 1: return [NSPredicate predicateWithBlock:isConstraintRelated];
|
|
|
+ case 2: return [NSPredicate predicateWithBlock:isObserver];
|
|
|
+
|
|
|
+ default: return nil;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
++ (NSArray<NSPredicate *> *)defaultPredicates {
|
|
|
+ return @[[self defaultPredicateForSection:0],
|
|
|
+ [self defaultPredicateForSection:1],
|
|
|
+ [self defaultPredicateForSection:2]];
|
|
|
+}
|
|
|
+
|
|
|
++ (NSArray<NSString *> *)defaultSectionTitles {
|
|
|
+ return @[@"", @"AutoLayout", @"Trivial"];
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#pragma mark - Initialization
|
|
|
|
|
|
- (id)initWithReferences:(NSArray<FLEXObjectRef *> *)references {
|
|
|
return [self initWithReferences:references predicates:nil sectionTitles:nil];
|
|
@@ -40,23 +101,21 @@
|
|
|
sectionTitles:(NSArray<NSString *> *)sectionTitles {
|
|
|
NSParameterAssert(predicates.count == sectionTitles.count);
|
|
|
|
|
|
- self = [super init];
|
|
|
+ self = [super initWithStyle:UITableViewStylePlain];
|
|
|
if (self) {
|
|
|
- self.instances = references;
|
|
|
- self.predicates = predicates;
|
|
|
- self.sectionTitles = sectionTitles;
|
|
|
+ self.references = references;
|
|
|
|
|
|
if (predicates.count) {
|
|
|
- [self buildSections];
|
|
|
+ [self buildSections:sectionTitles predicates:predicates];
|
|
|
} else {
|
|
|
- self.sections = @[references];
|
|
|
+ _sections = _allSections = @[[self makeSection:references title:nil]];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
-+ (instancetype)instancesTableViewControllerForClassName:(NSString *)className {
|
|
|
++ (instancetype)instancesOfClassWithName:(NSString *)className {
|
|
|
const char *classNameCString = className.UTF8String;
|
|
|
NSMutableArray *instances = [NSMutableArray array];
|
|
|
[FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id object, __unsafe_unretained Class actualClass) {
|
|
@@ -70,13 +129,25 @@
|
|
|
}
|
|
|
}
|
|
|
}];
|
|
|
+
|
|
|
NSArray<FLEXObjectRef *> *references = [FLEXObjectRef referencingAll:instances];
|
|
|
- FLEXInstancesViewController *viewController = [[self alloc] initWithReferences:references];
|
|
|
- viewController.title = [NSString stringWithFormat:@"%@ (%lu)", className, (unsigned long)instances.count];
|
|
|
- return viewController;
|
|
|
+ FLEXObjectListViewController *controller = [[self alloc] initWithReferences:references];
|
|
|
+ controller.title = [NSString stringWithFormat:@"%@ (%lu)", className, (unsigned long)instances.count];
|
|
|
+ return controller;
|
|
|
+}
|
|
|
+
|
|
|
++ (instancetype)subclassesOfClassWithName:(NSString *)className {
|
|
|
+ NSArray<Class> *classes = FLEXGetAllSubclasses(NSClassFromString(className), NO);
|
|
|
+ NSArray<FLEXObjectRef *> *references = [FLEXObjectRef referencingClasses:classes];
|
|
|
+ FLEXObjectListViewController *controller = [[self alloc] initWithReferences:references];
|
|
|
+ controller.title = [NSString stringWithFormat:@"Subclasses of %@ (%lu)",
|
|
|
+ className, (unsigned long)classes.count
|
|
|
+ ];
|
|
|
+
|
|
|
+ return controller;
|
|
|
}
|
|
|
|
|
|
-+ (instancetype)instancesTableViewControllerForInstancesReferencingObject:(id)object {
|
|
|
++ (instancetype)objectsWithReferencesToObject:(id)object {
|
|
|
static Class SwiftObjectClass = nil;
|
|
|
static dispatch_once_t onceToken;
|
|
|
dispatch_once(&onceToken, ^{
|
|
@@ -117,131 +188,120 @@
|
|
|
|
|
|
NSArray<NSPredicate *> *predicates = [self defaultPredicates];
|
|
|
NSArray<NSString *> *sectionTitles = [self defaultSectionTitles];
|
|
|
- FLEXInstancesViewController *viewController = [[self alloc] initWithReferences:instances
|
|
|
- predicates:predicates
|
|
|
- sectionTitles:sectionTitles];
|
|
|
- viewController.title = [NSString stringWithFormat:@"Referencing %@ %p", NSStringFromClass(object_getClass(object)), object];
|
|
|
+ FLEXObjectListViewController *viewController = [[self alloc]
|
|
|
+ initWithReferences:instances
|
|
|
+ predicates:predicates
|
|
|
+ sectionTitles:sectionTitles
|
|
|
+ ];
|
|
|
+ viewController.title = [NSString stringWithFormat:@"Referencing %@ %p",
|
|
|
+ NSStringFromClass(object_getClass(object)), object
|
|
|
+ ];
|
|
|
return viewController;
|
|
|
}
|
|
|
|
|
|
-+ (NSPredicate *)defaultPredicateForSection:(NSInteger)section {
|
|
|
- // These are the types of references that we typically don't care about.
|
|
|
- // We want this list of "object-ivar pairs" split into two sections.
|
|
|
- BOOL(^isObserver)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
|
|
|
- NSString *row = ref.reference;
|
|
|
- return [row isEqualToString:@"__NSObserver object"] ||
|
|
|
- [row isEqualToString:@"_CFXNotificationObjcObserverRegistration _object"];
|
|
|
- };
|
|
|
|
|
|
- /// These are common AutoLayout related references we also rarely care about.
|
|
|
- BOOL(^isConstraintRelated)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
|
|
|
- static NSSet *ignored = nil;
|
|
|
- static dispatch_once_t onceToken;
|
|
|
- dispatch_once(&onceToken, ^{
|
|
|
- ignored = [NSSet setWithArray:@[
|
|
|
- @"NSLayoutConstraint _container",
|
|
|
- @"NSContentSizeLayoutConstraint _container",
|
|
|
- @"NSAutoresizingMaskLayoutConstraint _container",
|
|
|
- @"MASViewConstraint _installedView",
|
|
|
- @"MASLayoutConstraint _container",
|
|
|
- @"MASViewAttribute _view"
|
|
|
- ]];
|
|
|
- });
|
|
|
+#pragma mark - Lifecycle
|
|
|
|
|
|
- NSString *row = ref.reference;
|
|
|
- return ([row hasPrefix:@"NSLayout"] && [row hasSuffix:@" _referenceItem"]) ||
|
|
|
- ([row hasPrefix:@"NSIS"] && [row hasSuffix:@" _delegate"]) ||
|
|
|
- ([row hasPrefix:@"_NSAutoresizingMask"] && [row hasSuffix:@" _referenceItem"]) ||
|
|
|
- [ignored containsObject:row];
|
|
|
- };
|
|
|
+- (void)viewDidLoad {
|
|
|
+ [super viewDidLoad];
|
|
|
+
|
|
|
+ self.showsSearchBar = YES;
|
|
|
+}
|
|
|
|
|
|
- BOOL(^isEssential)(FLEXObjectRef *, NSDictionary *) = ^BOOL(FLEXObjectRef *ref, NSDictionary *bindings) {
|
|
|
- return !(isObserver(ref, bindings) || isConstraintRelated(ref, bindings));
|
|
|
- };
|
|
|
|
|
|
- switch (section) {
|
|
|
- case 0: return [NSPredicate predicateWithBlock:isEssential];
|
|
|
- case 1: return [NSPredicate predicateWithBlock:isConstraintRelated];
|
|
|
- case 2: return [NSPredicate predicateWithBlock:isObserver];
|
|
|
+#pragma mark - Private
|
|
|
|
|
|
- default: return nil;
|
|
|
+- (void)buildSections:(NSArray<NSString *> *)titles predicates:(NSArray<NSPredicate *> *)predicates {
|
|
|
+ NSParameterAssert(titles.count == predicates.count);
|
|
|
+ NSParameterAssert(titles); NSParameterAssert(predicates);
|
|
|
+
|
|
|
+ _sections = _allSections = [NSArray flex_forEachUpTo:titles.count map:^id(NSUInteger i) {
|
|
|
+ NSArray *rows = [self.references filteredArrayUsingPredicate:predicates[i]];
|
|
|
+ return [self makeSection:rows title:titles[i]];
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (FLEXCollectionContentSection *)makeSection:(NSArray *)rows title:(NSString *)title {
|
|
|
+ FLEXCollectionContentSection *section = [FLEXCollectionContentSection forCollection:rows];
|
|
|
+ // We need custom filtering because we do custom cell configuration
|
|
|
+ section.customFilter = ^BOOL(NSString *filterText, FLEXObjectRef *ref) {
|
|
|
+ if (ref.summary && [ref.summary localizedCaseInsensitiveContainsString:filterText]) {
|
|
|
+ return YES;
|
|
|
+ }
|
|
|
+
|
|
|
+ return [ref.reference localizedCaseInsensitiveContainsString:filterText];
|
|
|
+ };
|
|
|
+
|
|
|
+ // Use custom title, or hide title entirely
|
|
|
+ if (title) {
|
|
|
+ section.customTitle = title;
|
|
|
+ } else {
|
|
|
+ section.hideSectionTitle = YES;
|
|
|
}
|
|
|
+
|
|
|
+ return section;
|
|
|
}
|
|
|
|
|
|
-+ (NSArray<NSPredicate *> *)defaultPredicates {
|
|
|
- return @[[self defaultPredicateForSection:0],
|
|
|
- [self defaultPredicateForSection:1],
|
|
|
- [self defaultPredicateForSection:2]];
|
|
|
+- (NSArray *)nonemptySections {
|
|
|
+ return [self.allSections flex_filtered:^BOOL(FLEXTableViewSection *section, NSUInteger idx) {
|
|
|
+ return section.numberOfRows > 0;
|
|
|
+ }];
|
|
|
}
|
|
|
|
|
|
-+ (NSArray<NSString *> *)defaultSectionTitles {
|
|
|
- return @[@"", @"AutoLayout", @"Trivial"];
|
|
|
+- (FLEXObjectRef *)referenceForIndexPath:(NSIndexPath *)ip {
|
|
|
+ return [self.sections[ip.section] objectForRow:ip.row];
|
|
|
}
|
|
|
|
|
|
-- (void)buildSections {
|
|
|
- NSInteger maxSections = self.maxSections;
|
|
|
- NSMutableArray *sections = [NSMutableArray array];
|
|
|
- for (NSInteger i = 0; i < maxSections; i++) {
|
|
|
- NSPredicate *predicate = self.predicates[i];
|
|
|
- [sections addObject:[self.instances filteredArrayUsingPredicate:predicate]];
|
|
|
+
|
|
|
+#pragma mark - Search
|
|
|
+
|
|
|
+- (void)updateSearchResults:(NSString *)newText; {
|
|
|
+ // Sections will adjust data based on this property
|
|
|
+ for (FLEXTableViewSection *section in self.allSections) {
|
|
|
+ section.filterText = newText;
|
|
|
}
|
|
|
|
|
|
- self.sections = sections;
|
|
|
-}
|
|
|
+ // Recalculate empty sections
|
|
|
+ self.sections = [self nonemptySections];
|
|
|
|
|
|
-- (NSInteger)maxSections {
|
|
|
- return self.predicates.count ?: 1;
|
|
|
+ // Refresh table view
|
|
|
+ if (self.isViewLoaded) {
|
|
|
+ [self.tableView reloadData];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
#pragma mark - Table View Data Source
|
|
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
|
- return self.maxSections;
|
|
|
+ return self.sections.count;
|
|
|
}
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
- return self.sections[section].count;
|
|
|
+ return self.sections[section].numberOfRows;
|
|
|
}
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
- static NSString *CellIdentifier = @"Cell";
|
|
|
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
|
- if (!cell) {
|
|
|
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
|
|
|
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
|
|
- UIFont *cellFont = UIFont.flex_defaultTableCellFont;
|
|
|
- cell.textLabel.font = cellFont;
|
|
|
- cell.detailTextLabel.font = cellFont;
|
|
|
- cell.detailTextLabel.textColor = UIColor.grayColor;
|
|
|
- }
|
|
|
-
|
|
|
- FLEXObjectRef *row = self.sections[indexPath.section][indexPath.row];
|
|
|
- cell.textLabel.text = row.reference;
|
|
|
- cell.detailTextLabel.text = [FLEXRuntimeUtility summaryForObject:row.object];
|
|
|
+ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXDetailCell forIndexPath:indexPath];
|
|
|
+ FLEXObjectRef *ref = [self referenceForIndexPath:indexPath];
|
|
|
+ cell.textLabel.text = ref.reference;
|
|
|
+ cell.detailTextLabel.text = ref.summary;
|
|
|
+ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
|
|
|
|
|
|
return cell;
|
|
|
}
|
|
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
|
|
|
- if (self.sectionTitles.count) {
|
|
|
- // Return nil instead of empty strings
|
|
|
- NSString *title = self.sectionTitles[section];
|
|
|
- if (title.length) {
|
|
|
- return title;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nil;
|
|
|
+ return self.sections[section].title;
|
|
|
}
|
|
|
|
|
|
|
|
|
#pragma mark - Table View Delegate
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
- id instance = self.instances[indexPath.row].object;
|
|
|
- FLEXObjectExplorerViewController *drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:instance];
|
|
|
- [self.navigationController pushViewController:drillInViewController animated:YES];
|
|
|
+ [self.navigationController pushViewController:[FLEXObjectExplorerFactory
|
|
|
+ explorerViewControllerForObject:[self referenceForIndexPath:indexPath].object
|
|
|
+ ] animated:YES];
|
|
|
}
|
|
|
|
|
|
@end
|