VLCGoogleDriveTableViewController.m 13 KB

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