VLCGoogleDriveTableViewController.m 14 KB

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