Browse Source

Fix #433, refresh explorer after edits

Tanner Bennett 5 years ago
parent
commit
01e4af47e2

+ 7 - 3
Classes/Editing/FLEXDefaultEditorViewController.h

@@ -8,10 +8,14 @@
 
 #import "FLEXFieldEditorViewController.h"
 
-@interface FLEXDefaultEditorViewController : FLEXFieldEditorViewController
+NS_ASSUME_NONNULL_BEGIN
 
-- (id)initWithDefaults:(NSUserDefaults *)defaults key:(NSString *)key;
+@interface FLEXDefaultEditorViewController : FLEXVariableEditorViewController
 
-+ (BOOL)canEditDefaultWithValue:(id)currentValue;
++ (instancetype)target:(NSUserDefaults *)defaults key:(NSString *)key commitHandler:(void(^_Nullable)())onCommit;
+
++ (BOOL)canEditDefaultWithValue:(nullable id)currentValue;
 
 @end
+
+NS_ASSUME_NONNULL_END

+ 20 - 19
Classes/Editing/FLEXDefaultEditorViewController.m

@@ -15,23 +15,24 @@
 @interface FLEXDefaultEditorViewController ()
 
 @property (nonatomic, readonly) NSUserDefaults *defaults;
-@property (nonatomic) NSString *key;
+@property (nonatomic, readonly) NSString *key;
 
 @end
 
 @implementation FLEXDefaultEditorViewController
 
-- (id)initWithDefaults:(NSUserDefaults *)defaults key:(NSString *)key {
-    self = [super initWithTarget:defaults];
-    if (self) {
-        self.key = key;
-        self.title = @"Edit Default";
-    }
-    return self;
++ (instancetype)target:(NSUserDefaults *)defaults key:(NSString *)key commitHandler:(void(^_Nullable)())onCommit {
+    FLEXDefaultEditorViewController *editor = [self target:defaults data:key commitHandler:onCommit];
+    editor.title = @"Edit Default";
+    return editor;
 }
 
 - (NSUserDefaults *)defaults {
-    return [self.target isKindOfClass:[NSUserDefaults class]] ? self.target : nil;
+    return [_target isKindOfClass:[NSUserDefaults class]] ? _target : nil;
+}
+
+- (NSString *)key {
+    return _data;
 }
 
 - (void)viewDidLoad {
@@ -50,8 +51,6 @@
 }
 
 - (void)actionButtonPressed:(id)sender {
-    [super actionButtonPressed:sender];
-    
     id value = self.firstInputView.inputValue;
     if (value) {
         [self.defaults setObject:value forKey:self.key];
@@ -59,14 +58,16 @@
         [self.defaults removeObjectForKey:self.key];
     }
     [self.defaults synchronize];
-
-    self.firstInputView.inputValue = [self.defaults objectForKey:self.key];
-}
-
-- (void)getterButtonPressed:(id)sender {
-    [super getterButtonPressed:sender];
-    id returnedObject = [self.defaults objectForKey:self.key];
-    [self exploreObjectOrPopViewController:returnedObject];
+    
+    // Dismiss keyboard and handle committed changes
+    [super actionButtonPressed:sender];
+    
+    // Go back after setting, but not for switches.
+    if (sender) {
+        [self.navigationController popViewControllerAnimated:YES];
+    } else {
+        self.firstInputView.inputValue = [self.defaults objectForKey:self.key];
+    }
 }
 
 + (BOOL)canEditDefaultWithValue:(id)currentValue {

+ 2 - 2
Classes/Editing/FLEXFieldEditorViewController.h

@@ -15,9 +15,9 @@ NS_ASSUME_NONNULL_BEGIN
 @interface FLEXFieldEditorViewController : FLEXVariableEditorViewController
 
 /// @return nil if the property is readonly or if the type is unsupported
-+ (nullable instancetype)target:(id)target property:(FLEXProperty *)property;
++ (nullable instancetype)target:(id)target property:(FLEXProperty *)property commitHandler:(void(^_Nullable)())onCommit;
 /// @return nil if the ivar type is unsupported
-+ (nullable instancetype)target:(id)target ivar:(FLEXIvar *)ivar;
++ (nullable instancetype)target:(id)target ivar:(FLEXIvar *)ivar commitHandler:(void(^_Nullable)())onCommit;
 
 /// Subclasses can change the button title via the \c title property
 @property (nonatomic, readonly) UIBarButtonItem *getterButton;

+ 7 - 6
Classes/Editing/FLEXFieldEditorViewController.m

@@ -30,20 +30,20 @@
 
 #pragma mark - Initialization
 
-+ (instancetype)target:(id)target property:(FLEXProperty *)property {
++ (instancetype)target:(id)target property:(nonnull FLEXProperty *)property commitHandler:(void(^_Nullable)())onCommit {
     id value = [property getValue:target];
     if (![self canEditProperty:property onObject:target currentValue:value]) {
         return nil;
     }
 
-    FLEXFieldEditorViewController *editor = [self target:target];
+    FLEXFieldEditorViewController *editor = [self target:target data:property commitHandler:onCommit];
     editor.title = [@"Property: " stringByAppendingString:property.name];
     editor.property = property;
     return editor;
 }
 
-+ (instancetype)target:(id)target ivar:(nonnull FLEXIvar *)ivar {
-    FLEXFieldEditorViewController *editor = [self target:target];
++ (instancetype)target:(id)target ivar:(nonnull FLEXIvar *)ivar commitHandler:(void(^_Nullable)())onCommit {
+    FLEXFieldEditorViewController *editor = [self target:target data:ivar commitHandler:onCommit];
     editor.title = [@"Ivar: " stringByAppendingString:ivar.name];
     editor.ivar = ivar;
     return editor;
@@ -86,8 +86,6 @@
 }
 
 - (void)actionButtonPressed:(id)sender {
-    [super actionButtonPressed:sender];
-
     if (self.property) {
         id userInputObject = self.firstInputView.inputValue;
         NSArray *arguments = userInputObject ? @[userInputObject] : nil;
@@ -103,6 +101,9 @@
         // this currently could and would assign NSArray to NSMutableArray
         [self.ivar setValue:self.firstInputView.inputValue onObject:self.target];
     }
+    
+    // Dismiss keyboard and handle committed changes
+    [super actionButtonPressed:sender];
 
     // Go back after setting, but not for switches.
     if (sender) {

+ 9 - 5
Classes/Editing/FLEXMethodCallingViewController.m

@@ -16,7 +16,7 @@
 #import "FLEXUtility.h"
 
 @interface FLEXMethodCallingViewController ()
-@property (nonatomic) FLEXMethod *method;
+@property (nonatomic, readonly) FLEXMethod *method;
 @end
 
 @implementation FLEXMethodCallingViewController
@@ -28,9 +28,8 @@
 - (id)initWithTarget:(id)target method:(FLEXMethod *)method {
     NSParameterAssert(method.isInstanceMethod == !object_isClass(target));
 
-    self = [super initWithTarget:target];
+    self = [super initWithTarget:target data:method commitHandler:nil];
     if (self) {
-        self.method = method;
         self.title = method.isInstanceMethod ? @"Method: " : @"Class Method: ";
         self.title = [self.title stringByAppendingString:method.selectorString];
     }
@@ -72,8 +71,6 @@
 }
 
 - (void)actionButtonPressed:(id)sender {
-    [super actionButtonPressed:sender];
-
     // Gather arguments
     NSMutableArray *arguments = [NSMutableArray new];
     for (FLEXArgumentInputView *inputView in self.fieldEditorView.argumentInputViews) {
@@ -89,6 +86,9 @@
         withArguments:arguments
         error:&error
     ];
+    
+    // Dismiss keyboard and handle committed changes
+    [super actionButtonPressed:sender];
 
     // Display return value or error
     if (error) {
@@ -103,4 +103,8 @@
     }
 }
 
+- (FLEXMethod *)method {
+    return _data;
+}
+
 @end

+ 30 - 10
Classes/Editing/FLEXVariableEditorViewController.h

@@ -11,25 +11,45 @@
 @class FLEXFieldEditorView;
 @class FLEXArgumentInputView;
 
-/// Provides a screen for editing or configuring one or more variables.
-@interface FLEXVariableEditorViewController : UIViewController
+NS_ASSUME_NONNULL_BEGIN
+
+/// An abstract screen for editing or configuring one or more variables.
+/// "Target" is the target of the edit operation, and "data" is the data
+/// you want to mutate or pass to the target when the action is performed.
+/// The action may be something like calling a method, setting an ivar, etc.
+@interface FLEXVariableEditorViewController : UIViewController {
+    @protected
+    id _target;
+    _Nullable id _data;
+    void (^_Nullable _commitHandler)();
+}
+
+/// @param target The target of the operation
+/// @param data The data associated with the operation
+/// @param onCommit An action to perform when the data changes 
++ (instancetype)target:(id)target data:(nullable id)data commitHandler:(void(^_Nullable)())onCommit;
+/// @param target The target of the operation
+/// @param data The data associated with the operation
+/// @param onCommit An action to perform when the data changes 
+- (id)initWithTarget:(id)target data:(nullable id)data commitHandler:(void(^_Nullable)())onCommit;
 
-+ (instancetype)target:(id)target;
-- (id)initWithTarget:(id)target;
+@property (nonatomic, readonly) id target;
 
-// Convenience accessor since many subclasses only use one input view
-@property (nonatomic, readonly) FLEXArgumentInputView *firstInputView;
+/// Convenience accessor since many subclasses only use one input view
+@property (nonatomic, readonly, nullable) FLEXArgumentInputView *firstInputView;
 
-// For subclass use only.
-@property (nonatomic, readonly) id target;
 @property (nonatomic, readonly) FLEXFieldEditorView *fieldEditorView;
 /// Subclasses can change the button title via the button's \c title property
 @property (nonatomic, readonly) UIBarButtonItem *actionButton;
 
-- (void)actionButtonPressed:(id)sender;
+/// Subclasses should override to provide "set" functionality.
+/// The commit handler--if present--is called here.
+- (void)actionButtonPressed:(nullable id)sender;
 
 /// Pushes an explorer view controller for the given object
 /// or pops the current view controller.
-- (void)exploreObjectOrPopViewController:(id)objectOrNil;
+- (void)exploreObjectOrPopViewController:(nullable id)objectOrNil;
 
 @end
+
+NS_ASSUME_NONNULL_END

+ 9 - 5
Classes/Editing/FLEXVariableEditorViewController.m

@@ -19,21 +19,22 @@
 
 @interface FLEXVariableEditorViewController () <UIScrollViewDelegate>
 @property (nonatomic) UIScrollView *scrollView;
-@property (nonatomic) id target;
 @end
 
 @implementation FLEXVariableEditorViewController
 
 #pragma mark - Initialization
 
-+ (instancetype)target:(id)target {
-    return [[self alloc] initWithTarget:target];
++ (instancetype)target:(id)target data:(nullable id)data commitHandler:(void(^_Nullable)())onCommit {
+    return [[self alloc] initWithTarget:target data:data commitHandler:onCommit];
 }
 
-- (id)initWithTarget:(id)target {
+- (id)initWithTarget:(id)target data:(nullable id)data commitHandler:(void(^_Nullable)())onCommit {
     self = [super init];
     if (self) {
-        self.target = target;
+        _target = target;
+        _data = data;
+        _commitHandler = onCommit;
         [NSNotificationCenter.defaultCenter
             addObserver:self selector:@selector(keyboardDidShow:)
             name:UIKeyboardDidShowNotification object:nil
@@ -120,6 +121,9 @@
 - (void)actionButtonPressed:(id)sender {
     // Subclasses can override
     [self.fieldEditorView endEditing:YES];
+    if (_commitHandler) {
+        _commitHandler();
+    }
 }
 
 - (void)exploreObjectOrPopViewController:(id)objectOrNil {