VLCDeletionCapableViewController.m 5.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Tobias Conradi <videolan # tobias-conradi.de>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCDeletionCapableViewController.h"

@interface VLCDeletionCapableViewController ()
@property (nonatomic) UITapGestureRecognizer *playPausePressRecognizer;
@property (nonatomic) UITapGestureRecognizer *cancelRecognizer;
@property (nonatomic) NSIndexPath *currentlyFocusedIndexPath;
@property (nonatomic) NSTimer *hintTimer;

@end

@implementation VLCDeletionCapableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(startEditMode)];
    recognizer.allowedPressTypes = @[@(UIPressTypeSelect)];
    recognizer.minimumPressDuration = 1.0;
    [self.view addGestureRecognizer:recognizer];

    UITapGestureRecognizer *cancelRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditMode)];
    cancelRecognizer.allowedPressTypes = @[@(UIPressTypeSelect),@(UIPressTypeMenu)];
    self.cancelRecognizer = cancelRecognizer;
    [self.view addGestureRecognizer:cancelRecognizer];

    UITapGestureRecognizer *playPauseRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePlayPausePress)];
    playPauseRecognizer.allowedPressTypes = @[@(UIPressTypePlayPause)];
    self.playPausePressRecognizer = playPauseRecognizer;
    [self.view addGestureRecognizer:playPauseRecognizer];
}

- (void)handlePlayPausePress
{
    NSString *fileToDelete = self.itemToDelete;
    if (fileToDelete == nil)
        return;
    NSIndexPath *indexPathToDelete = self.indexPathToDelete;

    NSString *title = fileToDelete.lastPathComponent;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DELETE", nil)
                                                           style:UIAlertActionStyleDestructive
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             [self deleteFileAtIndex:indexPathToDelete];
                                                         }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
                                                           style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                                                               self.editing = NO;
                                                           }];

    [alertController addAction:deleteAction];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
{
73 74 75
    [self.hintTimer invalidate];
    self.hintTimer = nil;
    [self animateDeletHintToVisibility:NO];
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
}

- (void)animateDeletHintToVisibility:(BOOL)visible
{
    const NSTimeInterval duration = 0.5;

    UIView *hintView = self.deleteHintView;

    if (hintView.hidden) {
        hintView.alpha = 0.0;
    }

    if (hintView.alpha == 0.0) {
        hintView.hidden = NO;
    }

    const CGFloat targetAlpha = visible ? 1.0 : 0.0;
    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         hintView.alpha = targetAlpha;
                     }
                     completion:^(BOOL finished) {
                         if (hintView.alpha == 0.0) {
                             hintView.hidden = YES;
                         }
                     }];
}

- (void)hintTimerFired:(NSTimer *)timer
{
    const NSTimeInterval waitUntilHideInterval = 5.0;

    NSNumber *userInfo = [timer userInfo];
    BOOL shouldShow = [userInfo isKindOfClass:[NSNumber class]] && [userInfo boolValue];
    [self animateDeletHintToVisibility:shouldShow];
    if (shouldShow) {
        [self.hintTimer invalidate];
        self.hintTimer = [NSTimer scheduledTimerWithTimeInterval:waitUntilHideInterval target:self selector:@selector(hintTimerFired:) userInfo:@(NO) repeats:NO];
    }
}

- (void)startEditMode
{
    self.editing = YES;
}
- (void)endEditMode
{
    self.editing = NO;
}

- (void)setEditing:(BOOL)editing
{
    [super setEditing:editing];

    if (editing) {
        [self.hintTimer invalidate];
        self.hintTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(hintTimerFired:) userInfo:@(YES) repeats:NO];
    } else {
        [self.hintTimer invalidate];
        self.hintTimer = nil;
        [self animateDeletHintToVisibility:NO];
    }

    self.cancelRecognizer.enabled = editing;
    self.playPausePressRecognizer.enabled = editing;
}

@end