VLCGoogleDriveTableViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCGoogleDriveTableViewController.h"
  14. #import "VLCAppDelegate.h"
  15. #import "GTMOAuth2ViewControllerTouch.h"
  16. #import "VLCGoogleDriveController.h"
  17. #import "UIDevice+VLC.h"
  18. @interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell>
  19. {
  20. VLCGoogleDriveController *_googleDriveController;
  21. GTLDriveFile *_selectedFile;
  22. GTMOAuth2ViewControllerTouch *_authController;
  23. }
  24. @end
  25. @implementation VLCGoogleDriveTableViewController
  26. - (void)viewDidLoad
  27. {
  28. [super viewDidLoad];
  29. _googleDriveController = [VLCGoogleDriveController sharedInstance];
  30. _googleDriveController.delegate = self;
  31. self.controller = _googleDriveController;
  32. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
  33. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
  34. [self.cloudStorageLogo sizeToFit];
  35. self.cloudStorageLogo.center = self.view.center;
  36. }
  37. - (void)viewWillAppear:(BOOL)animated
  38. {
  39. [super viewWillAppear:animated];
  40. [self updateViewAfterSessionChange];
  41. }
  42. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  43. {
  44. NSInteger currentOffset = scrollView.contentOffset.y;
  45. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  46. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  47. if (_googleDriveController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  48. [self _requestInformationForCurrentPath];
  49. }
  50. }
  51. }
  52. - (GTMOAuth2ViewControllerTouch *)createAuthController
  53. {
  54. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  55. clientID:kVLCGoogleDriveClientID
  56. clientSecret:kVLCGoogleDriveClientSecret
  57. keychainItemName:kKeychainItemName
  58. delegate:self
  59. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  60. return _authController;
  61. }
  62. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  63. {
  64. self.authorizationInProgress = NO;
  65. if (error != nil) {
  66. _googleDriveController.driveService.authorizer = nil;
  67. } else {
  68. _googleDriveController.driveService.authorizer = authResult;
  69. }
  70. [self updateViewAfterSessionChange];
  71. }
  72. - (void)viewWillDisappear:(BOOL)animated
  73. {
  74. [super viewWillDisappear:animated];
  75. if ((VLCAppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  76. [_googleDriveController stopSession];
  77. [self.tableView reloadData];
  78. }
  79. }
  80. #pragma mark - Table view data source
  81. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  82. {
  83. static NSString *CellIdentifier = @"GoogleDriveCell";
  84. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  85. if (cell == nil)
  86. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  87. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  88. cell.delegate = self;
  89. return cell;
  90. }
  91. #pragma mark - Table view delegate
  92. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  93. {
  94. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  95. if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
  96. [_googleDriveController streamFile:_selectedFile];
  97. } else {
  98. /* dive into subdirectory */
  99. if (![self.currentPath isEqualToString:@""])
  100. self.currentPath = [self.currentPath stringByAppendingString:@"/"];
  101. self.currentPath = [self.currentPath stringByAppendingString:_selectedFile.identifier];
  102. [self _requestInformationForCurrentPath];
  103. }
  104. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  105. }
  106. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  107. {
  108. _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  109. if (_selectedFile.fileSize.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  110. /* selected item is a proper file, ask the user if s/he wants to download it */
  111. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.title, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  112. [alert show];
  113. } else {
  114. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.title, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  115. [alert show];
  116. }
  117. }
  118. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  119. {
  120. if (buttonIndex == 1)
  121. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  122. _selectedFile = nil;
  123. }
  124. #pragma mark - login dialog
  125. - (IBAction)loginAction:(id)sender
  126. {
  127. if (![_googleDriveController isAuthorized]) {
  128. self.authorizationInProgress = YES;
  129. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  130. } else {
  131. [_googleDriveController logout];
  132. }
  133. }
  134. @end