VLCGoogleDriveTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  62. self.flatLoginButton.hidden = YES;
  63. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  64. } else {
  65. self.loginButton.hidden = YES;
  66. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  67. }
  68. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  69. [self _showProgressInToolbar:NO];
  70. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  71. _activityIndicator.hidesWhenStopped = YES;
  72. [self.view addSubview:_activityIndicator];
  73. }
  74. - (GTMOAuth2ViewControllerTouch *)createAuthController
  75. {
  76. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  77. clientID:kVLCGoogleDriveClientID
  78. clientSecret:kVLCGoogleDriveClientSecret
  79. keychainItemName:kKeychainItemName
  80. delegate:self
  81. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  82. return _authController;
  83. }
  84. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  85. {
  86. _authorizationInProgress = NO;
  87. if (error != nil) {
  88. [self showAlert:NSLocalizedString(@"GDRIVE_AUTHENTICATION_ERROR",nil) message:error.localizedDescription];
  89. _googleDriveController.driveService.authorizer = nil;
  90. } else {
  91. _googleDriveController.driveService.authorizer = authResult;
  92. }
  93. [self updateViewAfterSessionChange];
  94. }
  95. - (void)showAlert:(NSString *)title message:(NSString *)message
  96. {
  97. UIAlertView *alert;
  98. alert = [[UIAlertView alloc] initWithTitle: title
  99. message: message
  100. delegate: nil
  101. cancelButtonTitle: @"OK"
  102. otherButtonTitles: nil];
  103. [alert show];
  104. }
  105. - (void)viewWillAppear:(BOOL)animated
  106. {
  107. self.navigationController.toolbarHidden = NO;
  108. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  109. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  110. [self updateViewAfterSessionChange];
  111. [super viewWillAppear:animated];
  112. CGRect aiFrame = _activityIndicator.frame;
  113. CGSize tvSize = self.tableView.frame.size;
  114. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  115. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  116. _activityIndicator.frame = aiFrame;
  117. [self.cloudStorageLogo sizeToFit];
  118. self.cloudStorageLogo.center = self.view.center;
  119. }
  120. - (void)viewWillDisappear:(BOOL)animated
  121. {
  122. self.navigationController.toolbarHidden = YES;
  123. if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  124. [_googleDriveController stopSession];
  125. [self.tableView reloadData];
  126. }
  127. [super viewWillDisappear:animated];
  128. }
  129. - (void)_showProgressInToolbar:(BOOL)value
  130. {
  131. if (!value)
  132. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  133. else {
  134. _progressView.progress = 0.;
  135. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  136. }
  137. }
  138. - (void)_requestInformationForFiles
  139. {
  140. [_activityIndicator startAnimating];
  141. [_googleDriveController requestFileListing];
  142. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  143. }
  144. #pragma mark - interface interaction
  145. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  146. {
  147. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  148. return NO;
  149. return YES;
  150. }
  151. - (IBAction)goBack:(id)sender
  152. {
  153. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  154. }
  155. #pragma mark - Table view data source
  156. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  157. {
  158. return 1;
  159. }
  160. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  161. {
  162. return _googleDriveController.currentListFiles.count;
  163. }
  164. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  165. {
  166. static NSString *CellIdentifier = @"GoogleDriveCell";
  167. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  168. if (cell == nil)
  169. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  170. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  171. cell.downloadButton.hidden = YES;
  172. cell.delegate = self;
  173. return cell;
  174. }
  175. #pragma mark - Table view delegate
  176. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  177. {
  178. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  179. }
  180. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  181. {
  182. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  183. 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];
  184. [alert show];
  185. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  186. }
  187. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  188. {
  189. NSInteger currentOffset = scrollView.contentOffset.y;
  190. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  191. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  192. if (_googleDriveController.hasMoreFiles && !_activityIndicator.isAnimating) {
  193. [self _requestInformationForFiles];
  194. }
  195. }
  196. }
  197. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  198. {
  199. if (buttonIndex == 1)
  200. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  201. _selectedFile = nil;
  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.loginToCloudStorageView.superview)
  233. [self.loginToCloudStorageView removeFromSuperview];
  234. return;
  235. }
  236. if (![_googleDriveController isAuthorized]) {
  237. [self _showLoginPanel];
  238. return;
  239. } else if (self.loginToCloudStorageView.superview)
  240. [self.loginToCloudStorageView 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.loginToCloudStorageView.frame = self.tableView.frame;
  249. [self.view addSubview:self.loginToCloudStorageView];
  250. }
  251. - (IBAction)loginAction:(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