FLEXFileBrowserTableViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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) NSNumber *recursiveSize;
  16. @end
  17. @implementation FLEXFileBrowserTableViewController
  18. - (id)initWithStyle:(UITableViewStyle)style
  19. {
  20. return [self initWithPath:NSHomeDirectory()];
  21. }
  22. - (id)initWithPath:(NSString *)path
  23. {
  24. self = [super initWithStyle:UITableViewStyleGrouped];
  25. if (self) {
  26. self.path = path;
  27. self.title = [path lastPathComponent];
  28. FLEXFileBrowserTableViewController *__weak weakSelf = self;
  29. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  30. NSFileManager *fileManager = [[NSFileManager alloc] init];
  31. NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
  32. uint64_t totalSize = [attributes fileSize];
  33. for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
  34. attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
  35. totalSize += [attributes fileSize];
  36. // Bail if the interested view controller has gone away.
  37. if (!weakSelf) {
  38. return;
  39. }
  40. }
  41. dispatch_async(dispatch_get_main_queue(), ^{
  42. weakSelf.recursiveSize = @(totalSize);
  43. [weakSelf.tableView reloadData];
  44. });
  45. });
  46. self.childPaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
  47. }
  48. return self;
  49. }
  50. #pragma mark - Table view data source
  51. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  52. {
  53. return 1;
  54. }
  55. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  56. {
  57. return [self.childPaths count];
  58. }
  59. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  60. {
  61. NSString *sizeString = nil;
  62. if (!self.recursiveSize) {
  63. sizeString = @"Computing size…";
  64. } else {
  65. sizeString = [NSByteCountFormatter stringFromByteCount:[self.recursiveSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile];
  66. }
  67. return [NSString stringWithFormat:@"%lu files (%@)", (unsigned long)[self.childPaths count], sizeString];
  68. }
  69. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  70. {
  71. NSString *subpath = [self.childPaths objectAtIndex:indexPath.row];
  72. NSString *fullPath = [self.path stringByAppendingPathComponent:subpath];
  73. NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:NULL];
  74. BOOL isDirectory = [[attributes fileType] isEqual:NSFileTypeDirectory];
  75. NSString *subtitle = nil;
  76. if (isDirectory) {
  77. NSUInteger count = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:NULL] count];
  78. subtitle = [NSString stringWithFormat:@"%lu file%@", (unsigned long)count, (count == 1 ? @"" : @"s")];
  79. } else {
  80. NSString *sizeString = [NSByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleFile];
  81. subtitle = [NSString stringWithFormat:@"%@ - %@", sizeString, [attributes fileModificationDate]];
  82. }
  83. static NSString *textCellIdentifier = @"textCell";
  84. static NSString *imageCellIdentifier = @"imageCell";
  85. UITableViewCell *cell = nil;
  86. // Separate image and text only cells because otherwise the separator lines get out-of-whack on image cells reused with text only.
  87. BOOL showImagePreview = [FLEXUtility isImagePathExtension:[fullPath pathExtension]];
  88. NSString *cellIdentifier = showImagePreview ? imageCellIdentifier : textCellIdentifier;
  89. if (!cell) {
  90. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  91. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  92. cell.detailTextLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  93. cell.detailTextLabel.textColor = [UIColor grayColor];
  94. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  95. }
  96. NSString *cellTitle = [subpath lastPathComponent];
  97. cell.textLabel.text = cellTitle;
  98. cell.detailTextLabel.text = subtitle;
  99. if (showImagePreview) {
  100. cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
  101. cell.imageView.image = [UIImage imageWithContentsOfFile:fullPath];
  102. }
  103. return cell;
  104. }
  105. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. NSString *subpath = [self.childPaths objectAtIndex:indexPath.row];
  108. NSString *fullPath = [self.path stringByAppendingPathComponent:subpath];
  109. BOOL isDirectory = NO;
  110. BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
  111. if (stillExists) {
  112. UIViewController *drillInViewController = nil;
  113. if (isDirectory) {
  114. drillInViewController = [[[self class] alloc] initWithPath:fullPath];
  115. } else if ([FLEXUtility isImagePathExtension:[fullPath pathExtension]]) {
  116. UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
  117. drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
  118. } else {
  119. // Special case keyed archives, json, and plists to get more readable data.
  120. NSString *prettyString = nil;
  121. if ([[subpath pathExtension] isEqual:@"archive"]) {
  122. prettyString = [[NSKeyedUnarchiver unarchiveObjectWithFile:fullPath] description];
  123. } else if ([[subpath pathExtension] isEqualToString:@"json"]) {
  124. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  125. id jsonObject = [NSJSONSerialization JSONObjectWithData:fileData options:0 error:NULL];
  126. prettyString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:NULL] encoding:NSUTF8StringEncoding];
  127. } else if ([[subpath pathExtension] isEqualToString:@"plist"]) {
  128. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  129. prettyString = [[NSPropertyListSerialization propertyListWithData:fileData options:0 format:NULL error:NULL] description];
  130. }
  131. if ([prettyString length] > 0) {
  132. drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
  133. } else if ([FLEXWebViewController supportsPathExtension:[subpath pathExtension]]) {
  134. drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
  135. } else {
  136. NSString *fileString = [NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:NULL];
  137. if ([fileString length] > 0) {
  138. drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
  139. }
  140. }
  141. }
  142. if (drillInViewController) {
  143. drillInViewController.title = [subpath lastPathComponent];
  144. [self.navigationController pushViewController:drillInViewController animated:YES];
  145. } else {
  146. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  147. }
  148. } else {
  149. [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
  150. }
  151. }
  152. @end