123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- //
- // VLCGoogleDriveTableViewController.m
- // VLC for iOS
- //
- // Created by Carola Nitz on 21.09.13.
- // Copyright (c) 2013 VideoLAN. All rights reserved.
- //
- // Refer to the COPYING file of the official project for license.
- //
- #import "VLCGoogleDriveTableViewController.h"
- #import "VLCCloudStorageTableViewCell.h"
- #import "VLCGoogleDriveController.h"
- #import "VLCAppDelegate.h"
- #import "VLCPlaylistViewController.h"
- #import "UIBarButtonItem+Theme.h"
- #import "VLCGoogleDriveConstants.h"
- #import "GTMOAuth2ViewControllerTouch.h"
- @interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell>
- {
- GTLDriveFile *_selectedFile;
- GTMOAuth2ViewControllerTouch *_authController;
- NSString *_currentPath;
- UIBarButtonItem *_backButton;
- UIBarButtonItem *_backToMenuButton;
- UIBarButtonItem *_numberOfFilesBarButtonItem;
- UIBarButtonItem *_progressBarButtonItem;
- UIBarButtonItem *_downloadingBarLabel;
- UIProgressView *_progressView;
- UIActivityIndicatorView *_activityIndicator;
- BOOL _authorizationInProgress;
- VLCGoogleDriveController *_googleDriveController;
- }
- @end
- @implementation VLCGoogleDriveTableViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.modalPresentationStyle = UIModalPresentationFormSheet;
- _googleDriveController = [VLCGoogleDriveController sharedInstance];
- _googleDriveController.delegate = self;
- [_googleDriveController startSession];
- _authorizationInProgress = NO;
- 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;
- self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
- 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];
- _loginToGoogleDriveView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
- [_loginToGoogleDriveButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
- [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
- {
- _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
- clientID:kVLCGoogleDriveClientID
- clientSecret:kVLCGoogleDriveClientSecret
- keychainItemName:kKeychainItemName
- delegate:self
- finishedSelector:@selector(viewController:finishedWithAuth:error:)];
- return _authController;
- }
- - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
- {
- _authorizationInProgress = NO;
- if (error != nil) {
- //TODO:Localize
- [self showAlert:@"Authentication Error" message:error.localizedDescription];
- _googleDriveController.driveService.authorizer = nil;
- } else {
- _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;
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- self.navigationController.toolbarHidden = YES;
- [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];
- }
- }
- - (void)_requestInformationForCurrentPath
- {
- [_activityIndicator startAnimating];
- [_googleDriveController requestDirectoryListingAtPath:_currentPath];
- self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
- }
- #pragma mark - interface interaction
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
- {
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
- return NO;
- return YES;
- }
- - (IBAction)goBack:(id)sender
- {
- if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
- _currentPath = [_currentPath stringByDeletingLastPathComponent];
- [self _requestInformationForCurrentPath];
- } else
- [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
- }
- #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";
- VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
- cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
- cell.delegate = self;
- 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
- {
- _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
- if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
- [_googleDriveController streamFile:_selectedFile];
- } else {
- /* dive into subdirectory */
- _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.title];
- [self _requestInformationForCurrentPath];
- }
- _selectedFile = nil;
- [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
- }
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (buttonIndex == 1)
- [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
- _selectedFile = nil;
- }
- #pragma mark - table view cell delegation
- #pragma mark - VLCLocalNetworkListCell delegation
- - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
- {
- _selectedFile = _googleDriveController.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(@"GOOGLEDRIVE_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"GOOGLEDRIVE_DL_LONG", @""), _selectedFile.title, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil];
- [alert show];
- }
- #pragma mark - google drive controller delegate
- - (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
- {
- if(_authorizationInProgress) {
- if (self.loginToGoogleDriveView.superview)
- [self.loginToGoogleDriveView removeFromSuperview];
- return;
- }
- if (![_googleDriveController isAuthorized]) {
- [self _showLoginPanel];
- return;
- } else if (self.loginToGoogleDriveView.superview)
- [self.loginToGoogleDriveView removeFromSuperview];
- _currentPath = @"/";
- [self _requestInformationForCurrentPath];
- }
- #pragma mark - login dialog
- - (void)_showLoginPanel
- {
- self.loginToGoogleDriveView.frame = self.tableView.frame;
- [self.view addSubview:self.loginToGoogleDriveView];
- }
- - (IBAction)loginToGoogleDriveAction:(id)sender
- {
- if (![_googleDriveController isAuthorized]) {
- _authorizationInProgress = YES;
- [self.navigationController pushViewController:[self createAuthController] animated:YES];
- } else {
- [_googleDriveController logout];
- }
- }
- @end
|