VLCGoogleDriveTableViewController.m 8.57 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
 * $Id$
 *
 * Authors: Carola Nitz <nitz.carola # googlemail.com>
 *          Felix Paul Kühne <fkuehne # videolan.org>
10
 *          Soomin Lee <TheHungryBu # gmail.com>
11 12 13
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
Carola Nitz's avatar
Carola Nitz committed
14 15 16

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

22 23
#import <AppAuth/AppAuth.h>
#import <GTMAppAuth/GTMAppAuth.h>
24

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

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

@end

@implementation VLCGoogleDriveTableViewController

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

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

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

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

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

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

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

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

- (void)viewWillDisappear:(BOOL)animated
{
72 73
    [super viewWillDisappear:animated];
    if ((VLCAppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
74 75 76
        [_googleDriveController stopSession];
        [self.tableView reloadData];
    }
Carola Nitz's avatar
Carola Nitz committed
77 78 79

}

80 81
#pragma mark - Table view data source

Carola Nitz's avatar
Carola Nitz committed
82 83 84 85
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"GoogleDriveCell";

86
    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Carola Nitz's avatar
Carola Nitz committed
87
    if (cell == nil)
88
        cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
Carola Nitz's avatar
Carola Nitz committed
89

90 91 92 93 94 95 96 97 98
    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];
    }
99
    cell.delegate = self;
Carola Nitz's avatar
Carola Nitz committed
100 101 102 103 104 105 106 107

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
108 109 110 111 112
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

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

113
    _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
114
    if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
Carola Nitz's avatar
Carola Nitz committed
115
        [_googleDriveController streamFile:_selectedFile];
116 117
    } else {
        /* dive into subdirectory */
118 119 120
        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
121
        [self requestInformationForCurrentPath];
122
    }
Carola Nitz's avatar
Carola Nitz committed
123 124
}

Carola Nitz's avatar
Carola Nitz committed
125 126 127 128
- (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
{
    _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];

129
    if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
130
        /* selected item is a proper file, ask the user if s/he wants to download it */
131 132
        NSArray<VLCAlertButton *> *buttonsAction = @[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_CANCEL", nil)
                                                                                  action: ^(UIAlertAction* action){
133
                                                                                      self->_selectedFile = nil;
134 135 136
                                                                                  }],
                                                     [[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
                                                                                  action: ^(UIAlertAction* action){
137 138
                                                                                      [self->_googleDriveController downloadFileToDocumentFolder:self->_selectedFile];
                                                                                      self->_selectedFile = nil;
139 140
                                                                                  }]
                                                     ];
141 142 143 144
        [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
                                             errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
                                           viewController:self
                                            buttonsAction:buttonsAction];
145
    } else {
146 147
        [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
                                             errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
148
                                           viewController:self];
149
    }
Carola Nitz's avatar
Carola Nitz committed
150
}
Carola Nitz's avatar
Carola Nitz committed
151 152
#pragma mark - login dialog

153
- (IBAction)loginAction:(id)sender
Carola Nitz's avatar
Carola Nitz committed
154 155
{
    if (![_googleDriveController isAuthorized]) {
156

157
        self.authorizationInProgress = YES;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

        // Build the request with the clientID and scopes.
        OIDAuthorizationRequest *request = [[OIDAuthorizationRequest alloc] initWithConfiguration:[GTMAppAuthFetcherAuthorization configurationForGoogle]
                                                                                         clientId:kVLCGoogleDriveClientID
                                                                                     clientSecret:kVLCGoogleDriveClientSecret
                                                                                           scopes:@[OIDScopeOpenID, kGTLAuthScopeDrive]
                                                                                      redirectURL:[NSURL URLWithString:kVLCGoogleRedirectURI]
                                                                                     responseType:OIDResponseTypeCode
                                                                             additionalParameters:nil];

        // Perform the previously built request and saving the current authorization flow.
        ((VLCAppDelegate *)[UIApplication sharedApplication].delegate).currentGoogleAuthorizationFlow = [OIDAuthState authStateByPresentingAuthorizationRequest:request presentingViewController:self
           callback:^(OIDAuthState *_Nullable authState, NSError *_Nullable error) {

               self.authorizationInProgress = NO;
               if (authState) {
                   // Upon successful completion...
175 176
                   self->_googleDriveController.driveService.authorizer = [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
                   [GTMAppAuthFetcherAuthorization saveAuthorization:(GTMAppAuthFetcherAuthorization *)self->_googleDriveController.driveService.authorizer
177
                                                   toKeychainForName:kKeychainItemName];
178 179 180
                   [self updateViewAfterSessionChange];
                   [self.activityIndicator startAnimating];
               } else {
181
                   self->_googleDriveController.driveService.authorizer = nil;
182 183 184
               }
           }];

Carola Nitz's avatar
Carola Nitz committed
185 186 187 188 189 190
    } else {
        [_googleDriveController logout];
    }
}

@end