VLCDeletionCapableViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // NO-OP, implemented by subclass
  62. }
  63. - (void)animateDeletHintToVisibility:(BOOL)visible
  64. {
  65. const NSTimeInterval duration = 0.5;
  66. UIView *hintView = self.deleteHintView;
  67. if (hintView.hidden) {
  68. hintView.alpha = 0.0;
  69. }
  70. if (hintView.alpha == 0.0) {
  71. hintView.hidden = NO;
  72. }
  73. const CGFloat targetAlpha = visible ? 1.0 : 0.0;
  74. [UIView animateWithDuration:duration
  75. delay:0
  76. options:UIViewAnimationOptionBeginFromCurrentState
  77. animations:^{
  78. hintView.alpha = targetAlpha;
  79. }
  80. completion:^(BOOL finished) {
  81. if (hintView.alpha == 0.0) {
  82. hintView.hidden = YES;
  83. }
  84. }];
  85. }
  86. - (void)hintTimerFired:(NSTimer *)timer
  87. {
  88. const NSTimeInterval waitUntilHideInterval = 5.0;
  89. NSNumber *userInfo = [timer userInfo];
  90. BOOL shouldShow = [userInfo isKindOfClass:[NSNumber class]] && [userInfo boolValue];
  91. [self animateDeletHintToVisibility:shouldShow];
  92. if (shouldShow) {
  93. [self.hintTimer invalidate];
  94. self.hintTimer = [NSTimer scheduledTimerWithTimeInterval:waitUntilHideInterval target:self selector:@selector(hintTimerFired:) userInfo:@(NO) repeats:NO];
  95. }
  96. }
  97. - (void)startEditMode
  98. {
  99. self.editing = YES;
  100. }
  101. - (void)endEditMode
  102. {
  103. self.editing = NO;
  104. }
  105. - (void)setEditing:(BOOL)editing
  106. {
  107. [super setEditing:editing];
  108. if (editing) {
  109. [self.hintTimer invalidate];
  110. self.hintTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(hintTimerFired:) userInfo:@(YES) repeats:NO];
  111. } else {
  112. [self.hintTimer invalidate];
  113. self.hintTimer = nil;
  114. [self animateDeletHintToVisibility:NO];
  115. }
  116. self.cancelRecognizer.enabled = editing;
  117. self.playPausePressRecognizer.enabled = editing;
  118. }
  119. @end