VLCGoogleDriveTableViewController.m 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*****************************************************************************
 * VLCGoogleDriveTableViewController.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2013 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Carola Nitz <nitz.carola # googlemail.com>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
Carola Nitz's avatar
Carola Nitz committed
13 14

#import "VLCGoogleDriveTableViewController.h"
15
#import "VLCCloudStorageTableViewCell.h"
Carola Nitz's avatar
Carola Nitz committed
16 17 18 19 20 21
#import "VLCGoogleDriveController.h"
#import "VLCAppDelegate.h"
#import "VLCPlaylistViewController.h"
#import "UIBarButtonItem+Theme.h"
#import "VLCGoogleDriveConstants.h"
#import "GTMOAuth2ViewControllerTouch.h"
22
#import "VLCGoogleDriveController.h"
Carola Nitz's avatar
Carola Nitz committed
23

24
@interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell>
Carola Nitz's avatar
Carola Nitz committed
25
{
26
    GTLDriveFile *_selectedFile;
Carola Nitz's avatar
Carola Nitz committed
27
    GTMOAuth2ViewControllerTouch *_authController;
28

Carola Nitz's avatar
Carola Nitz committed
29 30 31 32 33 34 35 36 37
    UIBarButtonItem *_backButton;
    UIBarButtonItem *_backToMenuButton;

    UIBarButtonItem *_numberOfFilesBarButtonItem;
    UIBarButtonItem *_progressBarButtonItem;
    UIBarButtonItem *_downloadingBarLabel;
    UIProgressView *_progressView;

    UIActivityIndicatorView *_activityIndicator;
38

39
    BOOL _authorizationInProgress;
40
    VLCGoogleDriveController *_googleDriveController;
Carola Nitz's avatar
Carola Nitz committed
41 42 43 44 45 46 47 48 49 50 51
}

@end

@implementation VLCGoogleDriveTableViewController

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

52
    _googleDriveController = [VLCGoogleDriveController sharedInstance];
Carola Nitz's avatar
Carola Nitz committed
53 54
    _googleDriveController.delegate = self;
    [_googleDriveController startSession];
55 56 57

    _authorizationInProgress = NO;

Carola Nitz's avatar
Carola Nitz committed
58 59 60 61 62 63 64
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
    self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;

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

65
    self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
Carola Nitz's avatar
Carola Nitz committed
66 67 68 69 70 71 72 73 74 75 76
    self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
    self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];

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

    _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
    [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
78

79 80 81 82 83 84 85
    if (!SYSTEM_RUNS_IOS7_OR_LATER) {
        self.flatLoginButton.hidden = YES;
        [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
    } else {
        self.loginButton.hidden = YES;
        [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
    }
Carola Nitz's avatar
Carola Nitz committed
86 87 88 89 90 91 92 93 94 95 96 97 98

    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

    [self _showProgressInToolbar:NO];

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

    [self.view addSubview:_activityIndicator];
}

- (GTMOAuth2ViewControllerTouch *)createAuthController
{
99
    _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
Carola Nitz's avatar
Carola Nitz committed
100 101 102 103 104 105 106 107
                                                                clientID:kVLCGoogleDriveClientID
                                                            clientSecret:kVLCGoogleDriveClientSecret
                                                        keychainItemName:kKeychainItemName
                                                                delegate:self
                                                        finishedSelector:@selector(viewController:finishedWithAuth:error:)];
    return _authController;
}

108
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
Carola Nitz's avatar
Carola Nitz committed
109
{
110
    _authorizationInProgress = NO;
111
    if (error != nil) {
112
        [self showAlert:NSLocalizedString(@"GDRIVE_AUTHENTICATION_ERROR",nil) message:error.localizedDescription];
Carola Nitz's avatar
Carola Nitz committed
113
        _googleDriveController.driveService.authorizer = nil;
114
    } else {
Carola Nitz's avatar
Carola Nitz committed
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
        _googleDriveController.driveService.authorizer = authResult;
    }
    [self updateViewAfterSessionChange];
}

- (void)showAlert:(NSString *)title message:(NSString *)message
{
    UIAlertView *alert;
    alert = [[UIAlertView alloc] initWithTitle: title
                                       message: message
                                      delegate: nil
                             cancelButtonTitle: @"OK"
                             otherButtonTitles: nil];
    [alert show];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.toolbarHidden = NO;
    self.navigationController.toolbar.barStyle = UIBarStyleBlack;
    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    [self updateViewAfterSessionChange];
    [super viewWillAppear:animated];

    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;
144 145 146

    [self.cloudStorageLogo sizeToFit];
    self.cloudStorageLogo.center = self.view.center;
Carola Nitz's avatar
Carola Nitz committed
147 148 149 150 151
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.toolbarHidden = YES;
152 153 154 155
    if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
        [_googleDriveController stopSession];
        [self.tableView reloadData];
    }
Carola Nitz's avatar
Carola Nitz committed
156 157 158 159 160 161 162 163 164 165 166 167 168
    [super viewWillDisappear:animated];
}

- (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];
    }
}

169
- (void)_requestInformationForFiles
Carola Nitz's avatar
Carola Nitz committed
170 171
{
    [_activityIndicator startAnimating];
172
    [_googleDriveController requestFileListing];
Carola Nitz's avatar
Carola Nitz committed
173

174
    self.navigationItem.leftBarButtonItem = _backToMenuButton;
Carola Nitz's avatar
Carola Nitz committed
175 176 177 178 179 180 181 182 183 184 185 186 187
}

#pragma mark - interface interaction

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
    return YES;
}

- (IBAction)goBack:(id)sender
{
188
    [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
Carola Nitz's avatar
Carola Nitz committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

#pragma mark - Table view data source

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

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

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

207
    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Carola Nitz's avatar
Carola Nitz committed
208
    if (cell == nil)
209
        cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
Carola Nitz's avatar
Carola Nitz committed
210

211
    cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
212
    cell.downloadButton.hidden = YES;
213
    cell.delegate = self;
Carola Nitz's avatar
Carola Nitz committed
214 215 216 217 218 219 220 221 222 223 224 225 226

    return cell;
}

#pragma mark - Table view delegate

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
227
    _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
228 229
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", @""), _selectedFile.title, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil];
    [alert show];
Carola Nitz's avatar
Carola Nitz committed
230 231 232
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

233 234 235 236 237 238 239
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger currentOffset = scrollView.contentOffset.y;
    NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

    if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
        if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
240
            [self _requestInformationForFiles];
241 242 243 244
        }
    }
}

Carola Nitz's avatar
Carola Nitz committed
245 246
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
247 248
    if (buttonIndex == 1)
        [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
Carola Nitz's avatar
Carola Nitz committed
249

250
    _selectedFile = nil;
Carola Nitz's avatar
Carola Nitz committed
251 252
}

253
#pragma mark - google drive controller delegate
Carola Nitz's avatar
Carola Nitz committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

- (void)mediaListUpdated
{
    [_activityIndicator stopAnimating];

    [self.tableView reloadData];

    NSUInteger count = _googleDriveController.currentListFiles.count;
    if (count == 0)
        _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
    else if (count != 1)
        _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
    else
        _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
}

- (void)operationWithProgressInformationStarted
{
    [self _showProgressInToolbar:YES];
}

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

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

#pragma mark - communication with app delegate

- (void)updateViewAfterSessionChange
{
289
    if(_authorizationInProgress) {
290 291
        if (self.loginToCloudStorageView.superview)
            [self.loginToCloudStorageView removeFromSuperview];
292 293
        return;
    }
Carola Nitz's avatar
Carola Nitz committed
294 295 296
    if (![_googleDriveController isAuthorized]) {
        [self _showLoginPanel];
        return;
297 298
    } else if (self.loginToCloudStorageView.superview)
        [self.loginToCloudStorageView removeFromSuperview];
299

300 301 302
    //reload if we didn't come back from streaming
    if([_googleDriveController.currentListFiles count] == 0)
        [self _requestInformationForFiles];
Carola Nitz's avatar
Carola Nitz committed
303 304 305 306 307 308
}

#pragma mark - login dialog

- (void)_showLoginPanel
{
309 310
    self.loginToCloudStorageView.frame = self.tableView.frame;
    [self.view addSubview:self.loginToCloudStorageView];
Carola Nitz's avatar
Carola Nitz committed
311 312
}

313
- (IBAction)loginAction:(id)sender
Carola Nitz's avatar
Carola Nitz committed
314 315
{
    if (![_googleDriveController isAuthorized]) {
316
        _authorizationInProgress = YES;
Carola Nitz's avatar
Carola Nitz committed
317 318 319 320 321 322 323
        [self.navigationController pushViewController:[self createAuthController] animated:YES];
    } else {
        [_googleDriveController logout];
    }
}

@end