FLEXFileBrowserController.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0) {
  302. __weak __typeof__(self) weakSelf = self;
  303. return [UIContextMenuConfiguration configurationWithIdentifier:nil
  304. previewProvider:nil
  305. actionProvider:^UIMenu * _Nullable(NSArray<UIMenuElement *> * _Nonnull suggestedActions) {
  306. UITableViewCell * const cell = [tableView cellForRowAtIndexPath:indexPath];
  307. UIAction *rename = [UIAction actionWithTitle:@"Rename"
  308. image:nil
  309. identifier:@"Rename"
  310. handler:^(__kindof UIAction * _Nonnull action) {
  311. [weakSelf fileBrowserRename:cell];
  312. }];
  313. UIAction *delete = [UIAction actionWithTitle:@"Delete"
  314. image:nil
  315. identifier:@"Delete"
  316. handler:^(__kindof UIAction * _Nonnull action) {
  317. [weakSelf fileBrowserDelete:cell];
  318. }];
  319. UIAction *copyPath = [UIAction actionWithTitle:@"Copy Path"
  320. image:nil
  321. identifier:@"Copy Path"
  322. handler:^(__kindof UIAction * _Nonnull action) {
  323. [weakSelf fileBrowserCopyPath:cell];
  324. }];
  325. UIAction *share = [UIAction actionWithTitle:@"Share"
  326. image:nil
  327. identifier:@"Share"
  328. handler:^(__kindof UIAction * _Nonnull action) {
  329. [weakSelf fileBrowserShare:cell];
  330. }];
  331. return [UIMenu menuWithTitle:@"Manage File" image:nil identifier:@"Manage File" options:UIMenuOptionsDisplayInline children:@[rename, delete, copyPath, share]];
  332. }];
  333. }
  334. #endif
  335. - (void)openFileController:(NSString *)fullPath {
  336. UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
  337. controller.URL = [NSURL fileURLWithPath:fullPath];
  338. [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  339. self.documentController = controller;
  340. }
  341. - (void)fileBrowserRename:(UITableViewCell *)sender {
  342. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  343. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  344. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:self.path isDirectory:NULL];
  345. if (stillExists) {
  346. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  347. make.title([NSString stringWithFormat:@"Rename %@?", fullPath.lastPathComponent]);
  348. make.configuredTextField(^(UITextField *textField) {
  349. textField.placeholder = @"New file name";
  350. textField.text = fullPath.lastPathComponent;
  351. });
  352. make.button(@"Rename").handler(^(NSArray<NSString *> *strings) {
  353. NSString *newFileName = strings.firstObject;
  354. NSString *newPath = [fullPath.stringByDeletingLastPathComponent stringByAppendingPathComponent:newFileName];
  355. [NSFileManager.defaultManager moveItemAtPath:fullPath toPath:newPath error:NULL];
  356. [self reloadDisplayedPaths];
  357. });
  358. make.button(@"Cancel").cancelStyle();
  359. } showFrom:self];
  360. } else {
  361. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  362. }
  363. }
  364. - (void)fileBrowserDelete:(UITableViewCell *)sender {
  365. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  366. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  367. BOOL isDirectory = NO;
  368. BOOL stillExists = [NSFileManager.defaultManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
  369. if (stillExists) {
  370. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  371. make.title(@"Confirm Deletion");
  372. make.message([NSString stringWithFormat:
  373. @"The %@ '%@' will be deleted. This operation cannot be undone",
  374. (isDirectory ? @"directory" : @"file"), fullPath.lastPathComponent
  375. ]);
  376. make.button(@"Delete").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
  377. [NSFileManager.defaultManager removeItemAtPath:fullPath error:NULL];
  378. [self reloadDisplayedPaths];
  379. });
  380. make.button(@"Cancel").cancelStyle();
  381. } showFrom:self];
  382. } else {
  383. [FLEXAlert showAlert:@"File Removed" message:@"The file at the specified path no longer exists." from:self];
  384. }
  385. }
  386. - (void)fileBrowserCopyPath:(UITableViewCell *)sender {
  387. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  388. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  389. UIPasteboard.generalPasteboard.string = fullPath;
  390. }
  391. - (void)fileBrowserShare:(UITableViewCell *)sender {
  392. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  393. NSString *pathString = [self filePathAtIndexPath:indexPath];
  394. NSURL *filePath = [NSURL fileURLWithPath:pathString];
  395. BOOL isDirectory = NO;
  396. [NSFileManager.defaultManager fileExistsAtPath:pathString isDirectory:&isDirectory];
  397. if (isDirectory) {
  398. // UIDocumentInteractionController for folders
  399. [self openFileController:pathString];
  400. } else {
  401. // Share sheet for files
  402. UIActivityViewController *shareSheet = [[UIActivityViewController alloc] initWithActivityItems:@[filePath] applicationActivities:nil];
  403. [self presentViewController:shareSheet animated:true completion:nil];
  404. }
  405. }
  406. - (void)reloadDisplayedPaths {
  407. if (self.searchController.isActive) {
  408. [self updateSearchPaths];
  409. } else {
  410. [self reloadCurrentPath];
  411. [self.tableView reloadData];
  412. }
  413. }
  414. - (void)reloadCurrentPath {
  415. NSMutableArray<NSString *> *childPaths = [NSMutableArray new];
  416. NSArray<NSString *> *subpaths = [NSFileManager.defaultManager contentsOfDirectoryAtPath:self.path error:NULL];
  417. for (NSString *subpath in subpaths) {
  418. [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
  419. }
  420. if (self.sortAttribute != FLEXFileBrowserSortAttributeNone) {
  421. [childPaths sortUsingComparator:^NSComparisonResult(NSString *path1, NSString *path2) {
  422. switch (self.sortAttribute) {
  423. case FLEXFileBrowserSortAttributeNone:
  424. // invalid state
  425. return NSOrderedSame;
  426. case FLEXFileBrowserSortAttributeName:
  427. return [path1 compare:path2];
  428. case FLEXFileBrowserSortAttributeCreationDate: {
  429. NSDictionary<NSFileAttributeKey, id> *path1Attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path1
  430. error:NULL];
  431. NSDictionary<NSFileAttributeKey, id> *path2Attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path2
  432. error:NULL];
  433. NSDate *path1Date = path1Attributes[NSFileCreationDate];
  434. NSDate *path2Date = path2Attributes[NSFileCreationDate];
  435. return [path1Date compare:path2Date];
  436. }
  437. }
  438. }];
  439. }
  440. self.childPaths = childPaths;
  441. }
  442. - (void)updateSearchPaths {
  443. self.searchPaths = nil;
  444. self.searchPathsSize = nil;
  445. //clear pre search request and start a new one
  446. [self.operationQueue cancelAllOperations];
  447. FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchText];
  448. newOperation.delegate = self;
  449. [self.operationQueue addOperation:newOperation];
  450. }
  451. - (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath {
  452. return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
  453. }
  454. @end
  455. @implementation FLEXFileBrowserTableViewCell
  456. - (void)forwardAction:(SEL)action withSender:(id)sender {
  457. id target = [self.nextResponder targetForAction:action withSender:sender];
  458. [UIApplication.sharedApplication sendAction:action to:target from:self forEvent:nil];
  459. }
  460. - (void)fileBrowserRename:(UIMenuController *)sender {
  461. [self forwardAction:_cmd withSender:sender];
  462. }
  463. - (void)fileBrowserDelete:(UIMenuController *)sender {
  464. [self forwardAction:_cmd withSender:sender];
  465. }
  466. - (void)fileBrowserCopyPath:(UIMenuController *)sender {
  467. [self forwardAction:_cmd withSender:sender];
  468. }
  469. - (void)fileBrowserShare:(UIMenuController *)sender {
  470. [self forwardAction:_cmd withSender:sender];
  471. }
  472. @end