Kaynağa Gözat

Merge pull request #188 from NSExceptional/master

Simplify view controller presentation code
Tanner Bennett 8 yıl önce
ebeveyn
işleme
a2bdc03684

+ 6 - 0
Classes/ExplorerInterface/FLEXExplorerViewController.h

@@ -17,6 +17,12 @@
 - (BOOL)shouldReceiveTouchAtWindowPoint:(CGPoint)pointInWindowCoordinates;
 - (BOOL)shouldReceiveTouchAtWindowPoint:(CGPoint)pointInWindowCoordinates;
 - (BOOL)wantsWindowToBecomeKey;
 - (BOOL)wantsWindowToBecomeKey;
 
 
+/// @brief Used to present (or dismiss) a modal view controller ("tool"), typically triggered by pressing a button in the toolbar.
+///
+/// If a tool is already presented, this method simply dismisses it and calls the completion block.
+/// If no tool is presented, @code future() @endcode is presented and the completion block is called.
+- (void)toggleToolWithViewControllerProvider:(UIViewController *(^)())future completion:(void(^)())completion;
+
 // Keyboard shortcut helpers
 // Keyboard shortcut helpers
 
 
 - (void)toggleSelectTool;
 - (void)toggleSelectTool;

+ 31 - 39
Classes/ExplorerInterface/FLEXExplorerViewController.m

@@ -755,7 +755,7 @@ typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
 {
 {
     // Note that we need to wait until the view controller is dismissed to calculated the frame of the outline view.
     // Note that we need to wait until the view controller is dismissed to calculated the frame of the outline view.
     // Otherwise the coordinate conversion doesn't give the correct result.
     // Otherwise the coordinate conversion doesn't give the correct result.
-    [self resignKeyAndDismissViewControllerAnimated:YES completion:^{
+    [self toggleViewsToolWithCompletion:^{
         // If the selected view is outside of the tap point array (selected from "Full Hierarchy"),
         // If the selected view is outside of the tap point array (selected from "Full Hierarchy"),
         // then clear out the tap point array and remove all the outline views.
         // then clear out the tap point array and remove all the outline views.
         if (![self.viewsAtTapPoint containsObject:selectedView]) {
         if (![self.viewsAtTapPoint containsObject:selectedView]) {
@@ -835,6 +835,15 @@ typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
     return self.previousKeyWindow != nil;
     return self.previousKeyWindow != nil;
 }
 }
 
 
+- (void)toggleToolWithViewControllerProvider:(UIViewController *(^)())future completion:(void(^)())completion
+{
+    if (self.presentedViewController) {
+        [self resignKeyAndDismissViewControllerAnimated:YES completion:completion];
+    } else {
+        [self makeKeyAndPresentViewController:future() animated:YES completion:completion];
+    }
+}
+
 #pragma mark - Keyboard Shortcut Helpers
 #pragma mark - Keyboard Shortcut Helpers
 
 
 - (void)toggleSelectTool
 - (void)toggleSelectTool
@@ -857,49 +866,32 @@ typedef NS_ENUM(NSUInteger, FLEXExplorerMode) {
 
 
 - (void)toggleViewsTool
 - (void)toggleViewsTool
 {
 {
-    BOOL viewsModalShown = [[self presentedViewController] isKindOfClass:[UINavigationController class]];
-    viewsModalShown = viewsModalShown && [[[(UINavigationController *)[self presentedViewController] viewControllers] firstObject] isKindOfClass:[FLEXHierarchyTableViewController class]];
-    if (viewsModalShown) {
-        [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
-    } else {
-        void (^presentBlock)(void) = ^{
-            NSArray<UIView *> *allViews = [self allViewsInHierarchy];
-            NSDictionary<NSValue *, NSNumber *> *depthsForViews = [self hierarchyDepthsForViews:allViews];
-            FLEXHierarchyTableViewController *hierarchyTVC = [[FLEXHierarchyTableViewController alloc] initWithViews:allViews viewsAtTap:self.viewsAtTapPoint selectedView:self.selectedView depths:depthsForViews];
-            hierarchyTVC.delegate = self;
-            UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:hierarchyTVC];
-            [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
-        };
-        
-        if (self.presentedViewController) {
-            [self resignKeyAndDismissViewControllerAnimated:NO completion:presentBlock];
-        } else {
-            presentBlock();
+    [self toggleViewsToolWithCompletion:nil];
+}
+
+- (void)toggleViewsToolWithCompletion:(void(^)())completion
+{
+    [self toggleToolWithViewControllerProvider:^UIViewController *{
+        NSArray<UIView *> *allViews = [self allViewsInHierarchy];
+        NSDictionary *depthsForViews = [self hierarchyDepthsForViews:allViews];
+        FLEXHierarchyTableViewController *hierarchyTVC = [[FLEXHierarchyTableViewController alloc] initWithViews:allViews viewsAtTap:self.viewsAtTapPoint selectedView:self.selectedView depths:depthsForViews];
+        hierarchyTVC.delegate = self;
+        return [[UINavigationController alloc] initWithRootViewController:hierarchyTVC];
+    } completion:^{
+        if (completion) {
+            completion();
         }
         }
-    }
+    }];
 }
 }
 
 
 - (void)toggleMenuTool
 - (void)toggleMenuTool
 {
 {
-    BOOL menuModalShown = [[self presentedViewController] isKindOfClass:[UINavigationController class]];
-    menuModalShown = menuModalShown && [[[(UINavigationController *)[self presentedViewController] viewControllers] firstObject] isKindOfClass:[FLEXGlobalsTableViewController class]];
-    if (menuModalShown) {
-        [self resignKeyAndDismissViewControllerAnimated:YES completion:nil];
-    } else {
-        void (^presentBlock)(void) = ^{
-            FLEXGlobalsTableViewController *globalsViewController = [[FLEXGlobalsTableViewController alloc] init];
-            globalsViewController.delegate = self;
-            [FLEXGlobalsTableViewController setApplicationWindow:[[UIApplication sharedApplication] keyWindow]];
-            UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:globalsViewController];
-            [self makeKeyAndPresentViewController:navigationController animated:YES completion:nil];
-        };
-        
-        if (self.presentedViewController) {
-            [self resignKeyAndDismissViewControllerAnimated:NO completion:presentBlock];
-        } else {
-            presentBlock();
-        }
-    }
+    [self toggleToolWithViewControllerProvider:^UIViewController *{
+        FLEXGlobalsTableViewController *globalsViewController = [[FLEXGlobalsTableViewController alloc] init];
+        globalsViewController.delegate = self;
+        [FLEXGlobalsTableViewController setApplicationWindow:[[UIApplication sharedApplication] keyWindow]];
+        return [[UINavigationController alloc] initWithRootViewController:globalsViewController];
+    } completion:nil];
 }
 }
 
 
 - (void)handleDownArrowKeyPressed
 - (void)handleDownArrowKeyPressed