FLEXFileBrowserController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  201. rightTap.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause],[NSNumber numberWithInteger:UIPressTypeRightArrow]];
  202. [self.tableView addGestureRecognizer:rightTap];
  203. }
  204. - (void)longPress:(UILongPressGestureRecognizer*)gesture {
  205. if ( gesture.state == UIGestureRecognizerStateEnded) {
  206. UITableViewCell *cell = [gesture.view valueForKey:@"_focusedCell"];
  207. [self showActionForCell:cell];
  208. }
  209. }
  210. - (void)fileBrowserOpen:(UITableViewCell *)cell {
  211. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  212. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  213. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:NULL];
  214. if (stillExists) {
  215. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  216. make.title([NSString stringWithFormat:@"Open %@?", fullPath.lastPathComponent]);
  217. make.button(@"OK").handler(^(NSArray<NSString *> *strings) {
  218. FXLog(@"TODO: implement opening files here!");
  219. });
  220. make.button(@"Cancel").cancelStyle();
  221. } showFrom:self];
  222. } else {
  223. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  224. }
  225. }
  226. - (void)showActionForCell:(UITableViewCell *)cell {
  227. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  228. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  229. FXLog(@"showActionForCell: %@", fullPath);
  230. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  231. make.title(@"Choose an action for this file");
  232. make.button(@"Open").handler(^(NSArray<NSString *> *strings) {
  233. [self fileBrowserOpen:cell];
  234. });
  235. make.button(@"Rename").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  236. [self fileBrowserRename:cell];
  237. });
  238. make.button(@"Delete").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  239. [self fileBrowserDelete:cell];
  240. });
  241. if ([FLEXUtility airdropAvailable]){
  242. make.button(@"Share").handler(^(NSArray<NSString *> *strings) {
  243. [self fileBrowserShare:cell];
  244. });
  245. }
  246. make.button(@"Cancel").cancelStyle();
  247. } showFrom:self];
  248. }
  249. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  250. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  251. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  252. NSString *subpath = fullPath.lastPathComponent;
  253. NSString *pathExtension = subpath.pathExtension;
  254. BOOL isDirectory = NO;
  255. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
  256. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  257. UIImage *image = cell.imageView.image;
  258. if (!stillExists) {
  259. [FLEXAlert showAlert:@"File Not Found" message:@"The file at the specified path no longer exists." from:self];
  260. [self reloadDisplayedPaths];
  261. return;
  262. }
  263. UIViewController *drillInViewController = nil;
  264. if (isDirectory) {
  265. drillInViewController = [[[self class] alloc] initWithPath:fullPath];
  266. } else if (image) {
  267. drillInViewController = [FLEXImagePreviewViewController forImage:image];
  268. } else {
  269. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  270. if (!fileData.length) {
  271. [FLEXAlert showAlert:@"Empty File" message:@"No data returned from the file." from:self];
  272. return;
  273. }
  274. // Special case keyed archives, json, and plists to get more readable data.
  275. NSString *prettyString = nil;
  276. if ([pathExtension isEqualToString:@"json"]) {
  277. prettyString = [FLEXUtility prettyJSONStringFromData:fileData];
  278. } else {
  279. // Regardless of file extension...
  280. id object = nil;
  281. @try {
  282. // Try to decode an archived object regardless of file extension
  283. object = [NSKeyedUnarchiver unarchiveObjectWithData:fileData];
  284. } @catch (NSException *e) { }
  285. // Try to decode other things instead
  286. object = object ?: [NSPropertyListSerialization
  287. propertyListWithData:fileData
  288. options:0
  289. format:NULL
  290. error:NULL
  291. ] ?: [NSDictionary dictionaryWithContentsOfFile:fullPath]
  292. ?: [NSArray arrayWithContentsOfFile:fullPath];
  293. if (object) {
  294. drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:object];
  295. } else {
  296. // Is it possibly a mach-O file?
  297. if (fileData.length > sizeof(struct mach_header_64)) {
  298. struct mach_header_64 header;
  299. [fileData getBytes:&header length:sizeof(struct mach_header_64)];
  300. // Does it have the mach header magic number?
  301. if (header.magic == MH_MAGIC_64) {
  302. // See if we can get some classes out of it...
  303. unsigned int count = 0;
  304. const char **classList = objc_copyClassNamesForImage(
  305. fullPath.UTF8String, &count
  306. );
  307. if (count > 0) {
  308. NSArray<NSString *> *classNames = [NSArray flex_forEachUpTo:count map:^id(NSUInteger i) {
  309. return objc_getClass(classList[i]);
  310. }];
  311. drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:classNames];
  312. }
  313. }
  314. }
  315. }
  316. }
  317. if (prettyString.length) {
  318. drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
  319. } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
  320. drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
  321. } else if ([FLEXTableListViewController supportsExtension:pathExtension]) {
  322. drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
  323. }
  324. else if (!drillInViewController) {
  325. NSString *fileString = [NSString stringWithUTF8String:fileData.bytes];
  326. if (fileString.length) {
  327. drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
  328. }
  329. }
  330. }
  331. if (drillInViewController) {
  332. drillInViewController.title = subpath.lastPathComponent;
  333. [self.navigationController pushViewController:drillInViewController animated:YES];
  334. } else {
  335. // Share the file otherwise
  336. [self openFileController:fullPath];
  337. }
  338. }
  339. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  340. #if !TARGET_OS_TV
  341. UIMenuItem *rename = [[UIMenuItem alloc] initWithTitle:@"Rename" action:@selector(fileBrowserRename:)];
  342. UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(fileBrowserDelete:)];
  343. UIMenuItem *copyPath = [[UIMenuItem alloc] initWithTitle:@"Copy Path" action:@selector(fileBrowserCopyPath:)];
  344. UIMenuItem *share = [[UIMenuItem alloc] initWithTitle:@"Share" action:@selector(fileBrowserShare:)];
  345. UIMenuController.sharedMenuController.menuItems = @[rename, delete, copyPath, share];
  346. return YES;
  347. #else
  348. return NO;
  349. #endif
  350. }
  351. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  352. return action == @selector(fileBrowserDelete:)
  353. || action == @selector(fileBrowserRename:)
  354. || action == @selector(fileBrowserCopyPath:)
  355. || action == @selector(fileBrowserShare:);
  356. }
  357. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  358. // Empty, but has to exist for the menu to show
  359. // The table view only calls this method for actions in the UIResponderStandardEditActions informal protocol.
  360. // Since our actions are outside of that protocol, we need to manually handle the action forwarding from the cells.
  361. }
  362. #if FLEX_AT_LEAST_IOS13_SDK
  363. #if !TARGET_OS_TV
  364. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  365. __weak __typeof__(self) weakSelf = self;
  366. return [UIContextMenuConfiguration configurationWithIdentifier:nil
  367. previewProvider:nil
  368. actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
  369. UITableViewCell * const cell = [tableView cellForRowAtIndexPath:indexPath];
  370. UIAction *rename = [UIAction actionWithTitle:@"Rename"
  371. image:nil
  372. identifier:@"Rename"
  373. handler:^(__kindof UIAction * _Nonnull action) {
  374. [weakSelf fileBrowserRename:cell];
  375. }];
  376. UIAction *delete = [UIAction actionWithTitle:@"Delete"
  377. image:nil
  378. identifier:@"Delete"
  379. handler:^(__kindof UIAction * _Nonnull action) {
  380. [weakSelf fileBrowserDelete:cell];
  381. }];
  382. UIAction *copyPath = [UIAction actionWithTitle:@"Copy Path"
  383. image:nil
  384. identifier:@"Copy Path"
  385. handler:^(__kindof UIAction * _Nonnull action) {
  386. [weakSelf fileBrowserCopyPath:cell];
  387. }];
  388. UIAction *share = [UIAction actionWithTitle:@"Share"
  389. image:nil
  390. identifier:@"Share"
  391. handler:^(__kindof UIAction * _Nonnull action) {
  392. [weakSelf fileBrowserShare:cell];
  393. }];
  394. return [UIMenu menuWithTitle:@"Manage File" image:nil identifier:@"Manage File" options:UIMenuOptionsDisplayInline children:@[rename, delete, copyPath, share]];
  395. }];
  396. }
  397. #endif
  398. #endif
  399. - (void)openFileController:(NSString *)fullPath {
  400. #if !TARGET_OS_TV
  401. UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
  402. controller.URL = [NSURL fileURLWithPath:fullPath];
  403. [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  404. self.documentController = controller;
  405. #endif
  406. }
  407. - (void)fileBrowserRename:(UITableViewCell *)sender {
  408. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  409. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  410. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:NULL];
  411. if (stillExists) {
  412. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  413. make.title([NSString stringWithFormat:@"Rename %@?", fullPath.lastPathComponent]);
  414. make.configuredTextField(^(UITextField *textField) {
  415. textField.placeholder = @"New file name";
  416. textField.text = fullPath.lastPathComponent;
  417. });
  418. make.button(@"Rename").handler(^(NSArray<NSString *> *strings) {
  419. NSString *newFileName = strings.firstObject;
  420. NSString *newPath = [fullPath.stringByDeletingLastPathComponent stringByAppendingPathComponent:newFileName];
  421. [NSFileManager.defaultManager moveItemAtPath:fullPath toPath:newPath error:NULL];
  422. [self reloadDisplayedPaths];
  423. });
  424. make.button(@"Cancel").cancelStyle();
  425. } showFrom:self];
  426. } else {
  427. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  428. }
  429. }
  430. - (void)fileBrowserDelete:(UITableViewCell *)sender {
  431. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  432. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  433. BOOL isDirectory = NO;
  434. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
  435. if (stillExists) {
  436. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  437. make.title(@"Confirm Deletion");
  438. make.message([NSString stringWithFormat:
  439. @"The %@ '%@' will be deleted. This operation cannot be undone",
  440. (isDirectory ? @"directory" : @"file"), fullPath.lastPathComponent
  441. ]);
  442. make.button(@"Delete").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  443. [NSFileManager.defaultManager removeItemAtPath:fullPath error:NULL];
  444. [self reloadDisplayedPaths];
  445. });
  446. make.button(@"Cancel").cancelStyle();
  447. } showFrom:self];
  448. } else {
  449. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  450. }
  451. }
  452. - (void)fileBrowserCopyPath:(UITableViewCell *)sender {
  453. #if !TARGET_OS_TV
  454. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  455. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  456. UIPasteboard.generalPasteboard.string = fullPath;
  457. #endif
  458. }
  459. - (void)fileBrowserShare:(UITableViewCell *)sender {
  460. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  461. NSString *pathString = [self filePathAtIndexPath:indexPath];
  462. NSURL *filePath = [NSURL fileURLWithPath:pathString];
  463. #if TARGET_OS_TV
  464. //This only helps on jailbroken AppleTV - it will allow you to share the files over AirDrop, no share option exists otherwise.
  465. if ([FLEXUtility airdropAvailable]){
  466. [FLEXUtility airDropFile:pathString];
  467. //NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"airdropper://%@", pathString]];
  468. //UIApplication *application = [UIApplication sharedApplication];
  469. //[application openURL:url options:@{} completionHandler:nil];
  470. } else {
  471. [FLEXAlert showAlert:@"Oh no" message:@"A jailbroken AppleTV is required to share files through AirDrop, sorry!" from:self];
  472. }
  473. #else
  474. BOOL isDirectory = NO;
  475. [NSFileManager.defaultManager fileExistsAtPath:pathString isDirectory:&isDirectory];
  476. if (isDirectory) {
  477. // UIDocumentInteractionController for folders
  478. [self openFileController:pathString];
  479. } else {
  480. // Share sheet for files
  481. UIActivityViewController *shareSheet = [[UIActivityViewController alloc] initWithActivityItems:@[filePath] applicationActivities:nil];
  482. [self presentViewController:shareSheet animated:true completion:nil];
  483. }
  484. #endif
  485. }
  486. - (void)reloadDisplayedPaths {
  487. if (self.searchController.isActive) {
  488. [self updateSearchPaths];
  489. } else {
  490. [self reloadCurrentPath];
  491. [self.tableView reloadData];
  492. }
  493. }
  494. - (void)reloadCurrentPath {
  495. NSMutableArray<NSString *> *childPaths = [NSMutableArray new];
  496. NSArray<NSString *> *subpaths = [NSFileManager.defaultManager contentsOfDirectoryAtPath:self.path error:NULL];
  497. for (NSString *subpath in subpaths) {
  498. [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
  499. }
  500. if (self.sortAttribute != FLEXFileBrowserSortAttributeNone) {
  501. [childPaths sortUsingComparator:^NSComparisonResult(NSString *path1, NSString *path2) {
  502. switch (self.sortAttribute) {
  503. case FLEXFileBrowserSortAttributeNone:
  504. // invalid state
  505. return NSOrderedSame;
  506. case FLEXFileBrowserSortAttributeName:
  507. return [path1 compare:path2];
  508. case FLEXFileBrowserSortAttributeCreationDate: {
  509. NSDictionary<NSFileAttributeKey, id> *path1Attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path1
  510. error:NULL];
  511. NSDictionary<NSFileAttributeKey, id> *path2Attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path2
  512. error:NULL];
  513. NSDate *path1Date = path1Attributes[NSFileCreationDate];
  514. NSDate *path2Date = path2Attributes[NSFileCreationDate];
  515. return [path1Date compare:path2Date];
  516. }
  517. }
  518. }];
  519. }
  520. self.childPaths = childPaths;
  521. }
  522. - (void)updateSearchPaths {
  523. self.searchPaths = nil;
  524. self.searchPathsSize = nil;
  525. //clear pre search request and start a new one
  526. [self.operationQueue cancelAllOperations];
  527. FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchText];
  528. newOperation.delegate = self;
  529. [self.operationQueue addOperation:newOperation];
  530. }
  531. - (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath {
  532. return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
  533. }
  534. @end
  535. @implementation FLEXFileBrowserTableViewCell
  536. - (void)forwardAction:(SEL)action withSender:(id)sender {
  537. id target = [self.nextResponder targetForAction:action withSender:sender];
  538. [UIApplication.sharedApplication sendAction:action to:target from:self forEvent:nil];
  539. }
  540. //really UIMenuController but this is to silence warnings
  541. - (void)fileBrowserRename:(UIViewController *)sender {
  542. [self forwardAction:_cmd withSender:sender];
  543. }
  544. - (void)fileBrowserDelete:(UIViewController *)sender {
  545. [self forwardAction:_cmd withSender:sender];
  546. }
  547. - (void)fileBrowserCopyPath:(UIViewController *)sender {
  548. [self forwardAction:_cmd withSender:sender];
  549. }
  550. - (void)fileBrowserShare:(UIViewController *)sender {
  551. [self forwardAction:_cmd withSender:sender];
  552. }
  553. @end