FLEXFileBrowserTableViewController.m 11 KB

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