VLCGoogleDriveTableViewController.m 14 KB

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