VLCGoogleDriveTableViewController.m 13 KB

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