VLCGoogleDriveTableViewController.m 13 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 "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 *_progressView;
  32. UIActivityIndicatorView *_activityIndicator;
  33. BOOL _authorizationInProgress;
  34. VLCGoogleDriveController *_googleDriveController;
  35. }
  36. @end
  37. @implementation VLCGoogleDriveTableViewController
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. [[NSBundle mainBundle] loadNibNamed:@"VLCCloudStorageTableViewController" owner:self options:nil];
  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. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  58. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  59. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  60. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  61. self.loginToCloudStorageView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  62. [self _setupLogo];
  63. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  64. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  65. [self _showProgressInToolbar:NO];
  66. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  67. _activityIndicator.hidesWhenStopped = YES;
  68. [self.view addSubview:_activityIndicator];
  69. }
  70. - (void)_setupLogo
  71. {
  72. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"driveWhite"]];
  73. CGRect rect;
  74. rect.size = [UIImage imageNamed:@"driveWhite"].size;
  75. rect.origin.x = (self.loginToCloudStorageView.frame.size.width - rect.size.width) / 2;
  76. rect.origin.y = self.loginButton.frame.origin.y - rect.size.height - 50;
  77. [self.cloudStorageLogo setFrame:CGRectIntegral(rect)];
  78. }
  79. - (GTMOAuth2ViewControllerTouch *)createAuthController
  80. {
  81. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  82. clientID:kVLCGoogleDriveClientID
  83. clientSecret:kVLCGoogleDriveClientSecret
  84. keychainItemName:kKeychainItemName
  85. delegate:self
  86. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  87. return _authController;
  88. }
  89. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  90. {
  91. _authorizationInProgress = NO;
  92. if (error != nil) {
  93. [self showAlert:NSLocalizedString(@"GDRIVE_AUTHENTICATION_ERROR",nil) message:error.localizedDescription];
  94. _googleDriveController.driveService.authorizer = nil;
  95. } else {
  96. _googleDriveController.driveService.authorizer = authResult;
  97. }
  98. [self updateViewAfterSessionChange];
  99. }
  100. - (void)showAlert:(NSString *)title message:(NSString *)message
  101. {
  102. UIAlertView *alert;
  103. alert = [[UIAlertView alloc] initWithTitle: title
  104. message: message
  105. delegate: nil
  106. cancelButtonTitle: @"OK"
  107. otherButtonTitles: nil];
  108. [alert show];
  109. }
  110. - (void)viewWillAppear:(BOOL)animated
  111. {
  112. self.navigationController.toolbarHidden = NO;
  113. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  114. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  115. [self updateViewAfterSessionChange];
  116. [super viewWillAppear:animated];
  117. CGRect aiFrame = _activityIndicator.frame;
  118. CGSize tvSize = self.tableView.frame.size;
  119. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  120. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  121. _activityIndicator.frame = aiFrame;
  122. }
  123. - (void)viewWillDisappear:(BOOL)animated
  124. {
  125. self.navigationController.toolbarHidden = YES;
  126. if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  127. [_googleDriveController stopSession];
  128. [self.tableView reloadData];
  129. }
  130. [super viewWillDisappear:animated];
  131. }
  132. - (void)_showProgressInToolbar:(BOOL)value
  133. {
  134. if (!value)
  135. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  136. else {
  137. _progressView.progress = 0.;
  138. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  139. }
  140. }
  141. - (void)_requestInformationForFiles
  142. {
  143. [_activityIndicator startAnimating];
  144. [_googleDriveController requestFileListing];
  145. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  146. }
  147. #pragma mark - interface interaction
  148. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  149. {
  150. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  151. return NO;
  152. return YES;
  153. }
  154. - (IBAction)goBack:(id)sender
  155. {
  156. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  157. }
  158. #pragma mark - Table view data source
  159. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  160. {
  161. return 1;
  162. }
  163. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  164. {
  165. return _googleDriveController.currentListFiles.count;
  166. }
  167. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  168. {
  169. static NSString *CellIdentifier = @"GoogleDriveCell";
  170. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  171. if (cell == nil)
  172. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  173. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  174. cell.delegate = self;
  175. return cell;
  176. }
  177. #pragma mark - Table view delegate
  178. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  181. }
  182. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  183. {
  184. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  185. [_googleDriveController streamFile:_selectedFile];
  186. _selectedFile = nil;
  187. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  188. }
  189. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  190. {
  191. NSInteger currentOffset = scrollView.contentOffset.y;
  192. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  193. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  194. if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
  195. [self _requestInformationForFiles];
  196. }
  197. }
  198. }
  199. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  200. {
  201. if (buttonIndex == 1)
  202. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  203. _selectedFile = nil;
  204. }
  205. #pragma mark - table view cell delegation
  206. #pragma mark - VLCLocalNetworkListCell delegation
  207. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  208. {
  209. _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  210. /* selected item is a proper file, ask the user if s/he wants to download it */
  211. 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];
  212. [alert show];
  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)currentProgressInformation:(float)progress
  232. {
  233. [_progressView setProgress: progress animated:YES];
  234. }
  235. - (void)operationWithProgressInformationStopped
  236. {
  237. [self _showProgressInToolbar:NO];
  238. }
  239. #pragma mark - communication with app delegate
  240. - (void)updateViewAfterSessionChange
  241. {
  242. if(_authorizationInProgress) {
  243. if (self.loginToCloudStorageView.superview)
  244. [self.loginToCloudStorageView removeFromSuperview];
  245. return;
  246. }
  247. if (![_googleDriveController isAuthorized]) {
  248. [self _showLoginPanel];
  249. return;
  250. } else if (self.loginToCloudStorageView.superview)
  251. [self.loginToCloudStorageView removeFromSuperview];
  252. //reload if we didn't come back from streaming
  253. if([_googleDriveController.currentListFiles count] == 0)
  254. [self _requestInformationForFiles];
  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