DBFilesTableViewController.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // The MIT License
  2. //
  3. // Copyright (c) 2016 Dariusz Bukowski
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "DBFilesTableViewController.h"
  23. #import "NSBundle+DBDebugToolkit.h"
  24. #import "UILabel+DBDebugToolkit.h"
  25. #import "DBFileTableViewCell.h"
  26. static NSString *const DBFilesTableViewControllerDirectoryCellIdentifier = @"DirectoryCell";
  27. static NSString *const DBFilesTableViewControllerFileCellIdentifier = @"FileCell";
  28. static NSString *const DBFilesTableViewControllerSkippedDirectory = @"DBDebugToolkit";
  29. static const NSInteger DBFilesTableViewControllerNextSizeAbbreviationThreshold = 1024;
  30. @interface DBFilesTableViewController ()
  31. @property (nonatomic, strong) NSMutableArray *subdirectories;
  32. @property (nonatomic, strong) NSMutableArray *files;
  33. @property (nonatomic, strong) UILabel *backgroundLabel;
  34. @property (nonatomic, weak) IBOutlet UILabel *pathLabel;
  35. @end
  36. @implementation DBFilesTableViewController
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. NSBundle *bundle = [NSBundle debugToolkitBundle];
  40. [self.tableView registerNib:[UINib nibWithNibName:@"DBFileTableViewCell" bundle:bundle]
  41. forCellReuseIdentifier:DBFilesTableViewControllerFileCellIdentifier];
  42. self.tableView.tableFooterView = [UIView new];
  43. [self setupBackgroundLabel];
  44. [self setupContents];
  45. }
  46. - (void)setupContents {
  47. self.pathLabel.text = self.path ? [self.path stringByReplacingOccurrencesOfString:NSHomeDirectory() withString:@""] : @"/";
  48. if (self.path == nil) {
  49. self.path = NSHomeDirectory();
  50. }
  51. NSMutableArray *subdirectories = [NSMutableArray array];
  52. NSMutableArray *files = [NSMutableArray array];
  53. NSError *error;
  54. NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.path error:&error];
  55. for (NSString *element in directoryContent) {
  56. if ([element isEqualToString:DBFilesTableViewControllerSkippedDirectory]) {
  57. continue;
  58. }
  59. BOOL isDirectory;
  60. NSString *fullElementPath = [self.path stringByAppendingPathComponent:element];
  61. [[NSFileManager defaultManager] fileExistsAtPath:fullElementPath isDirectory:&isDirectory];
  62. if (isDirectory) {
  63. [subdirectories addObject:element];
  64. } else {
  65. [files addObject:element];
  66. }
  67. }
  68. self.subdirectories = [NSMutableArray arrayWithArray:[subdirectories sortedArrayUsingSelector:@selector(compare:)]];
  69. self.files = [NSMutableArray arrayWithArray:[files sortedArrayUsingSelector:@selector(compare:)]];
  70. [self refreshBackgroundLabel];
  71. }
  72. #pragma mark - Background label
  73. - (void)setupBackgroundLabel {
  74. self.backgroundLabel = [UILabel tableViewBackgroundLabel];
  75. self.tableView.backgroundView = self.backgroundLabel;
  76. }
  77. - (void)refreshBackgroundLabel {
  78. self.backgroundLabel.text = self.subdirectories.count + self.files.count > 0 ? @"" : @"This directory is empty.";
  79. }
  80. #pragma mark - UITableViewDataSource
  81. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  82. return 2;
  83. }
  84. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  85. return section == 0 ? self.subdirectories.count : self.files.count;
  86. }
  87. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  88. if (indexPath.section == 0) {
  89. UITableViewCell *subdirectoryCell = [tableView dequeueReusableCellWithIdentifier:DBFilesTableViewControllerDirectoryCellIdentifier];
  90. subdirectoryCell.textLabel.text = self.subdirectories[indexPath.row];
  91. return subdirectoryCell;
  92. } else {
  93. NSString *fileName = self.files[indexPath.row];
  94. DBFileTableViewCell *fileCell = [tableView dequeueReusableCellWithIdentifier:DBFilesTableViewControllerFileCellIdentifier];
  95. fileCell.nameLabel.text = fileName;
  96. fileCell.sizeLabel.text = [self sizeStringForFileWithName:fileName];
  97. return fileCell;
  98. }
  99. }
  100. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  101. NSString *fullPath = [self fullPathForElementWithIndexPath:indexPath];
  102. return [[NSFileManager defaultManager] isDeletableFileAtPath:fullPath];
  103. }
  104. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  105. if (editingStyle == UITableViewCellEditingStyleDelete) {
  106. NSString *fullPath = [self fullPathForElementWithIndexPath:indexPath];
  107. NSError *error;
  108. [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
  109. if (error) {
  110. [self presentAlertWithError:error];
  111. } else {
  112. [self removeElementFromDataSourceWithIndexPath:indexPath];
  113. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  114. [self refreshBackgroundLabel];
  115. }
  116. }
  117. }
  118. #pragma mark - UITableViewDelegate
  119. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  120. if (indexPath.section == 0) {
  121. NSString *subdirectoryName = self.subdirectories[indexPath.row];
  122. NSBundle *bundle = [NSBundle debugToolkitBundle];
  123. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"DBFilesTableViewController" bundle:bundle];
  124. DBFilesTableViewController *filesTableViewController = [storyboard instantiateInitialViewController];
  125. filesTableViewController.path = [self.path stringByAppendingPathComponent:subdirectoryName];
  126. filesTableViewController.title = subdirectoryName;
  127. [self.navigationController pushViewController:filesTableViewController animated:YES];
  128. } else {
  129. NSString *filePath = [self.path stringByAppendingPathComponent:self.files[indexPath.row]];
  130. NSURL *fileURL = [NSURL fileURLWithPath:filePath];
  131. UIActivityViewController *activity = [[UIActivityViewController alloc]
  132. initWithActivityItems:@[fileURL]
  133. applicationActivities:nil];
  134. UIPopoverPresentationController *popover = activity.popoverPresentationController;
  135. if (popover) {
  136. popover.sourceView = tableView;
  137. popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
  138. }
  139. [self presentViewController:activity animated:YES completion:nil];
  140. }
  141. }
  142. #pragma mark - File size
  143. - (NSString *)sizeStringForFileWithName:(NSString *)fileName {
  144. NSString *fullPath = [self.path stringByAppendingPathComponent:fileName];
  145. unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil] fileSize];
  146. NSArray *sizeUnitAbbreviations = [NSArray arrayWithObjects:@"B", @"KB", @"MB", @"GB", nil];
  147. for (int abbreviationIndex = 0; abbreviationIndex < sizeUnitAbbreviations.count; abbreviationIndex++) {
  148. if (fileSize < DBFilesTableViewControllerNextSizeAbbreviationThreshold) {
  149. return [NSString stringWithFormat:@"%llu%@", fileSize, sizeUnitAbbreviations[abbreviationIndex]];
  150. }
  151. fileSize /= DBFilesTableViewControllerNextSizeAbbreviationThreshold;
  152. }
  153. return nil;
  154. }
  155. #pragma mark - Element paths
  156. - (NSString *)fullPathForElementWithIndexPath:(NSIndexPath *)indexPath {
  157. NSString *elementName = indexPath.section == 0 ? self.subdirectories[indexPath.row] : self.files[indexPath.row];
  158. return [self.path stringByAppendingPathComponent:elementName];
  159. }
  160. - (void)removeElementFromDataSourceWithIndexPath:(NSIndexPath *)indexPath {
  161. NSMutableArray *affectedArray = indexPath.section == 0 ? self.subdirectories : self.files;
  162. [affectedArray removeObjectAtIndex:indexPath.row];
  163. }
  164. #pragma mark - Alert
  165. - (void)presentAlertWithError:(NSError *)error {
  166. UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Deletion error"
  167. message:[error localizedDescription]
  168. preferredStyle:UIAlertControllerStyleAlert];
  169. UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
  170. style:UIAlertActionStyleDefault
  171. handler:nil];
  172. [alert addAction:defaultAction];
  173. [self presentViewController:alert animated:YES completion:nil];
  174. }
  175. @end