FLEXFileBrowserTableViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // FLEXFileBrowserTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/9/14.
  6. //
  7. //
  8. #import "FLEXFileBrowserTableViewController.h"
  9. #import "FLEXFileBrowserFileOperationController.h"
  10. #import "FLEXUtility.h"
  11. #import "FLEXWebViewController.h"
  12. #import "FLEXImagePreviewViewController.h"
  13. #import "FLEXTableListViewController.h"
  14. @interface FLEXFileBrowserTableViewCell : UITableViewCell
  15. @end
  16. @interface FLEXFileBrowserTableViewController () <FLEXFileBrowserFileOperationControllerDelegate, FLEXFileBrowserSearchOperationDelegate, UISearchResultsUpdating, UISearchControllerDelegate>
  17. @property (nonatomic, copy) NSString *path;
  18. @property (nonatomic, copy) NSArray *childPaths;
  19. @property (nonatomic, strong) NSArray *searchPaths;
  20. @property (nonatomic, strong) NSNumber *recursiveSize;
  21. @property (nonatomic, strong) NSNumber *searchPathsSize;
  22. @property (nonatomic, strong) UISearchController *searchController;
  23. @property (nonatomic) NSOperationQueue *operationQueue;
  24. @property (nonatomic, strong) UIDocumentInteractionController *documentController;
  25. @property (nonatomic, strong) id<FLEXFileBrowserFileOperationController> fileOperationController;
  26. @end
  27. @implementation FLEXFileBrowserTableViewController
  28. - (id)initWithStyle:(UITableViewStyle)style
  29. {
  30. return [self initWithPath:NSHomeDirectory()];
  31. }
  32. - (id)initWithPath:(NSString *)path
  33. {
  34. self = [super initWithStyle:UITableViewStyleGrouped];
  35. if (self) {
  36. self.path = path;
  37. self.title = [path lastPathComponent];
  38. self.operationQueue = [NSOperationQueue new];
  39. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  40. self.searchController.searchResultsUpdater = self;
  41. self.searchController.delegate = self;
  42. self.searchController.dimsBackgroundDuringPresentation = NO;
  43. self.tableView.tableHeaderView = self.searchController.searchBar;
  44. //computing path size
  45. FLEXFileBrowserTableViewController *__weak weakSelf = self;
  46. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  47. NSFileManager *fileManager = [NSFileManager defaultManager];
  48. NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
  49. uint64_t totalSize = [attributes fileSize];
  50. for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
  51. attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
  52. totalSize += [attributes fileSize];
  53. // Bail if the interested view controller has gone away.
  54. if (!weakSelf) {
  55. return;
  56. }
  57. }
  58. dispatch_async(dispatch_get_main_queue(), ^{
  59. FLEXFileBrowserTableViewController *__strong strongSelf = weakSelf;
  60. strongSelf.recursiveSize = @(totalSize);
  61. [strongSelf.tableView reloadData];
  62. });
  63. });
  64. [self reloadChildPaths];
  65. }
  66. return self;
  67. }
  68. #pragma mark - UIViewController
  69. - (void)viewDidLoad
  70. {
  71. [super viewDidLoad];
  72. UIMenuItem *renameMenuItem = [[UIMenuItem alloc] initWithTitle:@"Rename" action:@selector(fileBrowserRename:)];
  73. UIMenuItem *deleteMenuItem = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(fileBrowserDelete:)];
  74. [UIMenuController sharedMenuController].menuItems = @[renameMenuItem, deleteMenuItem];
  75. }
  76. #pragma mark - FLEXFileBrowserSearchOperationDelegate
  77. - (void)fileBrowserSearchOperationResult:(NSArray *)searchResult size:(uint64_t)size
  78. {
  79. self.searchPaths = searchResult;
  80. self.searchPathsSize = @(size);
  81. [self.tableView reloadData];
  82. }
  83. #pragma mark - UISearchResultsUpdating
  84. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  85. {
  86. [self reloadDisplayedPaths];
  87. }
  88. #pragma mark - UISearchControllerDelegate
  89. - (void)willDismissSearchController:(UISearchController *)searchController
  90. {
  91. [self.operationQueue cancelAllOperations];
  92. [self reloadChildPaths];
  93. [self.tableView reloadData];
  94. }
  95. #pragma mark - Table view data source
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  97. {
  98. return 1;
  99. }
  100. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  101. {
  102. return self.searchController.isActive ? [self.searchPaths count] : [self.childPaths count];
  103. }
  104. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  105. {
  106. BOOL isSearchActive = self.searchController.isActive;
  107. NSNumber *currentSize = isSearchActive ? self.searchPathsSize : self.recursiveSize;
  108. NSArray *currentPaths = isSearchActive ? self.searchPaths : self.childPaths;
  109. NSString *sizeString = nil;
  110. if (!currentSize) {
  111. sizeString = @"Computing size…";
  112. } else {
  113. sizeString = [NSByteCountFormatter stringFromByteCount:[currentSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile];
  114. }
  115. return [NSString stringWithFormat:@"%lu files (%@)", (unsigned long)[currentPaths count], sizeString];
  116. }
  117. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  120. NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:NULL];
  121. BOOL isDirectory = [[attributes fileType] isEqual:NSFileTypeDirectory];
  122. NSString *subtitle = nil;
  123. if (isDirectory) {
  124. NSUInteger count = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:NULL] count];
  125. subtitle = [NSString stringWithFormat:@"%lu file%@", (unsigned long)count, (count == 1 ? @"" : @"s")];
  126. } else {
  127. NSString *sizeString = [NSByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleFile];
  128. subtitle = [NSString stringWithFormat:@"%@ - %@", sizeString, [attributes fileModificationDate]];
  129. }
  130. static NSString *textCellIdentifier = @"textCell";
  131. static NSString *imageCellIdentifier = @"imageCell";
  132. UITableViewCell *cell = nil;
  133. // Separate image and text only cells because otherwise the separator lines get out-of-whack on image cells reused with text only.
  134. BOOL showImagePreview = [FLEXUtility isImagePathExtension:[fullPath pathExtension]];
  135. NSString *cellIdentifier = showImagePreview ? imageCellIdentifier : textCellIdentifier;
  136. if (!cell) {
  137. cell = [[FLEXFileBrowserTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  138. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  139. cell.detailTextLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  140. cell.detailTextLabel.textColor = [UIColor grayColor];
  141. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  142. }
  143. NSString *cellTitle = [fullPath lastPathComponent];
  144. cell.textLabel.text = cellTitle;
  145. cell.detailTextLabel.text = subtitle;
  146. if (showImagePreview) {
  147. cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
  148. cell.imageView.image = [UIImage imageWithContentsOfFile:fullPath];
  149. }
  150. return cell;
  151. }
  152. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  153. {
  154. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  155. NSString *subpath = [fullPath lastPathComponent];
  156. BOOL isDirectory = NO;
  157. BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
  158. if (stillExists) {
  159. UIViewController *drillInViewController = nil;
  160. if (isDirectory) {
  161. drillInViewController = [[[self class] alloc] initWithPath:fullPath];
  162. } else if ([FLEXUtility isImagePathExtension:[fullPath pathExtension]]) {
  163. UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
  164. drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
  165. } else {
  166. // Special case keyed archives, json, and plists to get more readable data.
  167. NSString *prettyString = nil;
  168. if ([[subpath pathExtension] isEqual:@"archive"]) {
  169. prettyString = [[NSKeyedUnarchiver unarchiveObjectWithFile:fullPath] description];
  170. } else if ([[subpath pathExtension] isEqualToString:@"json"]) {
  171. prettyString = [FLEXUtility prettyJSONStringFromData:[NSData dataWithContentsOfFile:fullPath]];
  172. } else if ([[subpath pathExtension] isEqualToString:@"plist"]) {
  173. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  174. prettyString = [[NSPropertyListSerialization propertyListWithData:fileData options:0 format:NULL error:NULL] description];
  175. }
  176. if ([prettyString length] > 0) {
  177. drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
  178. } else if ([FLEXWebViewController supportsPathExtension:[subpath pathExtension]]) {
  179. drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
  180. } else if ([[subpath pathExtension] isEqualToString:@"db"]) {
  181. drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
  182. }
  183. else {
  184. NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:NULL];
  185. if ([fileString length] > 0) {
  186. drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
  187. }
  188. }
  189. }
  190. if (drillInViewController) {
  191. drillInViewController.title = [subpath lastPathComponent];
  192. [self.navigationController pushViewController:drillInViewController animated:YES];
  193. } else {
  194. [self openFileController:fullPath];
  195. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  196. }
  197. } else {
  198. [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
  199. [self reloadDisplayedPaths];
  200. }
  201. }
  202. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  203. {
  204. return YES;
  205. }
  206. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  207. {
  208. return action == @selector(fileBrowserDelete:) || action == @selector(fileBrowserRename:);
  209. }
  210. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  211. {
  212. // Empty, but has to exist for the menu to show
  213. // The table view only calls this method for actions in the UIResponderStandardEditActions informal protocol.
  214. // Since our actions are outside of that protocol, we need to manually handle the action forwarding from the cells.
  215. }
  216. #pragma mark - FLEXFileBrowserFileOperationControllerDelegate
  217. - (void)fileOperationControllerDidDismiss:(id<FLEXFileBrowserFileOperationController>)controller
  218. {
  219. [self reloadDisplayedPaths];
  220. }
  221. - (void)openFileController:(NSString *)fullPath
  222. {
  223. UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
  224. controller.URL = [[NSURL alloc] initFileURLWithPath:fullPath];
  225. [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  226. self.documentController = controller;
  227. }
  228. - (void)fileBrowserRename:(UITableViewCell *)sender
  229. {
  230. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  231. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  232. self.fileOperationController = [[FLEXFileBrowserFileRenameOperationController alloc] initWithPath:fullPath];
  233. self.fileOperationController.delegate = self;
  234. [self.fileOperationController show];
  235. }
  236. - (void)fileBrowserDelete:(UITableViewCell *)sender
  237. {
  238. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  239. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  240. self.fileOperationController = [[FLEXFileBrowserFileDeleteOperationController alloc] initWithPath:fullPath];
  241. self.fileOperationController.delegate = self;
  242. [self.fileOperationController show];
  243. }
  244. - (void)reloadDisplayedPaths
  245. {
  246. if (self.searchController.isActive) {
  247. [self reloadSearchPaths];
  248. } else {
  249. [self reloadChildPaths];
  250. }
  251. [self.tableView reloadData];
  252. }
  253. - (void)reloadChildPaths
  254. {
  255. NSMutableArray *childPaths = [NSMutableArray array];
  256. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.path error:NULL];
  257. for (NSString *subpath in subpaths) {
  258. [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
  259. }
  260. self.childPaths = childPaths;
  261. }
  262. - (void)reloadSearchPaths
  263. {
  264. self.searchPaths = nil;
  265. self.searchPathsSize = nil;
  266. //clear pre search request and start a new one
  267. [self.operationQueue cancelAllOperations];
  268. FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchController.searchBar.text];
  269. newOperation.delegate = self;
  270. [self.operationQueue addOperation:newOperation];
  271. }
  272. - (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath
  273. {
  274. return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
  275. }
  276. @end
  277. @implementation FLEXFileBrowserTableViewCell
  278. - (void)fileBrowserRename:(UIMenuController *)sender
  279. {
  280. id target = [self.nextResponder targetForAction:_cmd withSender:sender];
  281. [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
  282. }
  283. - (void)fileBrowserDelete:(UIMenuController *)sender
  284. {
  285. id target = [self.nextResponder targetForAction:_cmd withSender:sender];
  286. [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
  287. }
  288. @end