VLCGoogleDriveTableViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. GTLRDrive_File *_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. if (@available(iOS 11.0, *)) {
  44. self.navigationController.navigationBar.prefersLargeTitles = NO;
  45. }
  46. [self updateViewAfterSessionChange];
  47. }
  48. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  49. {
  50. NSInteger currentOffset = scrollView.contentOffset.y;
  51. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  52. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  53. if (_googleDriveController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  54. [self requestInformationForCurrentPath];
  55. }
  56. }
  57. }
  58. - (void)viewWillDisappear:(BOOL)animated
  59. {
  60. [super viewWillDisappear:animated];
  61. if ((VLCAppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  62. [_googleDriveController stopSession];
  63. [self.tableView reloadData];
  64. }
  65. }
  66. #pragma mark - Table view data source
  67. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  68. {
  69. static NSString *CellIdentifier = @"GoogleDriveCell";
  70. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  71. if (cell == nil)
  72. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  73. NSArray *listOfFiles = _googleDriveController.currentListFiles;
  74. NSInteger row = indexPath.row;
  75. if (row < listOfFiles.count) {
  76. cell.driveFile = listOfFiles[row];
  77. if ([cell.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"])
  78. [cell setIsDownloadable:NO];
  79. else
  80. [cell setIsDownloadable:YES];
  81. }
  82. cell.delegate = self;
  83. return cell;
  84. }
  85. #pragma mark - Table view delegate
  86. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  89. if (indexPath.row >= _googleDriveController.currentListFiles.count)
  90. return;
  91. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  92. if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
  93. [_googleDriveController streamFile:_selectedFile];
  94. } else {
  95. /* dive into subdirectory */
  96. if (![self.currentPath isEqualToString:@""])
  97. self.currentPath = [self.currentPath stringByAppendingString:@"/"];
  98. self.currentPath = [self.currentPath stringByAppendingString:_selectedFile.identifier];
  99. [self requestInformationForCurrentPath];
  100. }
  101. }
  102. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  103. {
  104. _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  105. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  106. /* selected item is a proper file, ask the user if s/he wants to download it */
  107. NSArray<VLCAlertButton *> *buttonsAction = @[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_CANCEL", nil)
  108. style: UIAlertActionStyleCancel
  109. action: ^(UIAlertAction *action) {
  110. self->_selectedFile = nil;
  111. }],
  112. [[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
  113. action: ^(UIAlertAction *action) {
  114. [self->_googleDriveController downloadFileToDocumentFolder:self->_selectedFile];
  115. self->_selectedFile = nil;
  116. }]
  117. ];
  118. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  119. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  120. viewController:self
  121. buttonsAction:buttonsAction];
  122. } else {
  123. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  124. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  125. viewController:self];
  126. }
  127. }
  128. #pragma mark - login dialog
  129. - (IBAction)loginAction:(id)sender
  130. {
  131. if (![_googleDriveController isAuthorized]) {
  132. self.authorizationInProgress = YES;
  133. // Build the request with the clientID and scopes.
  134. OIDAuthorizationRequest *request = [[OIDAuthorizationRequest alloc] initWithConfiguration:[GTMAppAuthFetcherAuthorization configurationForGoogle]
  135. clientId:kVLCGoogleDriveClientID
  136. clientSecret:kVLCGoogleDriveClientSecret
  137. scopes:@[OIDScopeOpenID, kGTLRAuthScopeDrive]
  138. redirectURL:[NSURL URLWithString:kVLCGoogleRedirectURI]
  139. responseType:OIDResponseTypeCode
  140. additionalParameters:nil];
  141. // Perform the previously built request and saving the current authorization flow.
  142. URLHandlers.googleURLHandler.currentGoogleAuthorizationFlow = [OIDAuthState authStateByPresentingAuthorizationRequest:request presentingViewController:self
  143. callback:^(OIDAuthState *_Nullable authState, NSError *_Nullable error) {
  144. self.authorizationInProgress = NO;
  145. if (authState) {
  146. // Upon successful completion...
  147. self->_googleDriveController.driveService.authorizer = [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
  148. [GTMAppAuthFetcherAuthorization saveAuthorization:(GTMAppAuthFetcherAuthorization *)self->_googleDriveController.driveService.authorizer
  149. toKeychainForName:kKeychainItemName];
  150. [self updateViewAfterSessionChange];
  151. [self.activityIndicator startAnimating];
  152. } else {
  153. self->_googleDriveController.driveService.authorizer = nil;
  154. }
  155. }];
  156. } else {
  157. [_googleDriveController logout];
  158. }
  159. }
  160. @end