VLCGoogleDriveTableViewController.m 5.8 KB

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