VLCDropboxTableViewController.m 7.07 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCDropboxTableViewController.m
 * VLC for iOS
 *****************************************************************************
5
 * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
6 7 8 9 10 11 12 13 14 15
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *          Gleb Pinigin <gpinigin # gmail.com>
 *          Carola Nitz <nitz.carola # googlemail.com>
 *          Fabio Ritrovato <sephiroth87 # videolan.org>
 *          Tamas Timar <ttimar.vlc # gmail.com>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
16 17 18

#import "VLCDropboxTableViewController.h"
#import "VLCDropboxController.h"
19
#import "VLCCloudStorageTableViewCell.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
20
#import "UIDevice+VLC.h"
21
#import "VLCAppDelegate.h"
22
#import "VLC-Swift.h"
23

24
@interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell, VLCCloudStorageDelegate>
25 26
{
    VLCDropboxController *_dropboxController;
27
    DBFILESMetadata *_selectedFile;
28
    NSArray *_mediaList;
29 30 31 32 33 34
}

@end

@implementation VLCDropboxTableViewController

35 36 37 38 39 40 41 42 43
- (instancetype)initWithPath:(NSString *)path
{
    self = [super init];
    if (self) {
        self.currentPath = path;
    }
    return self;
}

44 45 46
- (void)viewDidLoad
{
    [super viewDidLoad];
47

48
    _dropboxController = [VLCDropboxController sharedInstance];
49 50
    self.controller = _dropboxController;
    self.controller.delegate = self;
51

52
#if TARGET_OS_IOS
53 54

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
55

56
    [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
57 58 59

    [self.cloudStorageLogo sizeToFit];
    self.cloudStorageLogo.center = self.view.center;
60 61
#else
    self.title = @"Dropbox";
62
#endif
63 64
}

65 66 67
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
68

69
    self.controller = [VLCDropboxController sharedInstance];
70 71
    self.controller.delegate = self;

72 73 74
    if (self.currentPath != nil)
        self.title = self.currentPath.lastPathComponent;

75
    [self updateViewAfterSessionChange];
76
    [self.tableView reloadData];
77 78
}

79 80
#pragma mark - interface interaction

81
- (BOOL)shouldAutorotate
82
{
83
    UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
84 85 86 87 88
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
    return YES;
}

89 90 91 92 93 94
#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DropboxCell";

95
    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
96
    if (cell == nil)
97
        cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
98

99 100 101 102 103 104 105
    NSUInteger index = indexPath.row;
    if (_mediaList) {
        if (index < _mediaList.count) {
            cell.dropboxFile = _mediaList[index];
            cell.delegate = self;
        }
    }
106 107 108 109

    return cell;
}

110 111 112
- (void)mediaListUpdated
{
    _mediaList = [self.controller.currentListFiles copy];
113 114

    [super mediaListUpdated];
115 116
}

117 118 119 120
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
121
    _selectedFile = _mediaList[indexPath.row];
122
    if (![_selectedFile isKindOfClass:[DBFILESFolderMetadata class]])
123
        [_dropboxController streamFile:_selectedFile currentNavigationController:self.navigationController];
124
    else {
125
        /* dive into subdirectory */
126
        NSString *futurePath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.name];
127
        self.currentPath = futurePath;
128
        [self requestInformationForCurrentPath];
129
    }
130
    _selectedFile = nil;
131 132 133 134 135

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - login dialog
136

137
- (IBAction)loginAction:(id)sender
138
{
139 140
    if (!_dropboxController.isAuthorized) {
        self.authorizationInProgress = YES;
141 142 143 144 145 146

        [DBClientsManager authorizeFromController:[UIApplication sharedApplication]
                                       controller:self
                                          openURL:^(NSURL *url) {
                                              [[UIApplication sharedApplication] openURL:url];
                                          }];
147
    } else
148 149 150
        [_dropboxController logout];
}

151 152 153 154
- (void)sessionWasUpdated:(NSNotification *)aNotification
{
    self.authorizationInProgress = YES;
    [self updateViewAfterSessionChange];
155 156

    [_dropboxController shareCredentials];
157
}
158

159
#pragma mark - VLCCloudStorageTableViewCell delegation
Felix Paul Kühne's avatar
Felix Paul Kühne committed
160

161
#if TARGET_OS_IOS
162
- (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
163
{
164
    _selectedFile = _mediaList[[self.tableView indexPathForCell:cell].row];
165

166
    if (((DBFILESFileMetadata *)_selectedFile).size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
167
        /* selected item is a proper file, ask the user if s/he wants to download it */
168
        NSArray<VLCAlertButton *> *buttonsAction = @[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_CANCEL", nil)
169
                                                                                     style: UIAlertActionStyleCancel
170
                                                                                    action: ^(UIAlertAction *action) {
171 172 173
                                                                                        self->_selectedFile = nil;
                                                                                    }],
                                                     [[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
174
                                                                                    action: ^(UIAlertAction *action) {
175 176 177
                                                                                        [self->_dropboxController downloadFileToDocumentFolder:self->_selectedFile];
                                                                                        self->_selectedFile = nil;
                                                                                    }]
178
                                                     ];
179 180 181 182 183
        [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
                                             errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
                                           viewController:self
                                            buttonsAction:buttonsAction];

184
    } else {
185 186
        [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
                                             errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
187
                                           viewController:self];
188
    }
189 190
}

191 192
#endif

193
@end