Sfoglia il codice sorgente

Refactor file browser view controller

Fix #251
Tanner Bennett 7 anni fa
parent
commit
b010cdb072

+ 56 - 41
Classes/GlobalStateExplorers/FLEXFileBrowserTableViewController.m

@@ -88,6 +88,12 @@
 
 }
 
+#pragma mark - Misc
+
+- (void)alert:(NSString *)title message:(NSString *)message {
+    [[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
+}
+
 #pragma mark - FLEXFileBrowserSearchOperationDelegate
 
 - (void)fileBrowserSearchOperationResult:(NSArray<NSString *> *)searchResult size:(uint64_t)size
@@ -185,56 +191,65 @@
 
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
+    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
+
     NSString *fullPath = [self filePathAtIndexPath:indexPath];
-    NSString *subpath = [fullPath lastPathComponent];
-    NSString *pathExtension = [subpath pathExtension];
+    NSString *subpath = fullPath.lastPathComponent;
+    NSString *pathExtension = subpath.pathExtension;
 
     BOOL isDirectory = NO;
     BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
-    if (stillExists) {
-        UIViewController *drillInViewController = nil;
-        if (isDirectory) {
-            drillInViewController = [[[self class] alloc] initWithPath:fullPath];
-        } else if ([FLEXUtility isImagePathExtension:pathExtension]) {
-            UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
-            drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
-        } else {
-            // Special case keyed archives, json, and plists to get more readable data.
-            NSString *prettyString = nil;
-            if ([pathExtension isEqual:@"archive"] || [pathExtension isEqual:@"coded"]) {
-                prettyString = [[NSKeyedUnarchiver unarchiveObjectWithFile:fullPath] description];
-            } else if ([pathExtension isEqualToString:@"json"]) {
-                prettyString = [FLEXUtility prettyJSONStringFromData:[NSData dataWithContentsOfFile:fullPath]];
-            } else if ([pathExtension isEqualToString:@"plist"]) {
-                NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
-                prettyString = [[NSPropertyListSerialization propertyListWithData:fileData options:0 format:NULL error:NULL] description];
-            }
 
-            if ([prettyString length] > 0) {
-                drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
-            } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
-                drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
-            } else if ([FLEXTableListViewController supportsExtension:subpath.pathExtension]) {
-                drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
-            }
-            else {
-                NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:NULL];
-                if ([fileString length] > 0) {
-                    drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
-                }
-            }
+    if (!stillExists) {
+        [self alert:@"File Not Found" message:@"The file at the specified path no longer exists."];
+        [self reloadDisplayedPaths];
+        return;
+    }
+
+    UIViewController *drillInViewController = nil;
+    if (isDirectory) {
+        drillInViewController = [[[self class] alloc] initWithPath:fullPath];
+    } else if ([FLEXUtility isImagePathExtension:pathExtension]) {
+        UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
+        drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
+    } else {
+        NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
+        if (!fileData.length) {
+            [self alert:@"Empty File" message:@"No data returned from the file."];
+            return;
+        }
+
+        // Special case keyed archives, json, and plists to get more readable data.
+        NSString *prettyString = nil;
+        if ([pathExtension isEqual:@"archive"] || [pathExtension isEqual:@"coded"]) {
+            prettyString = [[NSKeyedUnarchiver unarchiveObjectWithData:fileData] description];
+        } else if ([pathExtension isEqualToString:@"json"]) {
+            prettyString = [FLEXUtility prettyJSONStringFromData:fileData];
+        } else if ([pathExtension isEqualToString:@"plist"]) {
+            prettyString = [[NSPropertyListSerialization propertyListWithData:fileData options:0 format:NULL error:NULL] description];
         }
 
-        if (drillInViewController) {
-            drillInViewController.title = [subpath lastPathComponent];
-            [self.navigationController pushViewController:drillInViewController animated:YES];
-        } else {
-            [self openFileController:fullPath];
-            [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
+        if (prettyString.length) {
+            drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
+        } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
+            drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
+        } else if ([FLEXTableListViewController supportsExtension:pathExtension]) {
+            drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
         }
+        else {
+            NSString *fileString = [NSString stringWithUTF8String:fileData.bytes];
+            if (fileString.length) {
+                drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
+            }
+        }
+    }
+
+    if (drillInViewController) {
+        drillInViewController.title = subpath.lastPathComponent;
+        [self.navigationController pushViewController:drillInViewController animated:YES];
     } else {
-        [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
-        [self reloadDisplayedPaths];
+        // Share the file otherwise
+        [self openFileController:fullPath];
     }
 }