VLCGoogleDriveTableViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*****************************************************************************
  2. * VLCGoogleDriveTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. * Soomin Lee <TheHungryBu # gmail.com>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCGoogleDriveTableViewController.h"
  15. #import "VLCAppDelegate.h"
  16. #import "VLCGoogleDriveController.h"
  17. #import "UIDevice+VLC.h"
  18. #import "VLCCloudStorageTableViewCell.h"
  19. #import "VLC-Swift.h"
  20. #import <AppAuth/AppAuth.h>
  21. #import <GTMAppAuth/GTMAppAuth.h>
  22. @interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell>
  23. {
  24. VLCGoogleDriveController *_googleDriveController;
  25. GTLDriveFile *_selectedFile;
  26. }
  27. @end
  28. @implementation VLCGoogleDriveTableViewController
  29. - (void)viewDidLoad
  30. {
  31. [super viewDidLoad];
  32. _googleDriveController = [VLCGoogleDriveController sharedInstance];
  33. _googleDriveController.delegate = self;
  34. self.controller = _googleDriveController;
  35. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
  36. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
  37. [self.cloudStorageLogo sizeToFit];
  38. self.cloudStorageLogo.center = self.view.center;
  39. }
  40. - (void)viewWillAppear:(BOOL)animated
  41. {
  42. [super viewWillAppear:animated];
  43. [self updateViewAfterSessionChange];
  44. }
  45. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  46. {
  47. NSInteger currentOffset = scrollView.contentOffset.y;
  48. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  49. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  50. if (_googleDriveController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  51. [self requestInformationForCurrentPath];
  52. }
  53. }
  54. }
  55. - (void)viewWillDisappear:(BOOL)animated
  56. {
  57. [super viewWillDisappear:animated];
  58. if ((VLCAppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  59. [_googleDriveController stopSession];
  60. [self.tableView reloadData];
  61. }
  62. }
  63. #pragma mark - Table view data source
  64. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. static NSString *CellIdentifier = @"GoogleDriveCell";
  67. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  68. if (cell == nil)
  69. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  70. NSArray *listOfFiles = _googleDriveController.currentListFiles;
  71. NSInteger row = indexPath.row;
  72. if (row < listOfFiles.count) {
  73. cell.driveFile = listOfFiles[row];
  74. if ([cell.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"])
  75. [cell setIsDownloadable:NO];
  76. else
  77. [cell setIsDownloadable:YES];
  78. }
  79. cell.delegate = self;
  80. return cell;
  81. }
  82. #pragma mark - Table view delegate
  83. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  84. {
  85. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  86. if (indexPath.row >= _googleDriveController.currentListFiles.count)
  87. return;
  88. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  89. if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
  90. [_googleDriveController streamFile:_selectedFile];
  91. } else {
  92. /* dive into subdirectory */
  93. if (![self.currentPath isEqualToString:@""])
  94. self.currentPath = [self.currentPath stringByAppendingString:@"/"];
  95. self.currentPath = [self.currentPath stringByAppendingString:_selectedFile.identifier];
  96. [self requestInformationForCurrentPath];
  97. }
  98. }
  99. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  100. {
  101. _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  102. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  103. /* selected item is a proper file, ask the user if s/he wants to download it */
  104. NSArray<VLCAlertButton *> *buttonsAction = @[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_CANCEL", nil)
  105. action: ^(UIAlertAction* action){
  106. self->_selectedFile = nil;
  107. }],
  108. [[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
  109. action: ^(UIAlertAction* action){
  110. [self->_googleDriveController downloadFileToDocumentFolder:self->_selectedFile];
  111. self->_selectedFile = nil;
  112. }]
  113. ];
  114. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  115. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  116. viewController:self
  117. buttonsAction:buttonsAction];
  118. } else {
  119. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  120. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  121. viewController:self];
  122. }
  123. }
  124. #pragma mark - login dialog
  125. - (IBAction)loginAction:(id)sender
  126. {
  127. if (![_googleDriveController isAuthorized]) {
  128. self.authorizationInProgress = YES;
  129. // Build the request with the clientID and scopes.
  130. OIDAuthorizationRequest *request = [[OIDAuthorizationRequest alloc] initWithConfiguration:[GTMAppAuthFetcherAuthorization configurationForGoogle]
  131. clientId:kVLCGoogleDriveClientID
  132. clientSecret:kVLCGoogleDriveClientSecret
  133. scopes:@[OIDScopeOpenID, kGTLAuthScopeDrive]
  134. redirectURL:[NSURL URLWithString:kVLCGoogleRedirectURI]
  135. responseType:OIDResponseTypeCode
  136. additionalParameters:nil];
  137. // Perform the previously built request and saving the current authorization flow.
  138. URLHandlers.googleURLHandler.currentGoogleAuthorizationFlow = [OIDAuthState authStateByPresentingAuthorizationRequest:request presentingViewController:self
  139. callback:^(OIDAuthState *_Nullable authState, NSError *_Nullable error) {
  140. self.authorizationInProgress = NO;
  141. if (authState) {
  142. // Upon successful completion...
  143. self->_googleDriveController.driveService.authorizer = [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
  144. [GTMAppAuthFetcherAuthorization saveAuthorization:(GTMAppAuthFetcherAuthorization *)self->_googleDriveController.driveService.authorizer
  145. toKeychainForName:kKeychainItemName];
  146. [self updateViewAfterSessionChange];
  147. [self.activityIndicator startAnimating];
  148. } else {
  149. self->_googleDriveController.driveService.authorizer = nil;
  150. }
  151. }];
  152. } else {
  153. [_googleDriveController logout];
  154. }
  155. }
  156. @end