Selaa lähdekoodia

Add class explorer view controller

Shortcut rows for +new, +alloc, and “Live Instances”
Reordering of the “Class Methods” section to the top.
Ryan Olson 12 vuotta sitten
vanhempi
commit
6c7b0ecb25

+ 13 - 0
Classes/Object Explorers/FLEXClassExplorerViewController.h

@@ -0,0 +1,13 @@
+//
+//  FLEXClassExplorerViewController.h
+//  Flipboard
+//
+//  Created by Ryan Olson on 6/18/14.
+//  Copyright (c) 2014 Flipboard. All rights reserved.
+//
+
+#import "FLEXObjectExplorerViewController.h"
+
+@interface FLEXClassExplorerViewController : FLEXObjectExplorerViewController
+
+@end

+ 122 - 0
Classes/Object Explorers/FLEXClassExplorerViewController.m

@@ -0,0 +1,122 @@
+//
+//  FLEXClassExplorerViewController.m
+//  Flipboard
+//
+//  Created by Ryan Olson on 6/18/14.
+//  Copyright (c) 2014 Flipboard. All rights reserved.
+//
+
+#import "FLEXClassExplorerViewController.h"
+#import "FLEXMethodCallingViewController.h"
+#import "FLEXInstancesTableViewController.h"
+
+typedef NS_ENUM(NSUInteger, FLEXClassExplorerRow) {
+    FLEXClassExplorerRowNew,
+    FLEXClassExplorerRowAlloc,
+    FLEXClassExplorerRowLiveInstances
+};
+
+@interface FLEXClassExplorerViewController ()
+
+@property (nonatomic, readonly) Class theClass;
+
+@end
+
+@implementation FLEXClassExplorerViewController
+
+- (Class)theClass
+{
+    Class theClass = Nil;
+    if (class_isMetaClass(object_getClass(self.object))) {
+        theClass = self.object;
+    }
+    return theClass;
+}
+
+#pragma mark - Superclass Overrides
+
+- (NSArray *)possibleExplorerSections
+{
+    // Move class methods to between our custom section and the properties section since
+    // we are more interested in the class sections than in the instance level sections.
+    NSMutableArray *mutableSections = [[super possibleExplorerSections] mutableCopy];
+    [mutableSections removeObject:@(FLEXObjectExplorerSectionClassMethods)];
+    [mutableSections insertObject:@(FLEXObjectExplorerSectionClassMethods) atIndex:[mutableSections indexOfObject:@(FLEXObjectExplorerSectionProperties)]];
+    return mutableSections;
+}
+
+- (NSString *)customSectionTitle
+{
+    return @"Shortcuts";
+}
+
+- (NSArray *)customSectionRowCookies
+{
+    NSMutableArray *cookies = [NSMutableArray array];
+    if ([self.theClass respondsToSelector:@selector(new)]) {
+        [cookies addObject:@(FLEXClassExplorerRowNew)];
+    }
+    if ([self.theClass respondsToSelector:@selector(alloc)]) {
+        [cookies addObject:@(FLEXClassExplorerRowAlloc)];
+    }
+    [cookies addObject:@(FLEXClassExplorerRowLiveInstances)];
+    return cookies;
+}
+
+- (NSString *)customSectionTitleForRowCookie:(id)rowCookie
+{
+    NSString *title = nil;
+    FLEXClassExplorerRow row = [rowCookie unsignedIntegerValue];
+    switch (row) {
+        case FLEXClassExplorerRowNew:
+            title = @"+ (id)new";
+            break;
+            
+        case FLEXClassExplorerRowAlloc:
+            title = @"+ (id)alloc";
+            break;
+            
+        case FLEXClassExplorerRowLiveInstances:
+            title = @"Live Instances";
+            break;
+    }
+    return title;
+}
+
+- (NSString *)customSectionSubtitleForRowCookie:(id)rowCookie
+{
+    return nil;
+}
+
+- (BOOL)customSectionCanDrillIntoRowWithCookie:(id)rowCookie
+{
+    return YES;
+}
+
+- (UIViewController *)customSectionDrillInViewControllerForRowCookie:(id)rowCookie
+{
+    UIViewController *drillInViewController = nil;
+    FLEXClassExplorerRow row = [rowCookie unsignedIntegerValue];
+    switch (row) {
+        case FLEXClassExplorerRowNew:
+            drillInViewController = [[FLEXMethodCallingViewController alloc] initWithTarget:self.theClass method:class_getClassMethod(self.theClass, @selector(new))];
+            break;
+            
+        case FLEXClassExplorerRowAlloc:
+            drillInViewController = [[FLEXMethodCallingViewController alloc] initWithTarget:self.theClass method:class_getClassMethod(self.theClass, @selector(alloc))];
+            break;
+            
+        case FLEXClassExplorerRowLiveInstances:
+            drillInViewController = [FLEXInstancesTableViewController instancesTableViewControllerForClassName:NSStringFromClass(self.theClass)];
+            break;
+    }
+    return drillInViewController;
+}
+
+- (BOOL)shouldShowDescription
+{
+    // Redundant with our title.
+    return NO;
+}
+
+@end

+ 14 - 6
Classes/Object Explorers/FLEXObjectExplorerFactory.m

@@ -15,6 +15,8 @@
 #import "FLEXViewControllerExplorerViewController.h"
 #import "FLEXViewControllerExplorerViewController.h"
 #import "FLEXViewExplorerViewController.h"
 #import "FLEXViewExplorerViewController.h"
 #import "FLEXImageExplorerViewController.h"
 #import "FLEXImageExplorerViewController.h"
+#import "FLEXClassExplorerViewController.h"
+#import <objc/runtime.h>
 
 
 @implementation FLEXObjectExplorerFactory
 @implementation FLEXObjectExplorerFactory
 
 
@@ -37,12 +39,18 @@
                                                    NSStringFromClass([UIImage class])          : [FLEXImageExplorerViewController class]};
                                                    NSStringFromClass([UIImage class])          : [FLEXImageExplorerViewController class]};
     });
     });
     
     
-    Class explorerClass = [FLEXObjectExplorerViewController class];
-    for (NSString *objectTypeString in explorerSubclassesForObjectTypeStrings) {
-        Class objectClass = NSClassFromString(objectTypeString);
-        if ([object isKindOfClass:objectClass]) {
-            explorerClass = [explorerSubclassesForObjectTypeStrings objectForKey:objectTypeString];
-            break;
+    Class explorerClass = nil;
+    BOOL objectIsClass = class_isMetaClass(object_getClass(object));
+    if (objectIsClass) {
+        explorerClass = [FLEXClassExplorerViewController class];
+    } else {
+        explorerClass = [FLEXObjectExplorerViewController class];
+        for (NSString *objectTypeString in explorerSubclassesForObjectTypeStrings) {
+            Class objectClass = NSClassFromString(objectTypeString);
+            if ([object isKindOfClass:objectClass]) {
+                explorerClass = [explorerSubclassesForObjectTypeStrings objectForKey:objectTypeString];
+                break;
+            }
         }
         }
     }
     }