You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.5 KiB

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