VLCDeletionCapableViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. * Felix Paul Kühne <fkuehne # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCDeletionCapableViewController.h"
  13. @interface VLCDeletionCapableViewController ()
  14. @property (nonatomic) UITapGestureRecognizer *playPausePressRecognizer;
  15. @property (nonatomic) UITapGestureRecognizer *cancelRecognizer;
  16. @property (nonatomic) NSIndexPath *currentlyFocusedIndexPath;
  17. @property (nonatomic) NSTimer *hintTimer;
  18. @end
  19. @implementation VLCDeletionCapableViewController
  20. - (void)viewDidLoad
  21. {
  22. [super viewDidLoad];
  23. UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(startEditMode)];
  24. recognizer.allowedPressTypes = @[@(UIPressTypeSelect)];
  25. recognizer.minimumPressDuration = 1.0;
  26. [self.view addGestureRecognizer:recognizer];
  27. UITapGestureRecognizer *cancelRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditMode)];
  28. cancelRecognizer.allowedPressTypes = @[@(UIPressTypeSelect),@(UIPressTypeMenu)];
  29. self.cancelRecognizer = cancelRecognizer;
  30. [self.view addGestureRecognizer:cancelRecognizer];
  31. UITapGestureRecognizer *playPauseRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePlayPausePress)];
  32. playPauseRecognizer.allowedPressTypes = @[@(UIPressTypePlayPause)];
  33. self.playPausePressRecognizer = playPauseRecognizer;
  34. [self.view addGestureRecognizer:playPauseRecognizer];
  35. }
  36. - (void)handlePlayPausePress
  37. {
  38. NSString *fileToDelete = self.itemToDelete;
  39. if (fileToDelete == nil)
  40. return;
  41. NSIndexPath *indexPathToDelete = self.indexPathToDelete;
  42. NSString *title = fileToDelete.lastPathComponent;
  43. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
  44. message:nil
  45. preferredStyle:UIAlertControllerStyleAlert];
  46. UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DELETE", nil)
  47. style:UIAlertActionStyleDestructive
  48. handler:^(UIAlertAction * _Nonnull action) {
  49. [self deleteFileAtIndex:indexPathToDelete];
  50. }];
  51. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  52. style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  53. self.editing = NO;
  54. }];
  55. [alertController addAction:deleteAction];
  56. [alertController addAction:cancelAction];
  57. [self presentViewController:alertController animated:YES completion:nil];
  58. }
  59. - (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
  60. {
  61. [self.hintTimer invalidate];
  62. self.hintTimer = nil;
  63. [self animateDeletHintToVisibility:NO];
  64. }
  65. - (void)animateDeletHintToVisibility:(BOOL)visible
  66. {
  67. const NSTimeInterval duration = 0.5;
  68. UIView *hintView = self.deleteHintView;
  69. if (hintView.hidden) {
  70. hintView.alpha = 0.0;
  71. }
  72. if (hintView.alpha == 0.0) {
  73. hintView.hidden = NO;
  74. }
  75. const CGFloat targetAlpha = visible ? 1.0 : 0.0;
  76. [UIView animateWithDuration:duration
  77. delay:0
  78. options:UIViewAnimationOptionBeginFromCurrentState
  79. animations:^{
  80. hintView.alpha = targetAlpha;
  81. }
  82. completion:^(BOOL finished) {
  83. if (hintView.alpha == 0.0) {
  84. hintView.hidden = YES;
  85. }
  86. }];
  87. }
  88. - (void)hintTimerFired:(NSTimer *)timer
  89. {
  90. const NSTimeInterval waitUntilHideInterval = 5.0;
  91. NSNumber *userInfo = [timer userInfo];
  92. BOOL shouldShow = [userInfo isKindOfClass:[NSNumber class]] && [userInfo boolValue];
  93. [self animateDeletHintToVisibility:shouldShow];
  94. if (shouldShow) {
  95. [self.hintTimer invalidate];
  96. self.hintTimer = [NSTimer scheduledTimerWithTimeInterval:waitUntilHideInterval target:self selector:@selector(hintTimerFired:) userInfo:@(NO) repeats:NO];
  97. }
  98. }
  99. - (void)startEditMode
  100. {
  101. self.editing = YES;
  102. }
  103. - (void)endEditMode
  104. {
  105. self.editing = NO;
  106. }
  107. - (void)setEditing:(BOOL)editing
  108. {
  109. [super setEditing:editing];
  110. if (editing) {
  111. [self.hintTimer invalidate];
  112. self.hintTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(hintTimerFired:) userInfo:@(YES) repeats:NO];
  113. } else {
  114. [self.hintTimer invalidate];
  115. self.hintTimer = nil;
  116. [self animateDeletHintToVisibility:NO];
  117. }
  118. self.cancelRecognizer.enabled = editing;
  119. self.playPausePressRecognizer.enabled = editing;
  120. }
  121. @end