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

#import "VLCGoogleDriveTableViewController.h"
#import "VLCAppDelegate.h"
16
#import "VLCGoogleDriveController.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
17
#import "UIDevice+VLC.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
18
#import "VLCCloudStorageTableViewCell.h"
Carola Nitz's avatar
Carola Nitz committed
19

20 21 22
#import "GTMOAuth2ViewControllerTouch.h"
#import "GTMOAuth2SignIn.h"

23
@interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell>
Carola Nitz's avatar
Carola Nitz committed
24
{
25 26
    VLCGoogleDriveController *_googleDriveController;

27
    GTLDriveFile *_selectedFile;
Carola Nitz's avatar
Carola Nitz committed
28 29 30 31 32 33 34 35 36 37
    GTMOAuth2ViewControllerTouch *_authController;
}

@end

@implementation VLCGoogleDriveTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
38

39
    _googleDriveController = [VLCGoogleDriveController sharedInstance];
Carola Nitz's avatar
Carola Nitz committed
40
    _googleDriveController.delegate = self;
41
    self.controller = _googleDriveController;
42

Carola Nitz's avatar
Carola Nitz committed
43 44
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];

45
    [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
46

47 48 49
    [self.cloudStorageLogo sizeToFit];
    self.cloudStorageLogo.center = self.view.center;
}
Carola Nitz's avatar
Carola Nitz committed
50

51 52 53 54 55
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self updateViewAfterSessionChange];
}
56

57 58 59 60
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger currentOffset = scrollView.contentOffset.y;
    NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
61

62 63
    if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
        if (_googleDriveController.hasMoreFiles && !self.activityIndicator.isAnimating) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
64
            [self requestInformationForCurrentPath];
65 66
        }
    }
Carola Nitz's avatar
Carola Nitz committed
67 68 69 70
}

- (GTMOAuth2ViewControllerTouch *)createAuthController
{
71 72 73 74 75 76 77
    _authController = [GTMOAuth2ViewControllerTouch controllerWithScope:kGTLAuthScopeDrive
                                                               clientID:kVLCGoogleDriveClientID
                                                           clientSecret:kVLCGoogleDriveClientSecret
                                                       keychainItemName:kKeychainItemName
                                                               delegate:self
                                                       finishedSelector:@selector(viewController:finishedWithAuth:error:)];

Carola Nitz's avatar
Carola Nitz committed
78 79 80
    return _authController;
}

81
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
Carola Nitz's avatar
Carola Nitz committed
82
{
83
    self.authorizationInProgress = NO;
84
    if (error != nil) {
85
        APLog(@"%s: error: %@", __PRETTY_FUNCTION__, error);
Carola Nitz's avatar
Carola Nitz committed
86
        _googleDriveController.driveService.authorizer = nil;
87
    } else {
Carola Nitz's avatar
Carola Nitz committed
88 89 90
        _googleDriveController.driveService.authorizer = authResult;
    }
    [self updateViewAfterSessionChange];
91
    [self.activityIndicator startAnimating];
Carola Nitz's avatar
Carola Nitz committed
92 93 94 95
}

- (void)viewWillDisappear:(BOOL)animated
{
96 97
    [super viewWillDisappear:animated];
    if ((VLCAppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
98 99 100
        [_googleDriveController stopSession];
        [self.tableView reloadData];
    }
Carola Nitz's avatar
Carola Nitz committed
101 102 103

}

104 105
#pragma mark - Table view data source

Carola Nitz's avatar
Carola Nitz committed
106 107 108 109
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"GoogleDriveCell";

110
    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Carola Nitz's avatar
Carola Nitz committed
111
    if (cell == nil)
112
        cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
Carola Nitz's avatar
Carola Nitz committed
113

114 115 116 117 118 119 120 121 122
    NSArray *listOfFiles = _googleDriveController.currentListFiles;
    NSInteger row = indexPath.row;
    if (row < listOfFiles.count) {
        cell.driveFile = listOfFiles[row];
        if ([cell.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"])
            [cell setIsDownloadable:NO];
        else
            [cell setIsDownloadable:YES];
    }
123
    cell.delegate = self;
Carola Nitz's avatar
Carola Nitz committed
124 125 126 127 128 129 130 131

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
132 133 134 135 136
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.row >= _googleDriveController.currentListFiles.count)
        return;

137
    _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
138
    if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
Carola Nitz's avatar
Carola Nitz committed
139
        [_googleDriveController streamFile:_selectedFile];
140 141
    } else {
        /* dive into subdirectory */
142 143 144
        if (![self.currentPath isEqualToString:@""])
            self.currentPath = [self.currentPath stringByAppendingString:@"/"];
        self.currentPath = [self.currentPath stringByAppendingString:_selectedFile.identifier];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
145
        [self requestInformationForCurrentPath];
146
    }
Carola Nitz's avatar
Carola Nitz committed
147 148
}

Carola Nitz's avatar
Carola Nitz committed
149 150 151 152
- (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
{
    _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];

153
    if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
154
        /* selected item is a proper file, ask the user if s/he wants to download it */
155
        VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
156
                                                          message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
157 158 159
                                                         delegate:self
                                                cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
                                                otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
160 161
        [alert show];
    } else {
162
        VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
163
                                                          message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
164 165 166
                                                         delegate:self
                                                cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
                                                otherButtonTitles:nil];
167 168
        [alert show];
    }
Carola Nitz's avatar
Carola Nitz committed
169 170
}

Carola Nitz's avatar
Carola Nitz committed
171 172
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
173 174 175
    if (buttonIndex == 1)
        [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
    _selectedFile = nil;
Carola Nitz's avatar
Carola Nitz committed
176 177 178 179
}

#pragma mark - login dialog

180
- (IBAction)loginAction:(id)sender
Carola Nitz's avatar
Carola Nitz committed
181 182
{
    if (![_googleDriveController isAuthorized]) {
183
        self.authorizationInProgress = YES;
Carola Nitz's avatar
Carola Nitz committed
184 185 186 187 188 189 190
        [self.navigationController pushViewController:[self createAuthController] animated:YES];
    } else {
        [_googleDriveController logout];
    }
}

@end