FLEXFileBrowserFileOperationController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // FLEXFileBrowserFileOperationController.m
  3. // Flipboard
  4. //
  5. // Created by Daniel Rodriguez Troitino on 2/13/15.
  6. // Copyright (c) 2015 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXFileBrowserFileOperationController.h"
  9. @interface FLEXFileBrowserFileDeleteOperationController () <UIAlertViewDelegate>
  10. @property (nonatomic, copy, readonly) NSString *path;
  11. @end
  12. @implementation FLEXFileBrowserFileDeleteOperationController
  13. @synthesize delegate = _delegate;
  14. - (instancetype)init
  15. {
  16. return [self initWithPath:nil];
  17. }
  18. - (instancetype)initWithPath:(NSString *)path
  19. {
  20. self = [super init];
  21. if (self) {
  22. _path = path;
  23. }
  24. return self;
  25. }
  26. - (void)show
  27. {
  28. BOOL isDirectory = NO;
  29. BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:self.path isDirectory:&isDirectory];
  30. if (stillExists) {
  31. UIAlertView *deleteWarning = [[UIAlertView alloc]
  32. initWithTitle:[NSString stringWithFormat:@"Delete %@?", self.path.lastPathComponent]
  33. message:[NSString stringWithFormat:@"The %@ will be deleted. This operation cannot be undone", isDirectory ? @"directory" : @"file"]
  34. delegate:self
  35. cancelButtonTitle:@"Cancel"
  36. otherButtonTitles:@"Delete", nil];
  37. [deleteWarning show];
  38. } else {
  39. [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
  40. }
  41. }
  42. #pragma mark - UIAlertViewDelegate
  43. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  44. {
  45. if (buttonIndex == alertView.cancelButtonIndex) {
  46. // Nothing, just cancel
  47. } else if (buttonIndex == alertView.firstOtherButtonIndex) {
  48. [[NSFileManager defaultManager] removeItemAtPath:self.path error:NULL];
  49. }
  50. }
  51. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  52. {
  53. [self.delegate fileOperationControllerDidDismiss:self];
  54. }
  55. @end
  56. @interface FLEXFileBrowserFileRenameOperationController () <UIAlertViewDelegate>
  57. @property (nonatomic, copy, readonly) NSString *path;
  58. @end
  59. @implementation FLEXFileBrowserFileRenameOperationController
  60. @synthesize delegate = _delegate;
  61. - (instancetype)init
  62. {
  63. return [self initWithPath:nil];
  64. }
  65. - (instancetype)initWithPath:(NSString *)path
  66. {
  67. self = [super init];
  68. if (self) {
  69. _path = path;
  70. }
  71. return self;
  72. }
  73. - (void)show
  74. {
  75. BOOL isDirectory = NO;
  76. BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:self.path isDirectory:&isDirectory];
  77. if (stillExists) {
  78. UIAlertView *renameDialog = [[UIAlertView alloc]
  79. initWithTitle:[NSString stringWithFormat:@"Rename %@?", self.path.lastPathComponent]
  80. message:nil
  81. delegate:self
  82. cancelButtonTitle:@"Cancel"
  83. otherButtonTitles:@"Rename", nil];
  84. renameDialog.alertViewStyle = UIAlertViewStylePlainTextInput;
  85. UITextField *textField = [renameDialog textFieldAtIndex:0];
  86. textField.placeholder = @"New file name";
  87. textField.text = self.path.lastPathComponent;
  88. [renameDialog show];
  89. } else {
  90. [[[UIAlertView alloc] initWithTitle:@"File Removed" message:@"The file at the specified path no longer exists." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
  91. }
  92. }
  93. #pragma mark - UIAlertViewDelegate
  94. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  95. {
  96. if (buttonIndex == alertView.cancelButtonIndex) {
  97. // Nothing, just cancel
  98. } else if (buttonIndex == alertView.firstOtherButtonIndex) {
  99. NSString *newFileName = [alertView textFieldAtIndex:0].text;
  100. NSString *newPath = [[self.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFileName];
  101. [[NSFileManager defaultManager] moveItemAtPath:self.path toPath:newPath error:NULL];
  102. }
  103. }
  104. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  105. {
  106. [self.delegate fileOperationControllerDidDismiss:self];
  107. }
  108. @end