FLEXFileBrowserController.m 24 KB

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