| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- //
- // FLEXFileBrowserTableViewController.m
- // Flipboard
- //
- // Created by Ryan Olson on 6/9/14.
- //
- //
- #import "FLEXFileBrowserTableViewController.h"
- #import "FLEXFileBrowserFileOperationController.h"
- #import "FLEXUtility.h"
- #import "FLEXWebViewController.h"
- #import "FLEXImagePreviewViewController.h"
- #import "FLEXTableListViewController.h"
- @interface FLEXFileBrowserTableViewCell : UITableViewCell
- @end
- @interface FLEXFileBrowserTableViewController () <FLEXFileBrowserFileOperationControllerDelegate, FLEXFileBrowserSearchOperationDelegate, UISearchResultsUpdating, UISearchControllerDelegate>
- @property (nonatomic, copy) NSString *path;
- @property (nonatomic, copy) NSArray *childPaths;
- @property (nonatomic, strong) NSArray *searchPaths;
- @property (nonatomic, strong) NSNumber *recursiveSize;
- @property (nonatomic, strong) NSNumber *searchPathsSize;
- @property (nonatomic, strong) UISearchController *searchController;
- @property (nonatomic) NSOperationQueue *operationQueue;
- @property (nonatomic, strong) UIDocumentInteractionController *documentController;
- @property (nonatomic, strong) id<FLEXFileBrowserFileOperationController> fileOperationController;
- @end
- @implementation FLEXFileBrowserTableViewController
- - (id)initWithStyle:(UITableViewStyle)style
- {
- return [self initWithPath:NSHomeDirectory()];
- }
- - (id)initWithPath:(NSString *)path
- {
- self = [super initWithStyle:UITableViewStyleGrouped];
- if (self) {
- self.path = path;
- self.title = [path lastPathComponent];
- self.operationQueue = [NSOperationQueue new];
-
- self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
- self.searchController.searchResultsUpdater = self;
- self.searchController.delegate = self;
- self.searchController.dimsBackgroundDuringPresentation = NO;
- self.tableView.tableHeaderView = self.searchController.searchBar;
-
- //computing path size
- FLEXFileBrowserTableViewController *__weak weakSelf = self;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
- uint64_t totalSize = [attributes fileSize];
-
- for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
- attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
- totalSize += [attributes fileSize];
-
- // Bail if the interested view controller has gone away.
- if (!weakSelf) {
- return;
- }
- }
-
- dispatch_async(dispatch_get_main_queue(), ^{
- FLEXFileBrowserTableViewController *__strong strongSelf = weakSelf;
- strongSelf.recursiveSize = @(totalSize);
- [strongSelf.tableView reloadData];
- });
- });
- [self reloadChildPaths];
- }
- return self;
- }
- #pragma mark - UIViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIMenuItem *renameMenuItem = [[UIMenuItem alloc] initWithTitle:@"Rename" action:@selector(fileBrowserRename:)];
- UIMenuItem *deleteMenuItem = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(fileBrowserDelete:)];
- [UIMenuController sharedMenuController].menuItems = @[renameMenuItem, deleteMenuItem];
- }
- #pragma mark - FLEXFileBrowserSearchOperationDelegate
- - (void)fileBrowserSearchOperationResult:(NSArray *)searchResult size:(uint64_t)size
- {
- self.searchPaths = searchResult;
- self.searchPathsSize = @(size);
- [self.tableView reloadData];
- }
- #pragma mark - UISearchResultsUpdating
- - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
- {
- [self reloadDisplayedPaths];
- }
- #pragma mark - UISearchControllerDelegate
- - (void)willDismissSearchController:(UISearchController *)searchController
- {
- [self.operationQueue cancelAllOperations];
- [self reloadChildPaths];
- [self.tableView reloadData];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return self.searchController.isActive ? [self.searchPaths count] : [self.childPaths count];
- }
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- BOOL isSearchActive = self.searchController.isActive;
- NSNumber *currentSize = isSearchActive ? self.searchPathsSize : self.recursiveSize;
- NSArray *currentPaths = isSearchActive ? self.searchPaths : self.childPaths;
-
- NSString *sizeString = nil;
- if (!currentSize) {
- sizeString = @"Computing size…";
- } else {
- sizeString = [NSByteCountFormatter stringFromByteCount:[currentSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile];
- }
-
- return [NSString stringWithFormat:@"%lu files (%@)", (unsigned long)[currentPaths count], sizeString];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSString *fullPath = [self filePathAtIndexPath:indexPath];
- NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:NULL];
- BOOL isDirectory = [[attributes fileType] isEqual:NSFileTypeDirectory];
- NSString *subtitle = nil;
- if (isDirectory) {
- NSUInteger count = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:NULL] count];
- subtitle = [NSString stringWithFormat:@"%lu file%@", (unsigned long)count, (count == 1 ? @"" : @"s")];
- } else {
- NSString *sizeString = [NSByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleFile];
- subtitle = [NSString stringWithFormat:@"%@ - %@", sizeString, [attributes fileModificationDate]];
- }
-
- static NSString *textCellIdentifier = @"textCell";
- static NSString *imageCellIdentifier = @"imageCell";
- UITableViewCell *cell = nil;
-
- // Separate image and text only cells because otherwise the separator lines get out-of-whack on image cells reused with text only.
- BOOL showImagePreview = [FLEXUtility isImagePathExtension:[fullPath pathExtension]];
- NSString *cellIdentifier = showImagePreview ? imageCellIdentifier : textCellIdentifier;
-
- if (!cell) {
- cell = [[FLEXFileBrowserTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
- cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
- cell.detailTextLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
- cell.detailTextLabel.textColor = [UIColor grayColor];
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- }
- NSString *cellTitle = [fullPath lastPathComponent];
- cell.textLabel.text = cellTitle;
- cell.detailTextLabel.text = subtitle;
-
- if (showImagePreview) {
- cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
- cell.imageView.image = [UIImage imageWithContentsOfFile:fullPath];
- }
-
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSString *fullPath = [self filePathAtIndexPath:indexPath];
- NSString *subpath = [fullPath lastPathComponent];
- NSString *pathExtension = [subpath pathExtension];
-
- BOOL isDirectory = NO;
- BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
- if (stillExists) {
- UIViewController *drillInViewController = nil;
- if (isDirectory) {
- drillInViewController = [[[self class] alloc] initWithPath:fullPath];
- } else if ([FLEXUtility isImagePathExtension:pathExtension]) {
- UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
- drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
- } else {
- // Special case keyed archives, json, and plists to get more readable data.
- NSString *prettyString = nil;
- if ([pathExtension isEqual:@"archive"] || [pathExtension isEqual:@"coded"]) {
- prettyString = [[NSKeyedUnarchiver unarchiveObjectWithFile:fullPath] description];
- } else if ([pathExtension isEqualToString:@"json"]) {
- prettyString = [FLEXUtility prettyJSONStringFromData:[NSData dataWithContentsOfFile:fullPath]];
- } else if ([pathExtension isEqualToString:@"plist"]) {
- NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
- prettyString = [[NSPropertyListSerialization propertyListWithData:fileData options:0 format:NULL error:NULL] description];
- }
-
- if ([prettyString length] > 0) {
- drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
- } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
- drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
- } else if ([FLEXTableListViewController supportsExtension:subpath.pathExtension]) {
- drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
- }
- else {
- NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:NULL];
- if ([fileString length] > 0) {
- drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
- }
- }
- }
-
- if (drillInViewController) {
- drillInViewController.title = [subpath lastPathComponent];
- [self.navigationController pushViewController:drillInViewController animated:YES];
- } else {
- [self openFileController:fullPath];
- [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- } else {
- [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
- [self reloadDisplayedPaths];
- }
- }
- - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
- - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
- {
- return action == @selector(fileBrowserDelete:) || action == @selector(fileBrowserRename:);
- }
- - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
- {
- // Empty, but has to exist for the menu to show
- // The table view only calls this method for actions in the UIResponderStandardEditActions informal protocol.
- // Since our actions are outside of that protocol, we need to manually handle the action forwarding from the cells.
- }
- #pragma mark - FLEXFileBrowserFileOperationControllerDelegate
- - (void)fileOperationControllerDidDismiss:(id<FLEXFileBrowserFileOperationController>)controller
- {
- [self reloadDisplayedPaths];
- }
- - (void)openFileController:(NSString *)fullPath
- {
- UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
- controller.URL = [[NSURL alloc] initFileURLWithPath:fullPath];
- [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
- self.documentController = controller;
- }
- - (void)fileBrowserRename:(UITableViewCell *)sender
- {
- NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
- NSString *fullPath = [self filePathAtIndexPath:indexPath];
- self.fileOperationController = [[FLEXFileBrowserFileRenameOperationController alloc] initWithPath:fullPath];
- self.fileOperationController.delegate = self;
- [self.fileOperationController show];
- }
- - (void)fileBrowserDelete:(UITableViewCell *)sender
- {
- NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
- NSString *fullPath = [self filePathAtIndexPath:indexPath];
-
- self.fileOperationController = [[FLEXFileBrowserFileDeleteOperationController alloc] initWithPath:fullPath];
- self.fileOperationController.delegate = self;
- [self.fileOperationController show];
- }
- - (void)reloadDisplayedPaths
- {
- if (self.searchController.isActive) {
- [self reloadSearchPaths];
- } else {
- [self reloadChildPaths];
- }
- [self.tableView reloadData];
- }
- - (void)reloadChildPaths
- {
- NSMutableArray *childPaths = [NSMutableArray array];
- NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.path error:NULL];
- for (NSString *subpath in subpaths) {
- [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
- }
- self.childPaths = childPaths;
- }
- - (void)reloadSearchPaths
- {
- self.searchPaths = nil;
- self.searchPathsSize = nil;
- //clear pre search request and start a new one
- [self.operationQueue cancelAllOperations];
- FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchController.searchBar.text];
- newOperation.delegate = self;
- [self.operationQueue addOperation:newOperation];
- }
- - (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath
- {
- return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
- }
- @end
- @implementation FLEXFileBrowserTableViewCell
- - (void)fileBrowserRename:(UIMenuController *)sender
- {
- id target = [self.nextResponder targetForAction:_cmd withSender:sender];
- [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
- }
- - (void)fileBrowserDelete:(UIMenuController *)sender
- {
- id target = [self.nextResponder targetForAction:_cmd withSender:sender];
- [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
- }
- @end
|