Browse Source

Convert file browser delete/rename to UIAlertController

Ryan Olson 6 years ago
parent
commit
16fbab783e

+ 2 - 4
Classes/Editing/FLEXMethodCallingViewController.m

@@ -13,6 +13,7 @@
 #import "FLEXObjectExplorerViewController.h"
 #import "FLEXArgumentInputView.h"
 #import "FLEXArgumentInputViewFactory.h"
+#import "FLEXUtility.h"
 
 @interface FLEXMethodCallingViewController ()
 
@@ -94,10 +95,7 @@
     id returnedObject = [FLEXRuntimeUtility performSelector:method_getName(self.method) onObject:self.target withArguments:arguments error:&error];
     
     if (error) {
-        NSString *title = @"Method Call Failed";
-        NSString *message = [error localizedDescription];
-        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
-        [alert show];
+        [FLEXUtility alert:@"Method Call Failed" message:[error localizedDescription] from:self];
     } else if (returnedObject) {
         // For non-nil (or void) return types, push an explorer view controller to display the returned object
         returnedObject = [FLEXRuntimeUtility potentiallyUnwrapBoxedPointer:returnedObject type:self.returnType];

+ 0 - 33
Classes/GlobalStateExplorers/FileBrowser/FLEXFileBrowserFileOperationController.h

@@ -1,33 +0,0 @@
-//
-//  FLEXFileBrowserFileOperationController.h
-//  Flipboard
-//
-//  Created by Daniel Rodriguez Troitino on 2/13/15.
-//  Copyright (c) 2015 Flipboard. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
-
-@protocol FLEXFileBrowserFileOperationController;
-
-@protocol FLEXFileBrowserFileOperationControllerDelegate <NSObject>
-
-- (void)fileOperationControllerDidDismiss:(id<FLEXFileBrowserFileOperationController>)controller;
-
-@end
-
-@protocol FLEXFileBrowserFileOperationController <NSObject>
-
-@property (nonatomic, weak) id<FLEXFileBrowserFileOperationControllerDelegate> delegate;
-
-- (instancetype)initWithPath:(NSString *)path;
-
-- (void)show;
-
-@end
-
-@interface FLEXFileBrowserFileDeleteOperationController : NSObject <FLEXFileBrowserFileOperationController>
-@end
-
-@interface FLEXFileBrowserFileRenameOperationController : NSObject <FLEXFileBrowserFileOperationController>
-@end

+ 0 - 142
Classes/GlobalStateExplorers/FileBrowser/FLEXFileBrowserFileOperationController.m

@@ -1,142 +0,0 @@
-//
-//  FLEXFileBrowserFileOperationController.m
-//  Flipboard
-//
-//  Created by Daniel Rodriguez Troitino on 2/13/15.
-//  Copyright (c) 2015 Flipboard. All rights reserved.
-//
-
-#import "FLEXFileBrowserFileOperationController.h"
-#import <UIKit/UIKit.h>
-
-@interface FLEXFileBrowserFileDeleteOperationController () <UIAlertViewDelegate>
-
-@property (nonatomic, copy, readonly) NSString *path;
-
-- (instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
-
-@end
-
-@implementation FLEXFileBrowserFileDeleteOperationController
-
-@synthesize delegate = _delegate;
-
-- (instancetype)init
-{
-    return [self initWithPath:nil];
-}
-
-- (instancetype)initWithPath:(NSString *)path
-{
-    self = [super init];
-    if (self) {
-        _path = path;
-    }
-
-    return self;
-}
-
-- (void)show
-{
-    BOOL isDirectory = NO;
-    BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:&isDirectory];
-
-    if (stillExists) {
-        UIAlertView *deleteWarning = [[UIAlertView alloc]
-                                      initWithTitle:[NSString stringWithFormat:@"Delete %@?", self.path.lastPathComponent]
-                                      message:[NSString stringWithFormat:@"The %@ will be deleted. This operation cannot be undone", isDirectory ? @"directory" : @"file"]
-                                      delegate:self
-                                      cancelButtonTitle:@"Cancel"
-                                      otherButtonTitles:@"Delete", nil];
-        [deleteWarning show];
-    } else {
-        [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
-    }
-}
-
-#pragma mark - UIAlertViewDelegate
-
-- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
-{
-    if (buttonIndex == alertView.cancelButtonIndex) {
-        // Nothing, just cancel
-    } else if (buttonIndex == alertView.firstOtherButtonIndex) {
-        [NSFileManager.defaultManager removeItemAtPath:self.path error:NULL];
-    }
-}
-
-- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
-{
-    [self.delegate fileOperationControllerDidDismiss:self];
-}
-
-@end
-
-@interface FLEXFileBrowserFileRenameOperationController () <UIAlertViewDelegate>
-
-@property (nonatomic, copy, readonly) NSString *path;
-
-- (instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
-
-@end
-
-@implementation FLEXFileBrowserFileRenameOperationController
-
-@synthesize delegate = _delegate;
-
-- (instancetype)init
-{
-    return [self initWithPath:nil];
-}
-
-- (instancetype)initWithPath:(NSString *)path
-{
-    self = [super init];
-    if (self) {
-        _path = path;
-    }
-
-    return self;
-}
-
-- (void)show
-{
-    BOOL isDirectory = NO;
-    BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:&isDirectory];
-
-    if (stillExists) {
-        UIAlertView *renameDialog = [[UIAlertView alloc]
-                                     initWithTitle:[NSString stringWithFormat:@"Rename %@?", self.path.lastPathComponent]
-                                     message:nil
-                                     delegate:self
-                                     cancelButtonTitle:@"Cancel"
-                                     otherButtonTitles:@"Rename", nil];
-        renameDialog.alertViewStyle = UIAlertViewStylePlainTextInput;
-        UITextField *textField = [renameDialog textFieldAtIndex:0];
-        textField.placeholder = @"New file name";
-        textField.text = self.path.lastPathComponent;
-        [renameDialog show];
-    } else {
-        [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
-    }
-}
-
-#pragma mark - UIAlertViewDelegate
-
-- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
-{
-    if (buttonIndex == alertView.cancelButtonIndex) {
-        // Nothing, just cancel
-    } else if (buttonIndex == alertView.firstOtherButtonIndex) {
-        NSString *newFileName = [alertView textFieldAtIndex:0].text;
-        NSString *newPath = [[self.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFileName];
-        [NSFileManager.defaultManager moveItemAtPath:self.path toPath:newPath error:NULL];
-    }
-}
-
-- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
-{
-    [self.delegate fileOperationControllerDidDismiss:self];
-}
-
-@end

+ 37 - 16
Classes/GlobalStateExplorers/FileBrowser/FLEXFileBrowserTableViewController.m

@@ -7,7 +7,6 @@
 //
 
 #import "FLEXFileBrowserTableViewController.h"
-#import "FLEXFileBrowserFileOperationController.h"
 #import "FLEXUtility.h"
 #import "FLEXWebViewController.h"
 #import "FLEXImagePreviewViewController.h"
@@ -18,7 +17,7 @@
 @interface FLEXFileBrowserTableViewCell : UITableViewCell
 @end
 
-@interface FLEXFileBrowserTableViewController () <FLEXFileBrowserFileOperationControllerDelegate, FLEXFileBrowserSearchOperationDelegate>
+@interface FLEXFileBrowserTableViewController () <FLEXFileBrowserSearchOperationDelegate>
 
 @property (nonatomic, copy) NSString *path;
 @property (nonatomic, copy) NSArray<NSString *> *childPaths;
@@ -27,7 +26,6 @@
 @property (nonatomic) NSNumber *searchPathsSize;
 @property (nonatomic) NSOperationQueue *operationQueue;
 @property (nonatomic) UIDocumentInteractionController *documentController;
-@property (nonatomic) id<FLEXFileBrowserFileOperationController> fileOperationController;
 
 @end
 
@@ -307,13 +305,6 @@
     // Since our actions are outside of that protocol, we need to manually handle the action forwarding from the cells.
 }
 
-#pragma mark - FLEXFileBrowserFileOperationControllerDelegate
-
-- (void)fileOperationControllerDidDismiss:(id<FLEXFileBrowserFileOperationController>)controller
-{
-    [self reloadDisplayedPaths];
-}
-
 - (void)openFileController:(NSString *)fullPath
 {
     UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
@@ -328,9 +319,27 @@
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
     NSString *fullPath = [self filePathAtIndexPath:indexPath];
 
-    self.fileOperationController = [[FLEXFileBrowserFileRenameOperationController alloc] initWithPath:fullPath];
-    self.fileOperationController.delegate = self;
-    [self.fileOperationController show];
+    BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:NULL];
+    if (stillExists) {
+        UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Rename %@?", fullPath.lastPathComponent]
+                                                                       message:nil
+                                                                preferredStyle:UIAlertControllerStyleAlert];
+        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
+            textField.placeholder = @"New file name";
+            textField.text = fullPath.lastPathComponent;
+        }];
+        __weak typeof(alert) weakAlert = alert;
+        [alert addAction:[UIAlertAction actionWithTitle:@"Rename" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            NSString *newFileName = weakAlert.textFields.firstObject.text;
+            NSString *newPath = [[fullPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFileName];
+            [NSFileManager.defaultManager moveItemAtPath:fullPath toPath:newPath error:NULL];
+            [self reloadDisplayedPaths];
+        }]];
+        [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
+        [self presentViewController:alert animated:YES completion:nil];
+    } else {
+        [FLEXUtility alert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
+    }
 }
 
 - (void)fileBrowserDelete:(UITableViewCell *)sender
@@ -338,9 +347,21 @@
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
     NSString *fullPath = [self filePathAtIndexPath:indexPath];
 
-    self.fileOperationController = [[FLEXFileBrowserFileDeleteOperationController alloc] initWithPath:fullPath];
-    self.fileOperationController.delegate = self;
-    [self.fileOperationController show];
+    BOOL isDirectory = NO;
+    BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
+    if (stillExists) {
+        UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Delete %@?", fullPath.lastPathComponent]
+                                                                       message:[NSString stringWithFormat:@"The %@ will be deleted. This operation cannot be undone", isDirectory ? @"directory" : @"file"]
+                                                                preferredStyle:UIAlertControllerStyleAlert];
+        [alert addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
+            [NSFileManager.defaultManager removeItemAtPath:fullPath error:NULL];
+            [self reloadDisplayedPaths];
+        }]];
+        [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
+        [self presentViewController:alert animated:YES completion:nil];
+    } else {
+        [FLEXUtility alert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
+    }
 }
 
 - (void)fileBrowserCopyPath:(UITableViewCell *)sender

+ 0 - 8
FLEX.xcodeproj/project.pbxproj

@@ -96,8 +96,6 @@
 		3A4C95121B5B21410088C3F2 /* FLEXPropertyEditorViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A4C948D1B5B21410088C3F2 /* FLEXPropertyEditorViewController.m */; };
 		3A4C951E1B5B21410088C3F2 /* FLEXClassesTableViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A4C949B1B5B21410088C3F2 /* FLEXClassesTableViewController.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		3A4C951F1B5B21410088C3F2 /* FLEXClassesTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A4C949C1B5B21410088C3F2 /* FLEXClassesTableViewController.m */; };
-		3A4C95201B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A4C949D1B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.h */; settings = {ATTRIBUTES = (Private, ); }; };
-		3A4C95211B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A4C949E1B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.m */; };
 		3A4C95221B5B21410088C3F2 /* FLEXFileBrowserSearchOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A4C949F1B5B21410088C3F2 /* FLEXFileBrowserSearchOperation.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		3A4C95231B5B21410088C3F2 /* FLEXFileBrowserSearchOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A4C94A01B5B21410088C3F2 /* FLEXFileBrowserSearchOperation.m */; };
 		3A4C95241B5B21410088C3F2 /* FLEXFileBrowserTableViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A4C94A11B5B21410088C3F2 /* FLEXFileBrowserTableViewController.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -308,8 +306,6 @@
 		3A4C948D1B5B21410088C3F2 /* FLEXPropertyEditorViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLEXPropertyEditorViewController.m; sourceTree = "<group>"; };
 		3A4C949B1B5B21410088C3F2 /* FLEXClassesTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLEXClassesTableViewController.h; sourceTree = "<group>"; };
 		3A4C949C1B5B21410088C3F2 /* FLEXClassesTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLEXClassesTableViewController.m; sourceTree = "<group>"; };
-		3A4C949D1B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLEXFileBrowserFileOperationController.h; sourceTree = "<group>"; };
-		3A4C949E1B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLEXFileBrowserFileOperationController.m; sourceTree = "<group>"; };
 		3A4C949F1B5B21410088C3F2 /* FLEXFileBrowserSearchOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLEXFileBrowserSearchOperation.h; sourceTree = "<group>"; };
 		3A4C94A01B5B21410088C3F2 /* FLEXFileBrowserSearchOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLEXFileBrowserSearchOperation.m; sourceTree = "<group>"; };
 		3A4C94A11B5B21410088C3F2 /* FLEXFileBrowserTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLEXFileBrowserTableViewController.h; sourceTree = "<group>"; };
@@ -747,8 +743,6 @@
 		C33CF16B22D664E600F9C6C0 /* FileBrowser */ = {
 			isa = PBXGroup;
 			children = (
-				3A4C949D1B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.h */,
-				3A4C949E1B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.m */,
 				3A4C949F1B5B21410088C3F2 /* FLEXFileBrowserSearchOperation.h */,
 				3A4C94A01B5B21410088C3F2 /* FLEXFileBrowserSearchOperation.m */,
 				3A4C94A11B5B21410088C3F2 /* FLEXFileBrowserTableViewController.h */,
@@ -859,7 +853,6 @@
 				779B1ED21C0C4D7C001F5E49 /* FLEXTableColumnHeader.h in Headers */,
 				3A4C94FD1B5B21410088C3F2 /* FLEXArgumentInputStructView.h in Headers */,
 				94A515141C4CA1C00063292F /* FLEXManager.h in Headers */,
-				3A4C95201B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.h in Headers */,
 				C37A0C93218BAC9600848CA7 /* FLEXObjcInternal.h in Headers */,
 				3A4C953E1B5B21410088C3F2 /* FLEXNetworkTransactionDetailTableViewController.h in Headers */,
 				3A4C95301B5B21410088C3F2 /* FLEXSystemLogMessage.h in Headers */,
@@ -1099,7 +1092,6 @@
 				3A4C95041B5B21410088C3F2 /* FLEXArgumentInputView.m in Sources */,
 				3A4C95411B5B21410088C3F2 /* FLEXNetworkTransactionTableViewCell.m in Sources */,
 				3A4C94D61B5B21410088C3F2 /* FLEXObjectExplorerViewController.m in Sources */,
-				3A4C95211B5B21410088C3F2 /* FLEXFileBrowserFileOperationController.m in Sources */,
 				3A4C94DE1B5B21410088C3F2 /* FLEXHeapEnumerator.m in Sources */,
 				3A4C95251B5B21410088C3F2 /* FLEXFileBrowserTableViewController.m in Sources */,
 				3A4C94DA1B5B21410088C3F2 /* FLEXViewControllerExplorerViewController.m in Sources */,