FLEXFileBrowserTableViewController.m 15 KB

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