VLCGoogleDriveTableViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. _googleDriveController.driveService.authorizer = nil;
  101. } else {
  102. _googleDriveController.driveService.authorizer = authResult;
  103. }
  104. [self updateViewAfterSessionChange];
  105. }
  106. - (void)showAlert:(NSString *)title message:(NSString *)message
  107. {
  108. UIAlertView *alert;
  109. alert = [[UIAlertView alloc] initWithTitle: title
  110. message: message
  111. delegate: nil
  112. cancelButtonTitle: @"OK"
  113. otherButtonTitles: nil];
  114. [alert show];
  115. }
  116. - (void)viewWillAppear:(BOOL)animated
  117. {
  118. self.navigationController.toolbarHidden = NO;
  119. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  120. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  121. [self updateViewAfterSessionChange];
  122. [super viewWillAppear:animated];
  123. CGRect aiFrame = _activityIndicator.frame;
  124. CGSize tvSize = self.tableView.frame.size;
  125. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  126. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  127. _activityIndicator.frame = aiFrame;
  128. [self.cloudStorageLogo sizeToFit];
  129. self.cloudStorageLogo.center = self.view.center;
  130. }
  131. - (void)viewWillDisappear:(BOOL)animated
  132. {
  133. self.navigationController.toolbarHidden = YES;
  134. if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  135. [_googleDriveController stopSession];
  136. [self.tableView reloadData];
  137. }
  138. [super viewWillDisappear:animated];
  139. }
  140. - (void)_showProgressInToolbar:(BOOL)value
  141. {
  142. if (!value)
  143. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  144. else {
  145. _progressBar.progress = 0.;
  146. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  147. }
  148. }
  149. - (void)_requestInformationForFiles
  150. {
  151. [_activityIndicator startAnimating];
  152. [_googleDriveController requestFileListing];
  153. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  154. }
  155. #pragma mark - interface interaction
  156. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  157. {
  158. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  159. return NO;
  160. return YES;
  161. }
  162. - (IBAction)goBack:(id)sender
  163. {
  164. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  165. }
  166. #pragma mark - Table view data source
  167. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  168. {
  169. return 1;
  170. }
  171. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  172. {
  173. return _googleDriveController.currentListFiles.count;
  174. }
  175. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  176. {
  177. static NSString *CellIdentifier = @"GoogleDriveCell";
  178. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  179. if (cell == nil)
  180. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  181. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  182. cell.downloadButton.hidden = YES;
  183. cell.delegate = self;
  184. return cell;
  185. }
  186. #pragma mark - Table view delegate
  187. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  188. {
  189. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  190. }
  191. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  192. {
  193. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  194. 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];
  195. [alert show];
  196. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  197. }
  198. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  199. {
  200. NSInteger currentOffset = scrollView.contentOffset.y;
  201. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  202. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  203. if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
  204. [self _requestInformationForFiles];
  205. }
  206. }
  207. }
  208. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  209. {
  210. if (buttonIndex == 1)
  211. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  212. _selectedFile = nil;
  213. }
  214. #pragma mark - google drive controller delegate
  215. - (void)mediaListUpdated
  216. {
  217. [_activityIndicator stopAnimating];
  218. [self.tableView reloadData];
  219. NSUInteger count = _googleDriveController.currentListFiles.count;
  220. if (count == 0)
  221. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  222. else if (count != 1)
  223. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  224. else
  225. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  226. }
  227. - (void)operationWithProgressInformationStarted
  228. {
  229. [self _showProgressInToolbar:YES];
  230. }
  231. - (void)updateRemainingTime:(NSString *)time
  232. {
  233. [_progressLabel setText:[NSString stringWithFormat:NSLocalizedString(@"REMAINING_TIME", nil), time]];
  234. CGSize size = [_progressLabel.text sizeWithFont:_progressLabel.font];
  235. [_progressLabel setFrame:CGRectMake(_progressLabel.frame.origin.x, _progressLabel.frame.origin.y, size.width, size.height)];
  236. }
  237. - (void)currentProgressInformation:(float)progress {
  238. [_progressBar setProgress:progress animated:YES];
  239. }
  240. - (void)operationWithProgressInformationStopped
  241. {
  242. [self _showProgressInToolbar:NO];
  243. }
  244. #pragma mark - communication with app delegate
  245. - (void)updateViewAfterSessionChange
  246. {
  247. if(_authorizationInProgress) {
  248. if (self.loginToCloudStorageView.superview)
  249. [self.loginToCloudStorageView removeFromSuperview];
  250. return;
  251. }
  252. if (![_googleDriveController isAuthorized]) {
  253. [self _showLoginPanel];
  254. return;
  255. } else if (self.loginToCloudStorageView.superview)
  256. [self.loginToCloudStorageView removeFromSuperview];
  257. //reload if we didn't come back from streaming
  258. if([_googleDriveController.currentListFiles count] == 0)
  259. [self _requestInformationForFiles];
  260. }
  261. #pragma mark - login dialog
  262. - (void)_showLoginPanel
  263. {
  264. self.loginToCloudStorageView.frame = self.tableView.frame;
  265. [self.view addSubview:self.loginToCloudStorageView];
  266. }
  267. - (IBAction)loginAction:(id)sender
  268. {
  269. if (![_googleDriveController isAuthorized]) {
  270. _authorizationInProgress = YES;
  271. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  272. } else {
  273. [_googleDriveController logout];
  274. }
  275. }
  276. @end