FLEXFileBrowserTableViewController.m 15 KB

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