FLEXFileBrowserTableViewController.m 15 KB

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