Преглед изворни кода

Refactor FLEXClassShortcuts, add "List Subclasses"

Tanner Bennett пре 6 година
родитељ
комит
5b969b6438

+ 4 - 0
Classes/ObjectExplorers/FLEXObjectExplorerFactory.m

@@ -27,10 +27,13 @@ static NSMutableDictionary<Class, Class> *classesToRegisteredSections = nil;
     if (self == [FLEXObjectExplorerFactory class]) {
         #define ClassKey(name) (Class<NSCopying>)[name class]
         #define ClassKeyByName(str) (Class<NSCopying>)NSClassFromString(@ #str)
+        #define MetaclassKey(meta) (Class<NSCopying>)object_getClass([meta class])
         classesToRegisteredSections = [NSMutableDictionary dictionaryWithDictionary:@{
+            MetaclassKey(NSObject)     : [FLEXClassShortcuts class],
             ClassKey(NSArray)          : [FLEXCollectionContentSection class],
             ClassKey(NSSet)            : [FLEXCollectionContentSection class],
             ClassKey(NSDictionary)     : [FLEXCollectionContentSection class],
+            ClassKey(NSOrderedSet)     : [FLEXCollectionContentSection class],
             ClassKey(NSUserDefaults)   : [FLEXDefaultsContentSection class],
             ClassKey(UIViewController) : [FLEXViewControllerShortcuts class],
             ClassKey(UIView)           : [FLEXViewShortcuts class],
@@ -42,6 +45,7 @@ static NSMutableDictionary<Class, Class> *classesToRegisteredSections = nil;
         }];
         #undef ClassKey
         #undef ClassKeyByName
+        #undef MetaclassKey
     }
 }
 

+ 43 - 69
Classes/ObjectExplorers/Sections/Shortcuts/FLEXClassShortcuts.m

@@ -7,92 +7,66 @@
 //
 
 #import "FLEXClassShortcuts.h"
-#import "FLEXObjectExplorerFactory.h"
 #import "FLEXShortcut.h"
-#import "FLEXInstancesViewController.h"
-
-/// Pretty much only necessary because I want to provide
-/// a useful subtitle for the bundles of classes
-@interface FLEXBundleShortcut : NSObject <FLEXShortcut>
-@end
-#pragma mark - 
-@implementation FLEXBundleShortcut
-
-- (NSString *)titleWith:(id)object {
-    return @"Bundle";
-}
-
-- (NSString *)subtitleWith:(id)object {
-    return [self shortNameForBundlePath:[NSBundle bundleForClass:object].executablePath];
-}
-
-- (UIViewController *)viewerWith:(id)object {
-    NSBundle *bundle = [NSBundle bundleForClass:object];
-    return [FLEXObjectExplorerFactory explorerViewControllerForObject:bundle];
-}
-
-- (NSString *)shortNameForBundlePath:(NSString *)imageName {
-    NSArray<NSString *> *components = [imageName componentsSeparatedByString:@"/"];
-    if (components.count >= 2) {
-        return [NSString stringWithFormat:@"%@/%@",
-            components[components.count - 2],
-            components[components.count - 1]
-        ];
-    }
-
-    return imageName.lastPathComponent;
-}
-
-- (UITableViewCellAccessoryType)accessoryTypeWith:(id)object {
-    NSParameterAssert(object != nil);
-    return UITableViewCellAccessoryDisclosureIndicator;
-}
-
-- (NSString *)customReuseIdentifierWith:(id)object {
-    return nil;
-}
-
-- (void (^)(UIViewController *))didSelectActionWith:(id)object { 
-    return nil;
-}
-
-@end
+#import "FLEXObjectExplorerFactory.h"
+#import "FLEXObjectListViewController.h"
+#import "NSObject+Reflection.h"
 
-#pragma mark - 
 @interface FLEXClassShortcuts ()
 @property (nonatomic, readonly) Class cls;
 @end
 
 @implementation FLEXClassShortcuts
 
-#pragma mark Internal
-
-- (Class)cls {
-    return self.object;
-}
-
-
-#pragma mark Overrides
-
 + (instancetype)forObject:(Class)cls {
     // These additional rows will appear at the beginning of the shortcuts section.
     // The methods below are written in such a way that they will not interfere
     // with properties/etc being registered alongside these
-    return [self forObject:cls additionalRows:@[[FLEXBundleShortcut new], @"Live Instances"]];
+    return [self forObject:cls additionalRows:@[
+        [FLEXActionShortcut title:@"Find Live Instances" subtitle:nil
+            viewer:^UIViewController *(id obj) {
+                return [FLEXObjectListViewController
+                    instancesOfClassWithName:NSStringFromClass(obj)
+                ];
+            }
+            accessoryType:^UITableViewCellAccessoryType(id obj) {
+                return UITableViewCellAccessoryDisclosureIndicator;
+            }
+        ],
+        [FLEXActionShortcut title:@"List Subclasses" subtitle:nil
+            viewer:^UIViewController *(id obj) {
+                NSString *name = NSStringFromClass(obj);
+                return [FLEXObjectListViewController subclassesOfClassWithName:name];
+            }
+            accessoryType:^UITableViewCellAccessoryType(id view) {
+                return UITableViewCellAccessoryDisclosureIndicator;
+            }
+        ],
+        [FLEXActionShortcut title:@"Explore Bundle for Class"
+            subtitle:^NSString *(id obj) {
+                return [self shortNameForBundlePath:[NSBundle bundleForClass:obj].executablePath];
+            }
+            viewer:^UIViewController *(id obj) {
+                NSBundle *bundle = [NSBundle bundleForClass:obj];
+                return [FLEXObjectExplorerFactory explorerViewControllerForObject:bundle];
+            }
+            accessoryType:^UITableViewCellAccessoryType(id view) {
+                return UITableViewCellAccessoryDisclosureIndicator;
+            }
+        ],
+    ]];
 }
 
-- (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
-    if (row == 1) {
-        return [FLEXInstancesViewController
-            instancesTableViewControllerForClassName:NSStringFromClass(self.cls)
++ (NSString *)shortNameForBundlePath:(NSString *)imageName {
+    NSArray<NSString *> *components = [imageName componentsSeparatedByString:@"/"];
+    if (components.count >= 2) {
+        return [NSString stringWithFormat:@"%@/%@",
+            components[components.count - 2],
+            components[components.count - 1]
         ];
     }
 
-    return [super viewControllerToPushForRow:row];
-}
-
-- (UITableViewCellAccessoryType)accessoryTypeForRow:(NSInteger)row {
-    return row == 1 ? UITableViewCellAccessoryDisclosureIndicator : [super accessoryTypeForRow:row];
+    return imageName.lastPathComponent;
 }
 
 @end