FLEXFileBrowserTableViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. NSString *pathExtension = [subpath pathExtension];
  157. BOOL isDirectory = NO;
  158. BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
  159. if (stillExists) {
  160. UIViewController *drillInViewController = nil;
  161. if (isDirectory) {
  162. drillInViewController = [[[self class] alloc] initWithPath:fullPath];
  163. } else if ([FLEXUtility isImagePathExtension:pathExtension]) {
  164. UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
  165. drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
  166. } else {
  167. // Special case keyed archives, json, and plists to get more readable data.
  168. NSString *prettyString = nil;
  169. if ([pathExtension isEqual:@"archive"] || [pathExtension isEqual:@"coded"]) {
  170. prettyString = [[NSKeyedUnarchiver unarchiveObjectWithFile:fullPath] description];
  171. } else if ([pathExtension isEqualToString:@"json"]) {
  172. prettyString = [FLEXUtility prettyJSONStringFromData:[NSData dataWithContentsOfFile:fullPath]];
  173. } else if ([pathExtension isEqualToString:@"plist"]) {
  174. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  175. prettyString = [[NSPropertyListSerialization propertyListWithData:fileData options:0 format:NULL error:NULL] description];
  176. }
  177. if ([prettyString length] > 0) {
  178. drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
  179. } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
  180. drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
  181. } else if ([FLEXTableListViewController supportsExtension:subpath.pathExtension]) {
  182. drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
  183. }
  184. else {
  185. NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:NULL];
  186. if ([fileString length] > 0) {
  187. drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
  188. }
  189. }
  190. }
  191. if (drillInViewController) {
  192. drillInViewController.title = [subpath lastPathComponent];
  193. [self.navigationController pushViewController:drillInViewController animated:YES];
  194. } else {
  195. [self openFileController:fullPath];
  196. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  197. }
  198. } else {
  199. [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
  200. [self reloadDisplayedPaths];
  201. }
  202. }
  203. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  204. {
  205. return YES;
  206. }
  207. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  208. {
  209. return action == @selector(fileBrowserDelete:) || action == @selector(fileBrowserRename:);
  210. }
  211. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  212. {
  213. // Empty, but has to exist for the menu to show
  214. // The table view only calls this method for actions in the UIResponderStandardEditActions informal protocol.
  215. // Since our actions are outside of that protocol, we need to manually handle the action forwarding from the cells.
  216. }
  217. #pragma mark - FLEXFileBrowserFileOperationControllerDelegate
  218. - (void)fileOperationControllerDidDismiss:(id<FLEXFileBrowserFileOperationController>)controller
  219. {
  220. [self reloadDisplayedPaths];
  221. }
  222. - (void)openFileController:(NSString *)fullPath
  223. {
  224. UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
  225. controller.URL = [[NSURL alloc] initFileURLWithPath:fullPath];
  226. [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  227. self.documentController = controller;
  228. }
  229. - (void)fileBrowserRename:(UITableViewCell *)sender
  230. {
  231. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  232. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  233. self.fileOperationController = [[FLEXFileBrowserFileRenameOperationController alloc] initWithPath:fullPath];
  234. self.fileOperationController.delegate = self;
  235. [self.fileOperationController show];
  236. }
  237. - (void)fileBrowserDelete:(UITableViewCell *)sender
  238. {
  239. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  240. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  241. self.fileOperationController = [[FLEXFileBrowserFileDeleteOperationController alloc] initWithPath:fullPath];
  242. self.fileOperationController.delegate = self;
  243. [self.fileOperationController show];
  244. }
  245. - (void)reloadDisplayedPaths
  246. {
  247. if (self.searchController.isActive) {
  248. [self reloadSearchPaths];
  249. } else {
  250. [self reloadChildPaths];
  251. }
  252. [self.tableView reloadData];
  253. }
  254. - (void)reloadChildPaths
  255. {
  256. NSMutableArray *childPaths = [NSMutableArray array];
  257. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.path error:NULL];
  258. for (NSString *subpath in subpaths) {
  259. [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
  260. }
  261. self.childPaths = childPaths;
  262. }
  263. - (void)reloadSearchPaths
  264. {
  265. self.searchPaths = nil;
  266. self.searchPathsSize = nil;
  267. //clear pre search request and start a new one
  268. [self.operationQueue cancelAllOperations];
  269. FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchController.searchBar.text];
  270. newOperation.delegate = self;
  271. [self.operationQueue addOperation:newOperation];
  272. }
  273. - (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath
  274. {
  275. return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
  276. }
  277. @end
  278. @implementation FLEXFileBrowserTableViewCell
  279. - (void)fileBrowserRename:(UIMenuController *)sender
  280. {
  281. id target = [self.nextResponder targetForAction:_cmd withSender:sender];
  282. [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
  283. }
  284. - (void)fileBrowserDelete:(UIMenuController *)sender
  285. {
  286. id target = [self.nextResponder targetForAction:_cmd withSender:sender];
  287. [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
  288. }
  289. @end