FLEXFileBrowserTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // FLEXFileBrowserTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/9/14.
  6. //
  7. //
  8. #import "FLEXFileBrowserTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXWebViewController.h"
  11. #import "FLEXImagePreviewViewController.h"
  12. @interface FLEXFileBrowserTableViewController ()
  13. @property (nonatomic, copy) NSString *path;
  14. @property (nonatomic, copy) NSArray *childPaths;
  15. @property (nonatomic, strong) NSArray *searchPaths;
  16. @property (nonatomic, strong) NSNumber *recursiveSize;
  17. @property (nonatomic, strong) NSNumber *searchPathsSize;
  18. @property (nonatomic, strong) UISearchDisplayController *searchController;
  19. @property (nonatomic) NSOperationQueue *operationQueue;
  20. @property (nonatomic, strong) UIDocumentInteractionController *documentController;
  21. @end
  22. @implementation FLEXFileBrowserTableViewController
  23. - (id)initWithStyle:(UITableViewStyle)style
  24. {
  25. return [self initWithPath:NSHomeDirectory()];
  26. }
  27. - (id)initWithPath:(NSString *)path
  28. {
  29. self = [super initWithStyle:UITableViewStyleGrouped];
  30. if (self) {
  31. self.path = path;
  32. self.title = [path lastPathComponent];
  33. self.operationQueue = [NSOperationQueue new];
  34. //add search controller
  35. UISearchBar *searchBar = [UISearchBar new];
  36. [searchBar sizeToFit];
  37. self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
  38. self.searchController.delegate = self;
  39. self.searchController.searchResultsDataSource = self;
  40. self.searchController.searchResultsDelegate = self;
  41. self.tableView.tableHeaderView = self.searchController.searchBar;
  42. //computing path size
  43. FLEXFileBrowserTableViewController *__weak weakSelf = self;
  44. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  45. NSFileManager *fileManager = [NSFileManager defaultManager];
  46. NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
  47. uint64_t totalSize = [attributes fileSize];
  48. for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
  49. attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
  50. totalSize += [attributes fileSize];
  51. // Bail if the interested view controller has gone away.
  52. if (!weakSelf) {
  53. return;
  54. }
  55. }
  56. dispatch_async(dispatch_get_main_queue(), ^{
  57. FLEXFileBrowserTableViewController *__strong strongSelf = weakSelf;
  58. strongSelf.recursiveSize = @(totalSize);
  59. [strongSelf.tableView reloadData];
  60. });
  61. });
  62. self.childPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
  63. }
  64. return self;
  65. }
  66. #pragma mark - FLEXFileBrowserSearchOperationDelegate
  67. - (void)fileBrowserSearchOperationResult:(NSArray *)searchResult size:(uint64_t)size
  68. {
  69. self.searchPaths = searchResult;
  70. self.searchPathsSize = @(size);
  71. [self.searchController.searchResultsTableView reloadData];
  72. }
  73. #pragma mark - UISearchDisplayDelegate
  74. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  75. {
  76. self.searchPaths = nil;
  77. self.searchPathsSize = nil;
  78. //clear pre search request and start a new one
  79. [self.operationQueue cancelAllOperations];
  80. FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:searchString];
  81. newOperation.delegate = self;
  82. [self.operationQueue addOperation:newOperation];
  83. return YES;
  84. }
  85. - (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView
  86. {
  87. //confirm to clear all operations
  88. [self.operationQueue cancelAllOperations];
  89. }
  90. #pragma mark - Table view data source
  91. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  92. {
  93. return 1;
  94. }
  95. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  96. {
  97. if (tableView == self.tableView) {
  98. return [self.childPaths count];
  99. } else {
  100. return [self.searchPaths count];
  101. }
  102. }
  103. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  104. {
  105. NSNumber *currentSize = nil;
  106. NSArray *currentPaths = nil;
  107. if (tableView == self.tableView) {
  108. currentSize = self.recursiveSize;
  109. currentPaths = self.childPaths;
  110. } else {
  111. currentSize = self.searchPathsSize;
  112. currentPaths = self.searchPaths;
  113. }
  114. NSString *sizeString = nil;
  115. if (!currentSize) {
  116. sizeString = @"Computing size…";
  117. } else {
  118. sizeString = [NSByteCountFormatter stringFromByteCount:[currentSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile];
  119. }
  120. return [NSString stringWithFormat:@"%lu files (%@)", (unsigned long)[currentPaths count], sizeString];
  121. }
  122. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  123. {
  124. NSString *fullPath = nil;
  125. if (tableView == self.tableView) {
  126. NSString *subpath = [self.childPaths objectAtIndex:indexPath.row];
  127. fullPath = [self.path stringByAppendingPathComponent:subpath];
  128. } else {
  129. fullPath = [self.searchPaths objectAtIndex:indexPath.row];
  130. }
  131. NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:NULL];
  132. BOOL isDirectory = [[attributes fileType] isEqual:NSFileTypeDirectory];
  133. NSString *subtitle = nil;
  134. if (isDirectory) {
  135. NSUInteger count = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:NULL] count];
  136. subtitle = [NSString stringWithFormat:@"%lu file%@", (unsigned long)count, (count == 1 ? @"" : @"s")];
  137. } else {
  138. NSString *sizeString = [NSByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleFile];
  139. subtitle = [NSString stringWithFormat:@"%@ - %@", sizeString, [attributes fileModificationDate]];
  140. }
  141. static NSString *textCellIdentifier = @"textCell";
  142. static NSString *imageCellIdentifier = @"imageCell";
  143. UITableViewCell *cell = nil;
  144. // Separate image and text only cells because otherwise the separator lines get out-of-whack on image cells reused with text only.
  145. BOOL showImagePreview = [FLEXUtility isImagePathExtension:[fullPath pathExtension]];
  146. NSString *cellIdentifier = showImagePreview ? imageCellIdentifier : textCellIdentifier;
  147. if (!cell) {
  148. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  149. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  150. cell.detailTextLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  151. cell.detailTextLabel.textColor = [UIColor grayColor];
  152. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  153. }
  154. NSString *cellTitle = [fullPath lastPathComponent];
  155. cell.textLabel.text = cellTitle;
  156. cell.detailTextLabel.text = subtitle;
  157. if (showImagePreview) {
  158. cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
  159. cell.imageView.image = [UIImage imageWithContentsOfFile:fullPath];
  160. }
  161. return cell;
  162. }
  163. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  164. {
  165. NSString *subpath = nil;
  166. NSString *fullPath = nil;
  167. if (tableView == self.tableView) {
  168. subpath = [self.childPaths objectAtIndex:indexPath.row];
  169. fullPath = [self.path stringByAppendingPathComponent:subpath];
  170. } else {
  171. fullPath = [self.searchPaths objectAtIndex:indexPath.row];
  172. subpath = [fullPath lastPathComponent];
  173. }
  174. BOOL isDirectory = NO;
  175. BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
  176. if (stillExists) {
  177. UIViewController *drillInViewController = nil;
  178. if (isDirectory) {
  179. drillInViewController = [[[self class] alloc] initWithPath:fullPath];
  180. } else if ([FLEXUtility isImagePathExtension:[fullPath pathExtension]]) {
  181. UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
  182. drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
  183. } else {
  184. // Special case keyed archives, json, and plists to get more readable data.
  185. NSString *prettyString = nil;
  186. if ([[subpath pathExtension] isEqual:@"archive"]) {
  187. prettyString = [[NSKeyedUnarchiver unarchiveObjectWithFile:fullPath] description];
  188. } else if ([[subpath pathExtension] isEqualToString:@"json"]) {
  189. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  190. id jsonObject = [NSJSONSerialization JSONObjectWithData:fileData options:0 error:NULL];
  191. prettyString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:NULL] encoding:NSUTF8StringEncoding];
  192. } else if ([[subpath pathExtension] isEqualToString:@"plist"]) {
  193. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  194. prettyString = [[NSPropertyListSerialization propertyListWithData:fileData options:0 format:NULL error:NULL] description];
  195. }
  196. if ([prettyString length] > 0) {
  197. drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
  198. } else if ([FLEXWebViewController supportsPathExtension:[subpath pathExtension]]) {
  199. drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
  200. } else {
  201. NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:NULL];
  202. if ([fileString length] > 0) {
  203. drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
  204. }
  205. }
  206. }
  207. if (drillInViewController) {
  208. drillInViewController.title = [subpath lastPathComponent];
  209. [self.navigationController pushViewController:drillInViewController animated:YES];
  210. } else {
  211. [self openFileController:fullPath];
  212. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  213. }
  214. } else {
  215. [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
  216. }
  217. }
  218. - (void)openFileController:(NSString *)fullPath
  219. {
  220. UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
  221. controller.URL = [[NSURL alloc] initFileURLWithPath:fullPath];
  222. [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  223. self.documentController = controller;
  224. }
  225. @end