VLCDropboxTableViewController.m 10.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*****************************************************************************
 * VLCDropboxTableViewController.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2013 VideoLAN. All rights reserved.
 * $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

#import "VLCDropboxTableViewController.h"
18
#import "VLCCloudStorageTableViewCell.h"
19
20
21
#import "VLCDropboxController.h"
#import "VLCAppDelegate.h"
#import "VLCPlaylistViewController.h"
22
#import "VLCDropboxConstants.h"
23
#import "UIBarButtonItem+Theme.h"
24
25
#import <DropboxSDK/DropboxSDK.h>

26
@interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell>
27
28
29
{
    VLCDropboxController *_dropboxController;
    NSString *_currentPath;
30

31
    UIBarButtonItem *_backButton;
32
    UIBarButtonItem *_backToMenuButton;
33

34
35
    UIBarButtonItem *_numberOfFilesBarButtonItem;
    UIBarButtonItem *_progressBarButtonItem;
36
37
    UIBarButtonItem *_downloadingBarLabel;
    UIProgressView *_progressView;
38
39

    UIActivityIndicatorView *_activityIndicator;
40
    DBMetadata *_selectedFile;
41
42
43
44
45
46
47
48
49
}

@end

@implementation VLCDropboxTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
50
51
    self.modalPresentationStyle = UIModalPresentationFormSheet;

52
53
54
    _dropboxController = [[VLCDropboxController alloc] init];
    _dropboxController.delegate = self;

55
    DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
56
57
58
59
    [DBSession setSharedSession:dbSession];
    [DBRequest setNetworkRequestDelegate:_dropboxController];

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
60
    self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
61

62
    _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
63
    _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
64
    self.navigationItem.leftBarButtonItem = _backToMenuButton;
65

66
    self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
67
68
    self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
    self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
69

70
    _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
71
72
    [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];

73
74
75
76
    _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
    _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
    _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
    [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
77

78
    [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
79
80
    [self.cloudStorageLogo sizeToFit];
    self.cloudStorageLogo.center = self.view.center;
81
    [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
82

Felix Paul Kühne's avatar
Felix Paul Kühne committed
83
84
    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

85
    [self _showProgressInToolbar:NO];
86
87
88
89
90

    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _activityIndicator.hidesWhenStopped = YES;

    [self.view addSubview:_activityIndicator];
91
92
}

93
94
95
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.toolbarHidden = NO;
96
    self.navigationController.toolbar.barStyle = UIBarStyleBlack;
97
    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
98
    [self updateViewAfterSessionChange];
99
    [super viewWillAppear:animated];
100
101
102
103
104
105

    CGRect aiFrame = _activityIndicator.frame;
    CGSize tvSize = self.tableView.frame.size;
    aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
    aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
    _activityIndicator.frame = aiFrame;
106
107
108
109
110
111
112
113
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.toolbarHidden = YES;
    [super viewWillDisappear:animated];
}

114
115
116
117
118
119
120
121
- (void)_showProgressInToolbar:(BOOL)value
{
    if (!value)
        [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
    else {
        _progressView.progress = 0.;
        [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
    }
122
123
}

124
125
126
127
- (void)_requestInformationForCurrentPath
{
    [_activityIndicator startAnimating];
    [_dropboxController requestDirectoryListingAtPath:_currentPath];
128

129
    self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
130
131
}

132
133
#pragma mark - interface interaction

134
- (BOOL)shouldAutorotate
135
{
136
    UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
137
138
139
140
141
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
    return YES;
}

142
- (IBAction)goBack:(id)sender
143
{
144
    if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
145
146
147
        _currentPath = [_currentPath stringByDeletingLastPathComponent];
        [self _requestInformationForCurrentPath];
    } else
148
        [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
149
150
}

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dropboxController.currentListFiles.count;
}

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

167
    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
168
    if (cell == nil)
169
        cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
170
171

    cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
172
    cell.delegate = self;
173
174
175
176
177
178

    return cell;
}

#pragma mark - Table view delegate

179
180
181
182
183
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
}

184
185
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
186
    _selectedFile = _dropboxController.currentListFiles[indexPath.row];
187
188
189
    if (!_selectedFile.isDirectory)
        [_dropboxController streamFile:_selectedFile];
    else {
190
        /* dive into subdirectory */
191
        _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
192
        [self _requestInformationForCurrentPath];
193
    }
194
    _selectedFile = nil;
195
196
197
198

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

199
200
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
201
202
203
204
    if (buttonIndex == 1)
        [_dropboxController downloadFileToDocumentFolder:_selectedFile];

    _selectedFile = nil;
205
206
}

207
208
209
210
#pragma mark - dropbox controller delegate

- (void)mediaListUpdated
{
211
212
    [_activityIndicator stopAnimating];

213
    [self.tableView reloadData];
214
215

    NSUInteger count = _dropboxController.currentListFiles.count;
216
217
218
    if (count == 0)
        _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
    else if (count != 1)
219
220
221
        _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
    else
        _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
222
223
}

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
- (void)operationWithProgressInformationStarted
{
    [self _showProgressInToolbar:YES];
}

- (void)currentProgressInformation:(float)progress
{
    [_progressView setProgress: progress animated:YES];
}

- (void)operationWithProgressInformationStopped
{
    [self _showProgressInToolbar:NO];
}

239
240
241
242
243
244
245
#pragma mark - communication with app delegate

- (void)updateViewAfterSessionChange
{
    if (![[DBSession sharedSession] isLinked]) {
        [self _showLoginPanel];
        return;
246
247
    } else if (self.loginToCloudStorageView.superview)
        [self.loginToCloudStorageView removeFromSuperview];
248
249

    _currentPath = @"/";
250
    [self _requestInformationForCurrentPath];
251
252
253
254
255
256
}

#pragma mark - login dialog

- (void)_showLoginPanel
{
257
258
    self.loginToCloudStorageView.frame = self.tableView.frame;
    [self.view addSubview:self.loginToCloudStorageView];
259
260
}

261
- (IBAction)loginAction:(id)sender
262
263
264
265
266
267
268
{
    if (!_dropboxController.sessionIsLinked)
        [[DBSession sharedSession] linkFromController:self];
    else
        [_dropboxController logout];
}

269
270
271
272
#pragma mark - table view cell delegation


#pragma mark - VLCLocalNetworkListCell delegation
273
- (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
274
275
276
277
278
279
280
281
{
    _selectedFile = _dropboxController.currentListFiles[[self.tableView indexPathForCell:cell].row];

    /* selected item is a proper file, ask the user if s/he wants to download it */
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", @""), _selectedFile.filename, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil];
    [alert show];
}

282
@end