VLCGoogleDriveTableViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 *_progressBar;
  32. UILabel *_progressLabel;
  33. UIActivityIndicatorView *_activityIndicator;
  34. BOOL _authorizationInProgress;
  35. VLCGoogleDriveController *_googleDriveController;
  36. }
  37. @end
  38. @implementation VLCGoogleDriveTableViewController
  39. - (void)viewDidLoad
  40. {
  41. [super viewDidLoad];
  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. _progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  58. _progressLabel = [[UILabel alloc] init];
  59. _progressLabel.textColor = [UIColor whiteColor];
  60. _progressLabel.font = [UIFont systemFontOfSize:11.];
  61. UIView *progressView = [[UIView alloc] init];
  62. [progressView addSubview:_progressBar];
  63. [progressView addSubview:_progressLabel];
  64. [progressView addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_progressLabel attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]];
  65. progressView.translatesAutoresizingMaskIntoConstraints = NO;
  66. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:progressView];
  67. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
  68. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  69. self.flatLoginButton.hidden = YES;
  70. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  71. } else {
  72. self.loginButton.hidden = YES;
  73. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  74. }
  75. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  76. [self _showProgressInToolbar:NO];
  77. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  78. _activityIndicator.hidesWhenStopped = YES;
  79. [self.view addSubview:_activityIndicator];
  80. }
  81. - (GTMOAuth2ViewControllerTouch *)createAuthController
  82. {
  83. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  84. clientID:kVLCGoogleDriveClientID
  85. clientSecret:kVLCGoogleDriveClientSecret
  86. keychainItemName:kKeychainItemName
  87. delegate:self
  88. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  89. return _authController;
  90. }
  91. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  92. {
  93. _authorizationInProgress = NO;
  94. if (error != nil) {
  95. [self showAlert:NSLocalizedString(@"GDRIVE_AUTHENTICATION_ERROR",nil) message:error.localizedDescription];
  96. _googleDriveController.driveService.authorizer = nil;
  97. } else {
  98. _googleDriveController.driveService.authorizer = authResult;
  99. }
  100. [self updateViewAfterSessionChange];
  101. }
  102. - (void)showAlert:(NSString *)title message:(NSString *)message
  103. {
  104. UIAlertView *alert;
  105. alert = [[UIAlertView alloc] initWithTitle: title
  106. message: message
  107. delegate: nil
  108. cancelButtonTitle: @"OK"
  109. otherButtonTitles: nil];
  110. [alert show];
  111. }
  112. - (void)viewWillAppear:(BOOL)animated
  113. {
  114. self.navigationController.toolbarHidden = NO;
  115. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  116. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  117. [self updateViewAfterSessionChange];
  118. [super viewWillAppear:animated];
  119. CGRect aiFrame = _activityIndicator.frame;
  120. CGSize tvSize = self.tableView.frame.size;
  121. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  122. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  123. _activityIndicator.frame = aiFrame;
  124. [self.cloudStorageLogo sizeToFit];
  125. self.cloudStorageLogo.center = self.view.center;
  126. }
  127. - (void)viewWillDisappear:(BOOL)animated
  128. {
  129. self.navigationController.toolbarHidden = YES;
  130. if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  131. [_googleDriveController stopSession];
  132. [self.tableView reloadData];
  133. }
  134. [super viewWillDisappear:animated];
  135. }
  136. - (void)_showProgressInToolbar:(BOOL)value
  137. {
  138. if (!value)
  139. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  140. else {
  141. _progressBar.progress = 0.;
  142. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  143. }
  144. }
  145. - (void)_requestInformationForFiles
  146. {
  147. [_activityIndicator startAnimating];
  148. [_googleDriveController requestFileListing];
  149. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  150. }
  151. #pragma mark - interface interaction
  152. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  153. {
  154. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  155. return NO;
  156. return YES;
  157. }
  158. - (IBAction)goBack:(id)sender
  159. {
  160. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  161. }
  162. #pragma mark - Table view data source
  163. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  164. {
  165. return 1;
  166. }
  167. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  168. {
  169. return _googleDriveController.currentListFiles.count;
  170. }
  171. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  172. {
  173. static NSString *CellIdentifier = @"GoogleDriveCell";
  174. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  175. if (cell == nil)
  176. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  177. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  178. cell.downloadButton.hidden = YES;
  179. cell.delegate = self;
  180. return cell;
  181. }
  182. #pragma mark - Table view delegate
  183. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  184. {
  185. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  186. }
  187. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  188. {
  189. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  190. 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];
  191. [alert show];
  192. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  193. }
  194. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  195. {
  196. NSInteger currentOffset = scrollView.contentOffset.y;
  197. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  198. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  199. if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
  200. [self _requestInformationForFiles];
  201. }
  202. }
  203. }
  204. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  205. {
  206. if (buttonIndex == 1)
  207. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  208. _selectedFile = nil;
  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)updateRemainingTime:(NSString *)time
  228. {
  229. [_progressLabel setText:[NSString stringWithFormat:NSLocalizedString(@"REMAINING_TIME", nil), time]];
  230. CGSize size = [_progressLabel.text sizeWithFont:_progressLabel.font];
  231. [_progressLabel setFrame:CGRectMake(0, 2, size.width, size.height)];
  232. }
  233. - (void)currentProgressInformation:(float)progress {
  234. [_progressBar 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