FLEXFileBrowserController.m 24 KB

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