VLCGoogleDriveTableViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. UIBarButtonItem *_backButton;
  27. UIBarButtonItem *_backToMenuButton;
  28. UIBarButtonItem *_numberOfFilesBarButtonItem;
  29. UIBarButtonItem *_progressBarButtonItem;
  30. UIBarButtonItem *_downloadingBarLabel;
  31. UIProgressView *_progressBar;
  32. UILabel *_progressLabel;
  33. UIActivityIndicatorView *_activityIndicator;
  34. BOOL _authorizationInProgress;
  35. VLCGoogleDriveController *_googleDriveController;
  36. }
  37. @end
  38. @implementation VLCGoogleDriveTableViewController
  39. - (void)viewDidLoad
  40. {
  41. [super viewDidLoad];
  42. self.modalPresentationStyle = UIModalPresentationFormSheet;
  43. _googleDriveController = [VLCGoogleDriveController sharedInstance];
  44. _googleDriveController.delegate = self;
  45. [_googleDriveController startSession];
  46. _authorizationInProgress = NO;
  47. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
  48. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  49. _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  50. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  51. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  52. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  53. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  54. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  55. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  56. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  57. _progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  58. _progressLabel = [[UILabel alloc] init];
  59. _progressLabel.textColor = [UIColor whiteColor];
  60. _progressLabel.backgroundColor = [UIColor clearColor];
  61. _progressLabel.font = [UIFont systemFontOfSize:11.];
  62. UIView *progressView = [[UIView alloc] init];
  63. [progressView addSubview:_progressBar];
  64. [progressView addSubview:_progressLabel];
  65. [progressView addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_progressLabel attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]];
  66. [progressView addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_progressLabel attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]];
  67. [progressView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_progressBar]-[_progressLabel]-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(_progressBar, _progressLabel)]];
  68. progressView.translatesAutoresizingMaskIntoConstraints = NO;
  69. _progressLabel.translatesAutoresizingMaskIntoConstraints = NO;
  70. _progressBar.translatesAutoresizingMaskIntoConstraints = NO;
  71. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:progressView];
  72. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
  73. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  74. self.flatLoginButton.hidden = YES;
  75. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  76. } else {
  77. self.loginButton.hidden = YES;
  78. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  79. }
  80. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  81. [self _showProgressInToolbar:NO];
  82. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  83. _activityIndicator.hidesWhenStopped = YES;
  84. [self.view addSubview:_activityIndicator];
  85. }
  86. - (GTMOAuth2ViewControllerTouch *)createAuthController
  87. {
  88. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  89. clientID:kVLCGoogleDriveClientID
  90. clientSecret:kVLCGoogleDriveClientSecret
  91. keychainItemName:kKeychainItemName
  92. delegate:self
  93. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  94. return _authController;
  95. }
  96. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  97. {
  98. _authorizationInProgress = NO;
  99. if (error != nil) {
  100. [self showAlert:NSLocalizedString(@"GDRIVE_AUTHENTICATION_ERROR",nil) message:error.localizedDescription];
  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)_requestInformationForFiles
  151. {
  152. [_activityIndicator startAnimating];
  153. [_googleDriveController requestFileListing];
  154. self.navigationItem.leftBarButtonItem = _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. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  166. }
  167. #pragma mark - Table view data source
  168. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  169. {
  170. return 1;
  171. }
  172. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  173. {
  174. return _googleDriveController.currentListFiles.count;
  175. }
  176. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  177. {
  178. static NSString *CellIdentifier = @"GoogleDriveCell";
  179. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  180. if (cell == nil)
  181. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  182. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  183. cell.downloadButton.hidden = YES;
  184. cell.delegate = self;
  185. return cell;
  186. }
  187. #pragma mark - Table view delegate
  188. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  189. {
  190. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  191. }
  192. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  193. {
  194. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  195. 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];
  196. [alert show];
  197. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  198. }
  199. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  200. {
  201. NSInteger currentOffset = scrollView.contentOffset.y;
  202. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  203. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  204. if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
  205. [self _requestInformationForFiles];
  206. }
  207. }
  208. }
  209. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  210. {
  211. if (buttonIndex == 1)
  212. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  213. _selectedFile = nil;
  214. }
  215. #pragma mark - google drive controller delegate
  216. - (void)mediaListUpdated
  217. {
  218. [_activityIndicator stopAnimating];
  219. [self.tableView reloadData];
  220. NSUInteger count = _googleDriveController.currentListFiles.count;
  221. if (count == 0)
  222. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  223. else if (count != 1)
  224. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  225. else
  226. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  227. }
  228. - (void)operationWithProgressInformationStarted
  229. {
  230. [self _showProgressInToolbar:YES];
  231. }
  232. - (void)updateRemainingTime:(NSString *)time
  233. {
  234. [_progressLabel setText:[NSString stringWithFormat:NSLocalizedString(@"REMAINING_TIME", nil), time]];
  235. CGSize size = [_progressLabel.text sizeWithFont:_progressLabel.font];
  236. [_progressLabel setFrame:CGRectMake(_progressLabel.frame.origin.x, _progressLabel.frame.origin.y, size.width, size.height)];
  237. }
  238. - (void)currentProgressInformation:(float)progress {
  239. [_progressBar setProgress:progress animated:YES];
  240. }
  241. - (void)operationWithProgressInformationStopped
  242. {
  243. [self _showProgressInToolbar:NO];
  244. }
  245. #pragma mark - communication with app delegate
  246. - (void)updateViewAfterSessionChange
  247. {
  248. if(_authorizationInProgress) {
  249. if (self.loginToCloudStorageView.superview)
  250. [self.loginToCloudStorageView removeFromSuperview];
  251. return;
  252. }
  253. if (![_googleDriveController isAuthorized]) {
  254. [self _showLoginPanel];
  255. return;
  256. } else if (self.loginToCloudStorageView.superview)
  257. [self.loginToCloudStorageView removeFromSuperview];
  258. //reload if we didn't come back from streaming
  259. if([_googleDriveController.currentListFiles count] == 0)
  260. [self _requestInformationForFiles];
  261. }
  262. #pragma mark - login dialog
  263. - (void)_showLoginPanel
  264. {
  265. self.loginToCloudStorageView.frame = self.tableView.frame;
  266. [self.view addSubview:self.loginToCloudStorageView];
  267. }
  268. - (IBAction)loginAction:(id)sender
  269. {
  270. if (![_googleDriveController isAuthorized]) {
  271. _authorizationInProgress = YES;
  272. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  273. } else {
  274. [_googleDriveController logout];
  275. }
  276. }
  277. @end