VLCGoogleDriveTableViewController.m 12 KB

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