Просмотр исходного кода

globalsEntryRowAction: should take precedence in FLEXGlobalsEntry

Tanner Bennett лет назад: 6
Родитель
Сommit
31446c01be

+ 9 - 1
Classes/GlobalStateExplorers/Globals/FLEXGlobalsEntry.h

@@ -47,11 +47,19 @@ typedef void (^FLEXGlobalsTableViewControllerRowAction)(FLEXGlobalsTableViewCont
 /// Previously, the concrete entries relied on "futures" for the view controller and title.
 /// With this protocol, the conforming class itself can act as a future, since the methods
 /// will not be invoked until the title and view controller / row action are needed.
+///
+/// Entries can implement \c globalsEntryViewController: to unconditionally provide a
+/// view controller, or \c globalsEntryRowAction: to conditionally provide one and
+/// perform some action (such as present an alert) if no view controller is available,
+/// or both if there is a mix of rows where some are guaranteed to work and some are not.
+/// Where both are implemented, \c globalsEntryRowAction: takes precedence; if it returns
+/// an action for the requested row, that will be used instead of \c globalsEntryViewController:
 @protocol FLEXGlobalsEntry <NSObject>
 
 + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row;
 
-// Must respond to at least one of the below
+// Must respond to at least one of the below.
+// globalsEntryRowAction: takes precedence if both are implemented.
 @optional
 
 + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row;

+ 10 - 6
Classes/GlobalStateExplorers/Globals/FLEXGlobalsEntry.m

@@ -12,17 +12,21 @@
 
 + (instancetype)entryWithEntry:(Class<FLEXGlobalsEntry>)cls row:(FLEXGlobalsRow)row
 {
+    BOOL providesVCs = [cls respondsToSelector:@selector(globalsEntryViewController:)];
+    BOOL providesActions = [cls respondsToSelector:@selector(globalsEntryRowAction:)];
     NSParameterAssert(cls);
-    NSParameterAssert(
-        [cls respondsToSelector:@selector(globalsEntryViewController:)] ||
-        [cls respondsToSelector:@selector(globalsEntryRowAction:)]
-    );
+    NSParameterAssert(providesVCs || providesActions);
 
     FLEXGlobalsEntry *entry = [self new];
     entry->_entryNameFuture = ^{ return [cls globalsEntryTitle:row]; };
 
-    if ([cls respondsToSelector:@selector(globalsEntryViewController:)]) {
-        entry->_viewControllerFuture = ^{ return [cls globalsEntryViewController:row]; };
+    if (providesVCs) {
+        id action = providesActions ? [cls globalsEntryRowAction:row] : nil;
+        if (action) {
+            entry->_rowAction = action;
+        } else {
+            entry->_viewControllerFuture = ^{ return [cls globalsEntryViewController:row]; };
+        }
     } else {
         entry->_rowAction = [cls globalsEntryRowAction:row];
     }

+ 2 - 0
Classes/ObjectExplorers/FLEXObjectExplorerFactory.m

@@ -19,6 +19,8 @@
 #import "FLEXLayerExplorerViewController.h"
 #import "FLEXColorExplorerViewController.h"
 #import "FLEXBundleExplorerViewController.h"
+#import "FLEXGlobalsTableViewController.h"
+#import "FLEXAlert.h"
 #import <objc/runtime.h>
 
 @implementation FLEXObjectExplorerFactory