FLEXFileBrowserFileOperationController.m 4.3 KB

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