VLCGoogleDriveTableViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. @interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell>
  22. {
  23. GTLDriveFile *_selectedFile;
  24. GTMOAuth2ViewControllerTouch *_authController;
  25. NSString *_currentPath;
  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. self.modalPresentationStyle = UIModalPresentationFormSheet;
  42. _googleDriveController = [VLCGoogleDriveController sharedInstance];
  43. _googleDriveController.delegate = self;
  44. [_googleDriveController startSession];
  45. _authorizationInProgress = NO;
  46. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
  47. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  48. _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  49. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  50. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  51. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  52. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  53. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  54. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  55. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  56. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  57. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  58. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  59. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  60. _loginToGoogleDriveView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  61. [_loginToGoogleDriveButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  62. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  63. [self _showProgressInToolbar:NO];
  64. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  65. _activityIndicator.hidesWhenStopped = YES;
  66. [self.view addSubview:_activityIndicator];
  67. }
  68. - (GTMOAuth2ViewControllerTouch *)createAuthController
  69. {
  70. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  71. clientID:kVLCGoogleDriveClientID
  72. clientSecret:kVLCGoogleDriveClientSecret
  73. keychainItemName:kKeychainItemName
  74. delegate:self
  75. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  76. return _authController;
  77. }
  78. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  79. {
  80. _authorizationInProgress = NO;
  81. if (error != nil) {
  82. //TODO:Localize
  83. [self showAlert:@"Authentication Error" message:error.localizedDescription];
  84. _googleDriveController.driveService.authorizer = nil;
  85. } else {
  86. _googleDriveController.driveService.authorizer = authResult;
  87. }
  88. [self updateViewAfterSessionChange];
  89. }
  90. - (void)showAlert:(NSString *)title message:(NSString *)message
  91. {
  92. UIAlertView *alert;
  93. alert = [[UIAlertView alloc] initWithTitle: title
  94. message: message
  95. delegate: nil
  96. cancelButtonTitle: @"OK"
  97. otherButtonTitles: nil];
  98. [alert show];
  99. }
  100. - (void)viewWillAppear:(BOOL)animated
  101. {
  102. self.navigationController.toolbarHidden = NO;
  103. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  104. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  105. [self updateViewAfterSessionChange];
  106. [super viewWillAppear:animated];
  107. CGRect aiFrame = _activityIndicator.frame;
  108. CGSize tvSize = self.tableView.frame.size;
  109. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  110. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  111. _activityIndicator.frame = aiFrame;
  112. }
  113. - (void)viewWillDisappear:(BOOL)animated
  114. {
  115. self.navigationController.toolbarHidden = YES;
  116. [super viewWillDisappear:animated];
  117. }
  118. - (void)_showProgressInToolbar:(BOOL)value
  119. {
  120. if (!value)
  121. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  122. else {
  123. _progressView.progress = 0.;
  124. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  125. }
  126. }
  127. - (void)_requestInformationForCurrentPath
  128. {
  129. [_activityIndicator startAnimating];
  130. [_googleDriveController requestDirectoryListingAtPath:_currentPath];
  131. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
  132. }
  133. #pragma mark - interface interaction
  134. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  135. {
  136. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  137. return NO;
  138. return YES;
  139. }
  140. - (IBAction)goBack:(id)sender
  141. {
  142. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  143. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  144. [self _requestInformationForCurrentPath];
  145. } else
  146. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  147. }
  148. #pragma mark - Table view data source
  149. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  150. {
  151. return 1;
  152. }
  153. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  154. {
  155. return _googleDriveController.currentListFiles.count;
  156. }
  157. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  158. {
  159. static NSString *CellIdentifier = @"GoogleDriveCell";
  160. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  161. if (cell == nil)
  162. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  163. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  164. cell.delegate = self;
  165. return cell;
  166. }
  167. #pragma mark - Table view delegate
  168. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  169. {
  170. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  171. }
  172. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  175. if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
  176. [_googleDriveController streamFile:_selectedFile];
  177. } else {
  178. /* dive into subdirectory */
  179. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.title];
  180. [self _requestInformationForCurrentPath];
  181. }
  182. _selectedFile = nil;
  183. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  184. }
  185. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  186. {
  187. NSInteger currentOffset = scrollView.contentOffset.y;
  188. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  189. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  190. if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
  191. [self _requestInformationForCurrentPath];
  192. }
  193. }
  194. }
  195. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  196. {
  197. if (buttonIndex == 1)
  198. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  199. _selectedFile = nil;
  200. }
  201. #pragma mark - table view cell delegation
  202. #pragma mark - VLCLocalNetworkListCell delegation
  203. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  204. {
  205. _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  206. /* selected item is a proper file, ask the user if s/he wants to download it */
  207. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"GOOGLEDRIVE_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"GOOGLEDRIVE_DL_LONG", @""), _selectedFile.title, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil];
  208. [alert show];
  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)currentProgressInformation:(float)progress
  228. {
  229. [_progressView setProgress: progress animated:YES];
  230. }
  231. - (void)operationWithProgressInformationStopped
  232. {
  233. [self _showProgressInToolbar:NO];
  234. }
  235. #pragma mark - communication with app delegate
  236. - (void)updateViewAfterSessionChange
  237. {
  238. if(_authorizationInProgress) {
  239. if (self.loginToGoogleDriveView.superview)
  240. [self.loginToGoogleDriveView removeFromSuperview];
  241. return;
  242. }
  243. if (![_googleDriveController isAuthorized]) {
  244. [self _showLoginPanel];
  245. return;
  246. } else if (self.loginToGoogleDriveView.superview)
  247. [self.loginToGoogleDriveView removeFromSuperview];
  248. _currentPath = @"/";
  249. [self _requestInformationForCurrentPath];
  250. }
  251. #pragma mark - login dialog
  252. - (void)_showLoginPanel
  253. {
  254. self.loginToGoogleDriveView.frame = self.tableView.frame;
  255. [self.view addSubview:self.loginToGoogleDriveView];
  256. }
  257. - (IBAction)loginToGoogleDriveAction:(id)sender
  258. {
  259. if (![_googleDriveController isAuthorized]) {
  260. _authorizationInProgress = YES;
  261. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  262. } else {
  263. [_googleDriveController logout];
  264. }
  265. }
  266. @end