VLCGoogleDriveTableViewController.m 15 KB

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