FLEXFileBrowserController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. //
  2. // FLEXFileBrowserController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/9/14.
  6. //
  7. //
  8. #import "FLEXFileBrowserController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXWebViewController.h"
  11. #import "FLEXImagePreviewViewController.h"
  12. #import "FLEXTableListViewController.h"
  13. #import "FLEXObjectExplorerFactory.h"
  14. #import "FLEXObjectExplorerViewController.h"
  15. #import <mach-o/loader.h>
  16. @interface FLEXFileBrowserTableViewCell : UITableViewCell
  17. @end
  18. typedef NS_ENUM(NSUInteger, FLEXFileBrowserSortAttribute) {
  19. FLEXFileBrowserSortAttributeNone = 0,
  20. FLEXFileBrowserSortAttributeName,
  21. FLEXFileBrowserSortAttributeCreationDate,
  22. };
  23. @interface FLEXFileBrowserController () <FLEXFileBrowserSearchOperationDelegate>
  24. @property (nonatomic, copy) NSString *path;
  25. @property (nonatomic, copy) NSArray<NSString *> *childPaths;
  26. @property (nonatomic) NSArray<NSString *> *searchPaths;
  27. @property (nonatomic) NSNumber *recursiveSize;
  28. @property (nonatomic) NSNumber *searchPathsSize;
  29. @property (nonatomic) NSOperationQueue *operationQueue;
  30. #if !TARGET_OS_TV
  31. @property (nonatomic) UIDocumentInteractionController *documentController;
  32. #endif
  33. @property (nonatomic) FLEXFileBrowserSortAttribute sortAttribute;
  34. @end
  35. @implementation FLEXFileBrowserController
  36. + (instancetype)path:(NSString *)path {
  37. return [[self alloc] initWithPath:path];
  38. }
  39. - (id)init {
  40. return [self initWithPath:NSHomeDirectory()];
  41. }
  42. - (id)initWithPath:(NSString *)path {
  43. self = [super init];
  44. if (self) {
  45. self.path = path;
  46. self.title = [path lastPathComponent];
  47. self.operationQueue = [NSOperationQueue new];
  48. //computing path size
  49. FLEXFileBrowserController *__weak weakSelf = self;
  50. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  51. NSFileManager *fileManager = NSFileManager.defaultManager;
  52. NSDictionary<NSString *, id> *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
  53. uint64_t totalSize = [attributes fileSize];
  54. for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
  55. attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
  56. totalSize += [attributes fileSize];
  57. // Bail if the interested view controller has gone away.
  58. if (!weakSelf) {
  59. return;
  60. }
  61. }
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. FLEXFileBrowserController *__strong strongSelf = weakSelf;
  64. strongSelf.recursiveSize = @(totalSize);
  65. [strongSelf.tableView reloadData];
  66. });
  67. });
  68. [self reloadCurrentPath];
  69. }
  70. return self;
  71. }
  72. #pragma mark - UIViewController
  73. - (void)viewDidLoad {
  74. [super viewDidLoad];
  75. self.showsSearchBar = YES;
  76. self.searchBarDebounceInterval = kFLEXDebounceForAsyncSearch;
  77. [self addToolbarItems:@[
  78. [[UIBarButtonItem alloc] initWithTitle:@"Sort"
  79. style:UIBarButtonItemStylePlain
  80. target:self
  81. action:@selector(sortDidTouchUpInside:)]
  82. ]];
  83. #if TARGET_OS_TV
  84. [self addlongPressGestureRecognizer];
  85. #endif
  86. }
  87. - (void)sortDidTouchUpInside:(UIBarButtonItem *)sortButton {
  88. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Sort"
  89. message:nil
  90. preferredStyle:UIAlertControllerStyleActionSheet];
  91. [alertController addAction:[UIAlertAction actionWithTitle:@"None"
  92. style:UIAlertActionStyleCancel
  93. handler:^(UIAlertAction * _Nonnull action) {
  94. [self sortWithAttribute:FLEXFileBrowserSortAttributeNone];
  95. }]];
  96. [alertController addAction:[UIAlertAction actionWithTitle:@"Name"
  97. style:UIAlertActionStyleDefault
  98. handler:^(UIAlertAction * _Nonnull action) {
  99. [self sortWithAttribute:FLEXFileBrowserSortAttributeName];
  100. }]];
  101. [alertController addAction:[UIAlertAction actionWithTitle:@"Creation Date"
  102. style:UIAlertActionStyleDefault
  103. handler:^(UIAlertAction * _Nonnull action) {
  104. [self sortWithAttribute:FLEXFileBrowserSortAttributeCreationDate];
  105. }]];
  106. [self presentViewController:alertController animated:YES completion:nil];
  107. }
  108. - (void)sortWithAttribute:(FLEXFileBrowserSortAttribute)attribute {
  109. self.sortAttribute = attribute;
  110. [self reloadDisplayedPaths];
  111. }
  112. #pragma mark - FLEXGlobalsEntry
  113. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  114. switch (row) {
  115. case FLEXGlobalsRowBrowseBundle: return @"📁 Browse Bundle Directory";
  116. case FLEXGlobalsRowBrowseContainer: return @"📁 Browse Container Directory";
  117. default: return nil;
  118. }
  119. }
  120. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  121. switch (row) {
  122. case FLEXGlobalsRowBrowseBundle: return [[self alloc] initWithPath:NSBundle.mainBundle.bundlePath];
  123. case FLEXGlobalsRowBrowseContainer: return [[self alloc] initWithPath:NSHomeDirectory()];
  124. default: return [self new];
  125. }
  126. }
  127. #pragma mark - FLEXFileBrowserSearchOperationDelegate
  128. - (void)fileBrowserSearchOperationResult:(NSArray<NSString *> *)searchResult size:(uint64_t)size {
  129. self.searchPaths = searchResult;
  130. self.searchPathsSize = @(size);
  131. [self.tableView reloadData];
  132. }
  133. #pragma mark - Search bar
  134. - (void)updateSearchResults:(NSString *)newText {
  135. [self reloadDisplayedPaths];
  136. }
  137. #pragma mark UISearchControllerDelegate
  138. - (void)willDismissSearchController:(UISearchController *)searchController {
  139. [self.operationQueue cancelAllOperations];
  140. [self reloadCurrentPath];
  141. [self.tableView reloadData];
  142. }
  143. #pragma mark - Table view data source
  144. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  145. return 1;
  146. }
  147. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  148. return self.searchController.isActive ? self.searchPaths.count : self.childPaths.count;
  149. }
  150. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  151. BOOL isSearchActive = self.searchController.isActive;
  152. NSNumber *currentSize = isSearchActive ? self.searchPathsSize : self.recursiveSize;
  153. NSArray<NSString *> *currentPaths = isSearchActive ? self.searchPaths : self.childPaths;
  154. NSString *sizeString = nil;
  155. if (!currentSize) {
  156. sizeString = @"Computing size…";
  157. } else {
  158. sizeString = [NSByteCountFormatter stringFromByteCount:[currentSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile];
  159. }
  160. return [NSString stringWithFormat:@"%lu files (%@)", (unsigned long)currentPaths.count, sizeString];
  161. }
  162. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  163. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  164. NSDictionary<NSString *, id> *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:fullPath error:NULL];
  165. BOOL isDirectory = [attributes.fileType isEqual:NSFileTypeDirectory];
  166. NSString *subtitle = nil;
  167. if (isDirectory) {
  168. NSUInteger count = [NSFileManager.defaultManager contentsOfDirectoryAtPath:fullPath error:NULL].count;
  169. subtitle = [NSString stringWithFormat:@"%lu item%@", (unsigned long)count, (count == 1 ? @"" : @"s")];
  170. } else {
  171. NSString *sizeString = [NSByteCountFormatter stringFromByteCount:attributes.fileSize countStyle:NSByteCountFormatterCountStyleFile];
  172. subtitle = [NSString stringWithFormat:@"%@ - %@", sizeString, attributes.fileModificationDate ?: @"Never modified"];
  173. }
  174. static NSString *textCellIdentifier = @"textCell";
  175. static NSString *imageCellIdentifier = @"imageCell";
  176. UITableViewCell *cell = nil;
  177. // Separate image and text only cells because otherwise the separator lines get out-of-whack on image cells reused with text only.
  178. UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
  179. NSString *cellIdentifier = image ? imageCellIdentifier : textCellIdentifier;
  180. if (!cell) {
  181. cell = [[FLEXFileBrowserTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  182. cell.textLabel.font = UIFont.flex_defaultTableCellFont;
  183. cell.detailTextLabel.font = UIFont.flex_defaultTableCellFont;
  184. cell.detailTextLabel.textColor = UIColor.grayColor;
  185. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  186. }
  187. NSString *cellTitle = [fullPath lastPathComponent];
  188. cell.textLabel.text = cellTitle;
  189. cell.detailTextLabel.text = subtitle;
  190. if (image) {
  191. cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
  192. cell.imageView.image = image;
  193. }
  194. return cell;
  195. }
  196. - (void)addlongPressGestureRecognizer {
  197. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  198. longPress.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeSelect]];
  199. [self.tableView addGestureRecognizer:longPress];
  200. }
  201. - (void)longPress:(UILongPressGestureRecognizer*)gesture {
  202. if ( gesture.state == UIGestureRecognizerStateEnded) {
  203. UITableViewCell *cell = [gesture.view valueForKey:@"_focusedCell"];
  204. [self showActionForCell:cell];
  205. }
  206. }
  207. - (void)fileBrowserOpen:(UITableViewCell *)cell {
  208. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  209. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  210. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:NULL];
  211. if (stillExists) {
  212. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  213. make.title([NSString stringWithFormat:@"Open %@?", fullPath.lastPathComponent]);
  214. make.button(@"OK").handler(^(NSArray<NSString *> *strings) {
  215. FXLog(@"TODO: implement opening files here!");
  216. });
  217. make.button(@"Cancel").cancelStyle();
  218. } showFrom:self];
  219. } else {
  220. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  221. }
  222. }
  223. - (void)showActionForCell:(UITableViewCell *)cell {
  224. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  225. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  226. FXLog(@"showActionForCell: %@", fullPath);
  227. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  228. make.title(@"Choose an action for this file");
  229. make.button(@"Open").handler(^(NSArray<NSString *> *strings) {
  230. [self fileBrowserOpen:cell];
  231. });
  232. make.button(@"Rename").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  233. [self fileBrowserRename:cell];
  234. });
  235. make.button(@"Delete").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  236. [self fileBrowserDelete:cell];
  237. });
  238. if ([FLEXUtility airdropAvailable]){
  239. make.button(@"Share").handler(^(NSArray<NSString *> *strings) {
  240. [self fileBrowserShare:cell];
  241. });
  242. }
  243. make.button(@"Cancel").cancelStyle();
  244. } showFrom:self];
  245. }
  246. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  247. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  248. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  249. NSString *subpath = fullPath.lastPathComponent;
  250. NSString *pathExtension = subpath.pathExtension;
  251. BOOL isDirectory = NO;
  252. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
  253. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  254. UIImage *image = cell.imageView.image;
  255. if (!stillExists) {
  256. [FLEXAlert showAlert:@"File Not Found" message:@"The file at the specified path no longer exists." from:self];
  257. [self reloadDisplayedPaths];
  258. return;
  259. }
  260. UIViewController *drillInViewController = nil;
  261. if (isDirectory) {
  262. drillInViewController = [[[self class] alloc] initWithPath:fullPath];
  263. } else if (image) {
  264. drillInViewController = [FLEXImagePreviewViewController forImage:image];
  265. } else {
  266. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  267. if (!fileData.length) {
  268. [FLEXAlert showAlert:@"Empty File" message:@"No data returned from the file." from:self];
  269. return;
  270. }
  271. // Special case keyed archives, json, and plists to get more readable data.
  272. NSString *prettyString = nil;
  273. if ([pathExtension isEqualToString:@"json"]) {
  274. prettyString = [FLEXUtility prettyJSONStringFromData:fileData];
  275. } else {
  276. // Regardless of file extension...
  277. id object = nil;
  278. @try {
  279. // Try to decode an archived object regardless of file extension
  280. object = [NSKeyedUnarchiver unarchiveObjectWithData:fileData];
  281. } @catch (NSException *e) { }
  282. // Try to decode other things instead
  283. object = object ?: [NSPropertyListSerialization
  284. propertyListWithData:fileData
  285. options:0
  286. format:NULL
  287. error:NULL
  288. ] ?: [NSDictionary dictionaryWithContentsOfFile:fullPath]
  289. ?: [NSArray arrayWithContentsOfFile:fullPath];
  290. if (object) {
  291. drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:object];
  292. } else {
  293. // Is it possibly a mach-O file?
  294. if (fileData.length > sizeof(struct mach_header_64)) {
  295. struct mach_header_64 header;
  296. [fileData getBytes:&header length:sizeof(struct mach_header_64)];
  297. // Does it have the mach header magic number?
  298. if (header.magic == MH_MAGIC_64) {
  299. // See if we can get some classes out of it...
  300. unsigned int count = 0;
  301. const char **classList = objc_copyClassNamesForImage(
  302. fullPath.UTF8String, &count
  303. );
  304. if (count > 0) {
  305. NSArray<NSString *> *classNames = [NSArray flex_forEachUpTo:count map:^id(NSUInteger i) {
  306. return objc_getClass(classList[i]);
  307. }];
  308. drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:classNames];
  309. }
  310. }
  311. }
  312. }
  313. }
  314. if (prettyString.length) {
  315. drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
  316. } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
  317. drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
  318. } else if ([FLEXTableListViewController supportsExtension:pathExtension]) {
  319. drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
  320. }
  321. else if (!drillInViewController) {
  322. NSString *fileString = [NSString stringWithUTF8String:fileData.bytes];
  323. if (fileString.length) {
  324. drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
  325. }
  326. }
  327. }
  328. if (drillInViewController) {
  329. drillInViewController.title = subpath.lastPathComponent;
  330. [self.navigationController pushViewController:drillInViewController animated:YES];
  331. } else {
  332. // Share the file otherwise
  333. [self openFileController:fullPath];
  334. }
  335. }
  336. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  337. #if !TARGET_OS_TV
  338. UIMenuItem *rename = [[UIMenuItem alloc] initWithTitle:@"Rename" action:@selector(fileBrowserRename:)];
  339. UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(fileBrowserDelete:)];
  340. UIMenuItem *copyPath = [[UIMenuItem alloc] initWithTitle:@"Copy Path" action:@selector(fileBrowserCopyPath:)];
  341. UIMenuItem *share = [[UIMenuItem alloc] initWithTitle:@"Share" action:@selector(fileBrowserShare:)];
  342. UIMenuController.sharedMenuController.menuItems = @[rename, delete, copyPath, share];
  343. return YES;
  344. #else
  345. return NO;
  346. #endif
  347. }
  348. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  349. return action == @selector(fileBrowserDelete:)
  350. || action == @selector(fileBrowserRename:)
  351. || action == @selector(fileBrowserCopyPath:)
  352. || action == @selector(fileBrowserShare:);
  353. }
  354. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  355. // Empty, but has to exist for the menu to show
  356. // The table view only calls this method for actions in the UIResponderStandardEditActions informal protocol.
  357. // Since our actions are outside of that protocol, we need to manually handle the action forwarding from the cells.
  358. }
  359. #if FLEX_AT_LEAST_IOS13_SDK
  360. #if !TARGET_OS_TV
  361. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  362. __weak __typeof__(self) weakSelf = self;
  363. return [UIContextMenuConfiguration configurationWithIdentifier:nil
  364. previewProvider:nil
  365. actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
  366. UITableViewCell * const cell = [tableView cellForRowAtIndexPath:indexPath];
  367. UIAction *rename = [UIAction actionWithTitle:@"Rename"
  368. image:nil
  369. identifier:@"Rename"
  370. handler:^(__kindof UIAction * _Nonnull action) {
  371. [weakSelf fileBrowserRename:cell];
  372. }];
  373. UIAction *delete = [UIAction actionWithTitle:@"Delete"
  374. image:nil
  375. identifier:@"Delete"
  376. handler:^(__kindof UIAction * _Nonnull action) {
  377. [weakSelf fileBrowserDelete:cell];
  378. }];
  379. UIAction *copyPath = [UIAction actionWithTitle:@"Copy Path"
  380. image:nil
  381. identifier:@"Copy Path"
  382. handler:^(__kindof UIAction * _Nonnull action) {
  383. [weakSelf fileBrowserCopyPath:cell];
  384. }];
  385. UIAction *share = [UIAction actionWithTitle:@"Share"
  386. image:nil
  387. identifier:@"Share"
  388. handler:^(__kindof UIAction * _Nonnull action) {
  389. [weakSelf fileBrowserShare:cell];
  390. }];
  391. return [UIMenu menuWithTitle:@"Manage File" image:nil identifier:@"Manage File" options:UIMenuOptionsDisplayInline children:@[rename, delete, copyPath, share]];
  392. }];
  393. }
  394. #endif
  395. #endif
  396. - (void)openFileController:(NSString *)fullPath {
  397. #if !TARGET_OS_TV
  398. UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
  399. controller.URL = [NSURL fileURLWithPath:fullPath];
  400. [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  401. self.documentController = controller;
  402. #endif
  403. }
  404. - (void)fileBrowserRename:(UITableViewCell *)sender {
  405. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  406. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  407. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:NULL];
  408. if (stillExists) {
  409. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  410. make.title([NSString stringWithFormat:@"Rename %@?", fullPath.lastPathComponent]);
  411. make.configuredTextField(^(UITextField *textField) {
  412. textField.placeholder = @"New file name";
  413. textField.text = fullPath.lastPathComponent;
  414. });
  415. make.button(@"Rename").handler(^(NSArray<NSString *> *strings) {
  416. NSString *newFileName = strings.firstObject;
  417. NSString *newPath = [fullPath.stringByDeletingLastPathComponent stringByAppendingPathComponent:newFileName];
  418. [NSFileManager.defaultManager moveItemAtPath:fullPath toPath:newPath error:NULL];
  419. [self reloadDisplayedPaths];
  420. });
  421. make.button(@"Cancel").cancelStyle();
  422. } showFrom:self];
  423. } else {
  424. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  425. }
  426. }
  427. - (void)fileBrowserDelete:(UITableViewCell *)sender {
  428. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  429. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  430. BOOL isDirectory = NO;
  431. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
  432. if (stillExists) {
  433. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  434. make.title(@"Confirm Deletion");
  435. make.message([NSString stringWithFormat:
  436. @"The %@ '%@' will be deleted. This operation cannot be undone",
  437. (isDirectory ? @"directory" : @"file"), fullPath.lastPathComponent
  438. ]);
  439. make.button(@"Delete").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  440. [NSFileManager.defaultManager removeItemAtPath:fullPath error:NULL];
  441. [self reloadDisplayedPaths];
  442. });
  443. make.button(@"Cancel").cancelStyle();
  444. } showFrom:self];
  445. } else {
  446. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  447. }
  448. }
  449. - (void)fileBrowserCopyPath:(UITableViewCell *)sender {
  450. #if !TARGET_OS_TV
  451. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  452. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  453. UIPasteboard.generalPasteboard.string = fullPath;
  454. #endif
  455. }
  456. - (void)fileBrowserShare:(UITableViewCell *)sender {
  457. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  458. NSString *pathString = [self filePathAtIndexPath:indexPath];
  459. NSURL *filePath = [NSURL fileURLWithPath:pathString];
  460. #if TARGET_OS_TV
  461. //This only helps on jailbroken AppleTV - it will allow you to share the files over AirDrop, no share option exists otherwise.
  462. if ([FLEXUtility airdropAvailable]){
  463. [FLEXUtility airDropFile:pathString];
  464. //NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"airdropper://%@", pathString]];
  465. //UIApplication *application = [UIApplication sharedApplication];
  466. //[application openURL:url options:@{} completionHandler:nil];
  467. } else {
  468. [FLEXAlert showAlert:@"Oh no" message:@"A jailbroken AppleTV is required to share files through AirDrop, sorry!" from:self];
  469. }
  470. #else
  471. BOOL isDirectory = NO;
  472. [NSFileManager.defaultManager fileExistsAtPath:pathString isDirectory:&isDirectory];
  473. if (isDirectory) {
  474. // UIDocumentInteractionController for folders
  475. [self openFileController:pathString];
  476. } else {
  477. // Share sheet for files
  478. UIActivityViewController *shareSheet = [[UIActivityViewController alloc] initWithActivityItems:@[filePath] applicationActivities:nil];
  479. [self presentViewController:shareSheet animated:true completion:nil];
  480. }
  481. #endif
  482. }
  483. - (void)reloadDisplayedPaths {
  484. if (self.searchController.isActive) {
  485. [self updateSearchPaths];
  486. } else {
  487. [self reloadCurrentPath];
  488. [self.tableView reloadData];
  489. }
  490. }
  491. - (void)reloadCurrentPath {
  492. NSMutableArray<NSString *> *childPaths = [NSMutableArray new];
  493. NSArray<NSString *> *subpaths = [NSFileManager.defaultManager contentsOfDirectoryAtPath:self.path error:NULL];
  494. for (NSString *subpath in subpaths) {
  495. [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
  496. }
  497. if (self.sortAttribute != FLEXFileBrowserSortAttributeNone) {
  498. [childPaths sortUsingComparator:^NSComparisonResult(NSString *path1, NSString *path2) {
  499. switch (self.sortAttribute) {
  500. case FLEXFileBrowserSortAttributeNone:
  501. // invalid state
  502. return NSOrderedSame;
  503. case FLEXFileBrowserSortAttributeName:
  504. return [path1 compare:path2];
  505. case FLEXFileBrowserSortAttributeCreationDate: {
  506. NSDictionary<NSFileAttributeKey, id> *path1Attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path1
  507. error:NULL];
  508. NSDictionary<NSFileAttributeKey, id> *path2Attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path2
  509. error:NULL];
  510. NSDate *path1Date = path1Attributes[NSFileCreationDate];
  511. NSDate *path2Date = path2Attributes[NSFileCreationDate];
  512. return [path1Date compare:path2Date];
  513. }
  514. }
  515. }];
  516. }
  517. self.childPaths = childPaths;
  518. }
  519. - (void)updateSearchPaths {
  520. self.searchPaths = nil;
  521. self.searchPathsSize = nil;
  522. //clear pre search request and start a new one
  523. [self.operationQueue cancelAllOperations];
  524. FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchText];
  525. newOperation.delegate = self;
  526. [self.operationQueue addOperation:newOperation];
  527. }
  528. - (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath {
  529. return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
  530. }
  531. @end
  532. @implementation FLEXFileBrowserTableViewCell
  533. - (void)forwardAction:(SEL)action withSender:(id)sender {
  534. id target = [self.nextResponder targetForAction:action withSender:sender];
  535. [UIApplication.sharedApplication sendAction:action to:target from:self forEvent:nil];
  536. }
  537. //really UIMenuController but this is to silence warnings
  538. - (void)fileBrowserRename:(UIViewController *)sender {
  539. [self forwardAction:_cmd withSender:sender];
  540. }
  541. - (void)fileBrowserDelete:(UIViewController *)sender {
  542. [self forwardAction:_cmd withSender:sender];
  543. }
  544. - (void)fileBrowserCopyPath:(UIViewController *)sender {
  545. [self forwardAction:_cmd withSender:sender];
  546. }
  547. - (void)fileBrowserShare:(UIViewController *)sender {
  548. [self forwardAction:_cmd withSender:sender];
  549. }
  550. @end