FLEXFileBrowserTableViewController.m 8.0 KB

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