VLCGoogleDriveTableViewController.m 13 KB

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