Browse Source

added file browser contextual menu i was overlooking and am in process of adding them in other places as well

Kevin Bradley 3 years ago
parent
commit
bb6eb25f6a

+ 68 - 3
Classes/GlobalStateExplorers/FileBrowser/FLEXFileBrowserController.m

@@ -99,6 +99,9 @@ typedef NS_ENUM(NSUInteger, FLEXFileBrowserSortAttribute) {
                                         target:self
                                         action:@selector(sortDidTouchUpInside:)]
     ]];
+#if TARGET_OS_TV
+    [self addlongPressGestureRecognizer];
+#endif
 }
 
 - (void)sortDidTouchUpInside:(UIBarButtonItem *)sortButton {
@@ -233,6 +236,62 @@ typedef NS_ENUM(NSUInteger, FLEXFileBrowserSortAttribute) {
     return cell;
 }
 
+- (void)addlongPressGestureRecognizer {
+    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
+    longPress.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeSelect]];
+    [self.tableView addGestureRecognizer:longPress];
+}
+
+- (void)longPress:(UILongPressGestureRecognizer*)gesture {
+    if ( gesture.state == UIGestureRecognizerStateEnded) {
+        
+        UITableViewCell *cell = [gesture.view valueForKey:@"_focusedCell"];
+        [self showActionForCell:cell];
+    }
+}
+
+- (void)fileBrowserOpen:(UITableViewCell *)cell {
+    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
+    NSString *fullPath = [self filePathAtIndexPath:indexPath];
+
+    BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:NULL];
+    if (stillExists) {
+        [FLEXAlert makeAlert:^(FLEXAlert *make) {
+            make.title([NSString stringWithFormat:@"Open %@?", fullPath.lastPathComponent]);
+            make.button(@"OK").handler(^(NSArray<NSString *> *strings) {
+                FXLog(@"TODO: implement opening files here!");
+            });
+            make.button(@"Cancel").cancelStyle();
+        } showFrom:self];
+    } else {
+        [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
+    }
+}
+
+- (void)showActionForCell:(UITableViewCell *)cell {
+    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
+    NSString *fullPath = [self filePathAtIndexPath:indexPath];
+    FXLog(@"showActionForCell: %@", fullPath);
+    [FLEXAlert makeAlert:^(FLEXAlert *make) {
+        make.title(@"Choose an action for this file");
+        make.button(@"Open").handler(^(NSArray<NSString *> *strings) {
+            [self fileBrowserOpen:cell];
+        });
+        make.button(@"Rename").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
+            [self fileBrowserRename:cell];
+        });
+        make.button(@"Delete").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
+            [self fileBrowserDelete:cell];
+        });
+        if ([FLEXUtility airdropAvailable]){
+            make.button(@"Share").handler(^(NSArray<NSString *> *strings) {
+                [self fileBrowserShare:cell];
+            });
+        }
+        make.button(@"Cancel").cancelStyle();
+    } showFrom:self];
+}
+
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
 
@@ -476,9 +535,15 @@ typedef NS_ENUM(NSUInteger, FLEXFileBrowserSortAttribute) {
     NSURL *filePath = [NSURL fileURLWithPath:pathString];
 #if TARGET_OS_TV
     //This only helps on jailbroken AppleTV - it will allow you to share the files over AirDrop, no share option exists otherwise.
-    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"airdropper://%@", pathString]];
-    UIApplication *application = [UIApplication sharedApplication];
-    [application openURL:url options:@{} completionHandler:nil];
+    if ([FLEXUtility airdropAvailable]){
+        [FLEXUtility airDropFile:pathString];
+        //NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"airdropper://%@", pathString]];
+        //UIApplication *application = [UIApplication sharedApplication];
+        //[application openURL:url options:@{} completionHandler:nil];
+    } else {
+        [FLEXAlert showAlert:@"Oh no" message:@"A jailbroken AppleTV is required to share files through AirDrop, sorry!" from:self];
+    }
+    
 #else
     BOOL isDirectory = NO;
     [NSFileManager.defaultManager fileExistsAtPath:pathString isDirectory:&isDirectory];

+ 0 - 1
Classes/ObjectExplorers/FLEXObjectExplorerViewController.m

@@ -143,7 +143,6 @@
     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
     longPress.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeSelect]];
     [self.tableView addGestureRecognizer:longPress];
-    
 }
 
 - (void)longPress:(UILongPressGestureRecognizer*)gesture {

+ 4 - 2
Classes/ObjectExplorers/Sections/FLEXDefaultsContentSection.m

@@ -9,7 +9,9 @@
 #import "FLEXDefaultsContentSection.h"
 #import "FLEXDefaultEditorViewController.h"
 #import "FLEXUtility.h"
-
+#if TARGET_OS_TV
+#import "fakes.h"
+#endif
 @interface FLEXDefaultsContentSection ()
 @property (nonatomic) NSUserDefaults *defaults;
 @property (nonatomic) NSArray *keys;
@@ -65,7 +67,7 @@
 #if !TARGET_OS_TV
     return UITableViewCellAccessoryDetailDisclosureButton;
 #else
-    return UITableViewCellAccessoryDisclosureIndicator;
+    return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailDisclosureButton;
 #endif
 }
 

+ 6 - 0
Classes/ObjectExplorers/Sections/FLEXMetadataSection.m

@@ -15,6 +15,9 @@
 #import "FLEXIvar.h"
 #import "NSArray+FLEX.h"
 #import "FLEXRuntime+UIKitHelpers.h"
+#if TARGET_OS_TV
+#import "fakes.h"
+#endif
 
 @interface FLEXMetadataSection ()
 @property (nonatomic, readonly) FLEXObjectExplorer *explorer;
@@ -164,6 +167,9 @@
     #if !TARGET_OS_TV
     return accessory == UITableViewCellAccessoryDisclosureIndicator ||
         accessory == UITableViewCellAccessoryDetailDisclosureButton;
+    #else
+        return accessory == UITableViewCellAccessoryDisclosureIndicator ||
+        accessory == TVTableViewCellAccessoryDetailDisclosureButton;
     #endif
     return accessory == UITableViewCellAccessoryDisclosureIndicator;
 }

+ 6 - 0
Classes/ObjectExplorers/Sections/Shortcuts/FLEXShortcutsSection.m

@@ -17,6 +17,10 @@
 #import "FLEXMethod.h"
 #import "FLEXRuntime+UIKitHelpers.h"
 #import "FLEXObjectExplorer.h"
+#if TARGET_OS_TV
+#import "fakes.h"
+#endif
+
 
 #pragma mark Private
 
@@ -188,6 +192,8 @@
     hasDisclosure |= type == UITableViewCellAccessoryDisclosureIndicator;
     #if !TARGET_OS_TV
     hasDisclosure |= type == UITableViewCellAccessoryDetailDisclosureButton;
+    #else
+    hasDisclosure |= type == TVTableViewCellAccessoryDetailDisclosureButton;
     #endif
     return hasDisclosure;
 }

+ 7 - 4
Classes/Utility/Categories/FLEXRuntime+UIKitHelpers.m

@@ -17,6 +17,9 @@
 #import "FLEXUtility.h"
 #import "NSArray+FLEX.h"
 #import "NSString+FLEX.h"
+#if TARGET_OS_TV
+#import "fakes.h"
+#endif
 
 #define FLEXObjectExplorerDefaultsImpl \
 - (FLEXObjectExplorerDefaults *)defaults { \
@@ -113,7 +116,7 @@ FLEXObjectExplorerDefaultsImpl
             #if !TARGET_OS_TV
             return UITableViewCellAccessoryDetailDisclosureButton;
             #else
-            return UITableViewCellAccessoryDisclosureIndicator; //FIXME: not a proper fix
+            return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailDisclosureButton;
             #endif
         } else {
             // Uneditable non-nil value, chevron only
@@ -125,7 +128,7 @@ FLEXObjectExplorerDefaultsImpl
             #if !TARGET_OS_TV
             return UITableViewCellAccessoryDetailButton;
             #else
-            return UITableViewCellAccessoryDisclosureIndicator; //FIXME: not a proper fix
+            return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailButton;
             #endif
         } else {
             // Non-editable nil value, neither
@@ -255,7 +258,7 @@ FLEXObjectExplorerDefaultsImpl
 #if !TARGET_OS_TV
             return UITableViewCellAccessoryDetailDisclosureButton;
 #else
-            return UITableViewCellAccessoryDisclosureIndicator; //FIXME: not a proper fix
+            return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailDisclosureButton;
 #endif
         } else {
             // Uneditable non-nil value, chevron only
@@ -267,7 +270,7 @@ FLEXObjectExplorerDefaultsImpl
 #if !TARGET_OS_TV
             return UITableViewCellAccessoryDetailButton;
 #else
-            return UITableViewCellAccessoryDisclosureIndicator; //FIXME: not a proper fix
+            return (UITableViewCellAccessoryType)TVTableViewCellAccessoryDetailButton;
 #endif
         } else {
             // Non-editable nil value, neither

+ 2 - 0
Classes/Utility/FLEXUtility.h

@@ -50,6 +50,8 @@
 #if !TARGET_OS_TV
 + (UIInterfaceOrientationMask)infoPlistSupportedInterfaceOrientationsMask;
 #else
++ (BOOL)airdropAvailable;
++ (void)airDropFile:(NSString *)file;
 + (NSUInteger)infoPlistSupportedInterfaceOrientationsMask;
 #endif
 + (UIImage *)thumbnailedImageWithMaxPixelDimension:(NSInteger)dimension fromImageData:(NSData *)data;

+ 13 - 0
Classes/Utility/FLEXUtility.m

@@ -535,4 +535,17 @@ return 0;
     }
 }
 
+#if TARGET_OS_TV
+    + (BOOL)airdropAvailable {
+        return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"airdropper://"]];
+    }
+    
+    + (void)airDropFile:(NSString *)file {
+        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"airdropper://%@", file]];
+        UIApplication *application = [UIApplication sharedApplication];
+        [application openURL:url options:@{} completionHandler:nil];
+    }
+    
+#endif
+    
 @end

+ 14 - 10
Classes/ViewHierarchy/FLEXImagePreviewViewController.m

@@ -123,17 +123,21 @@
 
 - (void)actionButtonPressed:(id)sender {
 #if TARGET_OS_TV
-    NSString *outputFile = [[FLEXImagePreviewViewController documentsFolder] stringByAppendingPathComponent:@"FLEXViewImage.png"];
-    FXLog(@"exporting view image to file: %@", outputFile);
-    NSFileManager *man = [NSFileManager defaultManager];
-    if ([man fileExistsAtPath:outputFile]){
-        [man removeItemAtPath:outputFile error:nil];
+    if ([FLEXUtility airdropAvailable]){
+         NSString *outputFile = [[FLEXImagePreviewViewController documentsFolder] stringByAppendingPathComponent:@"FLEXViewImage.png"];
+           FXLog(@"exporting view image to file: %@", outputFile);
+           NSFileManager *man = [NSFileManager defaultManager];
+           if ([man fileExistsAtPath:outputFile]){
+               [man removeItemAtPath:outputFile error:nil];
+           }
+           [UIImagePNGRepresentation(self.image) writeToFile:outputFile atomically:true];
+           NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
+           NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"airdropper://%@?sender=%@", outputFile, bundleID]];
+           [[UIApplication sharedApplication] openURL:url];
+           return;
+    } else {
+        [FLEXAlert showAlert:@"Oh no" message:@"A jailbroken AppleTV is required to share files through AirDrop, sorry!" from:self];
     }
-    [UIImagePNGRepresentation(self.image) writeToFile:outputFile atomically:true];
-    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
-    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"airdropper://%@?sender=%@", outputFile, bundleID]];
-    [[UIApplication sharedApplication] openURL:url];
-    return;
 #endif
     
     static BOOL canSaveToCameraRoll = NO, didShowWarning = NO;

+ 12 - 0
Classes/tvOS/fakes.h

@@ -40,6 +40,18 @@
  
  */
 
+typedef NS_ENUM(NSInteger, TVTableViewCellAccessoryType) {
+    TVTableViewCellAccessoryNone,
+    TVTableViewCellAccessoryDisclosureIndicator,
+    TVTableViewCellAccessoryCheckmark               = 3,
+    TVTableViewCellAccessoryChevron                 = 5,
+    TVTableViewCellAccessoryChevronOpen,
+    TVTableViewCellAccessoryChevronDisclosureButton,
+    TVTableViewCellAccessoryChevronOpenDisclosureButton,
+    TVTableViewCellAccessoryDetailDisclosureButton      = 10,
+    TVTableViewCellAccessoryDetailButton                = 12
+};
+
 @interface KBSearchButton: UIButton <UITextFieldDelegate>
 @property UISearchBar * _Nullable searchBar; //keep a reference to the search bar to add our text value to the search bar field immediately.
 - (void)triggerSearchField;