VLCGoogleDriveTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "VLCCloudStorageTableViewCell.h"
  15. #import "VLCAppDelegate.h"
  16. #import "UIBarButtonItem+Theme.h"
  17. #import "VLCProgressView.h"
  18. #import "GTMOAuth2ViewControllerTouch.h"
  19. #import "VLCGoogleDriveController.h"
  20. @interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell, VLCGoogleDriveControllerDelegate>
  21. {
  22. VLCGoogleDriveController *_googleDriveController;
  23. GTLDriveFile *_selectedFile;
  24. GTMOAuth2ViewControllerTouch *_authController;
  25. NSString *_currentFolderId;
  26. UIBarButtonItem *_numberOfFilesBarButtonItem;
  27. UIBarButtonItem *_progressBarButtonItem;
  28. UIBarButtonItem *_logoutButton;
  29. VLCProgressView *_progressView;
  30. UIActivityIndicatorView *_activityIndicator;
  31. BOOL _authorizationInProgress;
  32. }
  33. @end
  34. @implementation VLCGoogleDriveTableViewController
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. self.modalPresentationStyle = UIModalPresentationFormSheet;
  39. _authorizationInProgress = NO;
  40. _googleDriveController = [VLCGoogleDriveController sharedInstance];
  41. _googleDriveController.delegate = self;
  42. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
  43. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  44. UIBarButtonItem *backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack)];
  45. self.navigationItem.leftBarButtonItem = backButton;
  46. _logoutButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_LOGOUT", "") style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];
  47. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  48. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  49. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  50. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  51. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  52. _progressView = [VLCProgressView new];
  53. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  54. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
  55. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  56. self.flatLoginButton.hidden = YES;
  57. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  58. } else {
  59. self.loginButton.hidden = YES;
  60. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  61. }
  62. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  63. [self _showProgressInToolbar:NO];
  64. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  65. _activityIndicator.hidesWhenStopped = YES;
  66. _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  67. [self.view addSubview:_activityIndicator];
  68. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  69. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  70. [self.cloudStorageLogo sizeToFit];
  71. self.cloudStorageLogo.center = self.view.center;
  72. }
  73. - (GTMOAuth2ViewControllerTouch *)createAuthController
  74. {
  75. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  76. clientID:kVLCGoogleDriveClientID
  77. clientSecret:kVLCGoogleDriveClientSecret
  78. keychainItemName:kKeychainItemName
  79. delegate:self
  80. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  81. return _authController;
  82. }
  83. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  84. {
  85. _authorizationInProgress = NO;
  86. if (error != nil) {
  87. _googleDriveController.driveService.authorizer = nil;
  88. } else {
  89. _googleDriveController.driveService.authorizer = authResult;
  90. }
  91. [self updateViewAfterSessionChange];
  92. }
  93. - (void)viewWillAppear:(BOOL)animated
  94. {
  95. self.navigationController.toolbarHidden = NO;
  96. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  97. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  98. [self updateViewAfterSessionChange];
  99. [super viewWillAppear:animated];
  100. }
  101. - (void)viewWillDisappear:(BOOL)animated
  102. {
  103. self.navigationController.toolbarHidden = YES;
  104. if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  105. [_googleDriveController stopSession];
  106. [self.tableView reloadData];
  107. }
  108. [super viewWillDisappear:animated];
  109. }
  110. - (void)_showProgressInToolbar:(BOOL)value
  111. {
  112. if (!value)
  113. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  114. else {
  115. _progressView.progressBar.progress = 0.;
  116. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  117. }
  118. }
  119. - (void)_requestInformationForCurrentFolderId
  120. {
  121. [_activityIndicator startAnimating];
  122. [_googleDriveController requestDirectoryListingWithFolderId:_currentFolderId];
  123. }
  124. - (void)goBack
  125. {
  126. if (![_currentFolderId isEqualToString:@""] && [_currentFolderId length] > 0) {
  127. _currentFolderId = [_currentFolderId stringByDeletingLastPathComponent];
  128. [self _requestInformationForCurrentFolderId];
  129. } else
  130. [self.navigationController popViewControllerAnimated:YES];
  131. }
  132. - (void)logout
  133. {
  134. [_googleDriveController logout];
  135. [self updateViewAfterSessionChange];
  136. }
  137. #pragma mark - Table view data source
  138. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  139. {
  140. return _googleDriveController.currentListFiles.count;
  141. }
  142. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  143. {
  144. static NSString *CellIdentifier = @"GoogleDriveCell";
  145. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  146. if (cell == nil)
  147. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  148. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  149. cell.delegate = self;
  150. return cell;
  151. }
  152. #pragma mark - Table view delegate
  153. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  156. }
  157. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  158. {
  159. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  160. if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
  161. [_googleDriveController streamFile:_selectedFile];
  162. } else {
  163. /* dive into subdirectory */
  164. if (![_currentFolderId isEqualToString:@""])
  165. _currentFolderId = [_currentFolderId stringByAppendingString:@"/"];
  166. _currentFolderId = [_currentFolderId stringByAppendingString:_selectedFile.identifier];
  167. [self _requestInformationForCurrentFolderId];
  168. }
  169. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  170. }
  171. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  172. {
  173. _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  174. /* selected item is a proper file, ask the user if s/he wants to download it */
  175. 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];
  176. [alert show];
  177. }
  178. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  179. {
  180. NSInteger currentOffset = scrollView.contentOffset.y;
  181. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  182. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  183. if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
  184. [self _requestInformationForCurrentFolderId];
  185. }
  186. }
  187. }
  188. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  189. {
  190. if (buttonIndex == 1)
  191. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  192. _selectedFile = nil;
  193. }
  194. #pragma mark - google drive controller delegate
  195. - (void)mediaListUpdated
  196. {
  197. [_activityIndicator stopAnimating];
  198. [self.tableView reloadData];
  199. NSUInteger count = _googleDriveController.currentListFiles.count;
  200. if (count == 0)
  201. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  202. else if (count != 1)
  203. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  204. else
  205. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  206. }
  207. - (void)operationWithProgressInformationStarted
  208. {
  209. [self _showProgressInToolbar:YES];
  210. }
  211. - (void)updateRemainingTime:(NSString *)time
  212. {
  213. [_progressView updateTime:time];
  214. }
  215. - (void)currentProgressInformation:(float)progress {
  216. [_progressView.progressBar setProgress:progress animated:YES];
  217. }
  218. - (void)operationWithProgressInformationStopped
  219. {
  220. [self _showProgressInToolbar:NO];
  221. }
  222. #pragma mark - communication with app delegate
  223. - (void)updateViewAfterSessionChange
  224. {
  225. self.navigationItem.rightBarButtonItem = _logoutButton;
  226. if(_authorizationInProgress) {
  227. if (self.loginToCloudStorageView.superview) {
  228. [self.loginToCloudStorageView removeFromSuperview];
  229. }
  230. return;
  231. }
  232. if (![_googleDriveController isAuthorized]) {
  233. [self _showLoginPanel];
  234. return;
  235. }
  236. //reload if we didn't come back from streaming
  237. _currentFolderId = @"";
  238. if([_googleDriveController.currentListFiles count] == 0)
  239. [self _requestInformationForCurrentFolderId];
  240. }
  241. #pragma mark - login dialog
  242. - (void)_showLoginPanel
  243. {
  244. self.loginToCloudStorageView.frame = self.tableView.frame;
  245. self.navigationItem.rightBarButtonItem = nil;
  246. [self.view addSubview:self.loginToCloudStorageView];
  247. }
  248. - (IBAction)loginAction:(id)sender
  249. {
  250. if (![_googleDriveController isAuthorized]) {
  251. _authorizationInProgress = YES;
  252. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  253. } else {
  254. [_googleDriveController logout];
  255. }
  256. }
  257. @end