VLCGoogleDriveTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // VLCGoogleDriveTableViewController.m
  3. // VLC for iOS
  4. //
  5. // Created by Carola Nitz on 21.09.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCGoogleDriveTableViewController.h"
  11. #import "VLCCloudStorageTableViewCell.h"
  12. #import "VLCGoogleDriveController.h"
  13. #import "VLCAppDelegate.h"
  14. #import "VLCPlaylistViewController.h"
  15. #import "UIBarButtonItem+Theme.h"
  16. #import "VLCGoogleDriveConstants.h"
  17. #import "GTMOAuth2ViewControllerTouch.h"
  18. @interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell>
  19. {
  20. GTLDriveFile *_selectedFile;
  21. GTMOAuth2ViewControllerTouch *_authController;
  22. NSString *_currentPath;
  23. UIBarButtonItem *_backButton;
  24. UIBarButtonItem *_backToMenuButton;
  25. UIBarButtonItem *_numberOfFilesBarButtonItem;
  26. UIBarButtonItem *_progressBarButtonItem;
  27. UIBarButtonItem *_downloadingBarLabel;
  28. UIProgressView *_progressView;
  29. UIActivityIndicatorView *_activityIndicator;
  30. BOOL _authorizationInProgress;
  31. VLCGoogleDriveController *_googleDriveController;
  32. }
  33. @end
  34. @implementation VLCGoogleDriveTableViewController
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. self.modalPresentationStyle = UIModalPresentationFormSheet;
  39. _googleDriveController = [VLCGoogleDriveController sharedInstance];
  40. _googleDriveController.delegate = self;
  41. [_googleDriveController startSession];
  42. _authorizationInProgress = NO;
  43. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
  44. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  45. _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  46. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  47. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  48. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  49. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  50. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  51. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  52. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  53. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  54. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  55. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  56. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  57. _loginToGoogleDriveView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  58. [_loginToGoogleDriveButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  59. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  60. [self _showProgressInToolbar:NO];
  61. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  62. _activityIndicator.hidesWhenStopped = YES;
  63. [self.view addSubview:_activityIndicator];
  64. }
  65. - (GTMOAuth2ViewControllerTouch *)createAuthController
  66. {
  67. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  68. clientID:kVLCGoogleDriveClientID
  69. clientSecret:kVLCGoogleDriveClientSecret
  70. keychainItemName:kKeychainItemName
  71. delegate:self
  72. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  73. return _authController;
  74. }
  75. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  76. {
  77. _authorizationInProgress = NO;
  78. if (error != nil) {
  79. //TODO:Localize
  80. [self showAlert:@"Authentication Error" message:error.localizedDescription];
  81. _googleDriveController.driveService.authorizer = nil;
  82. } else {
  83. _googleDriveController.driveService.authorizer = authResult;
  84. }
  85. [self updateViewAfterSessionChange];
  86. }
  87. - (void)showAlert:(NSString *)title message:(NSString *)message
  88. {
  89. UIAlertView *alert;
  90. alert = [[UIAlertView alloc] initWithTitle: title
  91. message: message
  92. delegate: nil
  93. cancelButtonTitle: @"OK"
  94. otherButtonTitles: nil];
  95. [alert show];
  96. }
  97. - (void)viewWillAppear:(BOOL)animated
  98. {
  99. self.navigationController.toolbarHidden = NO;
  100. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  101. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  102. [self updateViewAfterSessionChange];
  103. [super viewWillAppear:animated];
  104. CGRect aiFrame = _activityIndicator.frame;
  105. CGSize tvSize = self.tableView.frame.size;
  106. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  107. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  108. _activityIndicator.frame = aiFrame;
  109. }
  110. - (void)viewWillDisappear:(BOOL)animated
  111. {
  112. self.navigationController.toolbarHidden = YES;
  113. [super viewWillDisappear:animated];
  114. }
  115. - (void)_showProgressInToolbar:(BOOL)value
  116. {
  117. if (!value)
  118. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  119. else {
  120. _progressView.progress = 0.;
  121. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  122. }
  123. }
  124. - (void)_requestInformationForCurrentPath
  125. {
  126. [_activityIndicator startAnimating];
  127. [_googleDriveController requestDirectoryListingAtPath:_currentPath];
  128. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
  129. }
  130. #pragma mark - interface interaction
  131. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  132. {
  133. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  134. return NO;
  135. return YES;
  136. }
  137. - (IBAction)goBack:(id)sender
  138. {
  139. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  140. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  141. [self _requestInformationForCurrentPath];
  142. } else
  143. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  144. }
  145. #pragma mark - Table view data source
  146. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  147. {
  148. return 1;
  149. }
  150. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  151. {
  152. return _googleDriveController.currentListFiles.count;
  153. }
  154. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. static NSString *CellIdentifier = @"GoogleDriveCell";
  157. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  158. if (cell == nil)
  159. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  160. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  161. cell.delegate = self;
  162. return cell;
  163. }
  164. #pragma mark - Table view delegate
  165. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  166. {
  167. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  168. }
  169. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  170. {
  171. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  172. if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
  173. [_googleDriveController streamFile:_selectedFile];
  174. } else {
  175. /* dive into subdirectory */
  176. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.title];
  177. [self _requestInformationForCurrentPath];
  178. }
  179. _selectedFile = nil;
  180. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  181. }
  182. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  183. {
  184. if (buttonIndex == 1)
  185. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  186. _selectedFile = nil;
  187. }
  188. #pragma mark - table view cell delegation
  189. #pragma mark - VLCLocalNetworkListCell delegation
  190. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  191. {
  192. _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  193. /* selected item is a proper file, ask the user if s/he wants to download it */
  194. 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];
  195. [alert show];
  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.loginToGoogleDriveView.superview)
  227. [self.loginToGoogleDriveView removeFromSuperview];
  228. return;
  229. }
  230. if (![_googleDriveController isAuthorized]) {
  231. [self _showLoginPanel];
  232. return;
  233. } else if (self.loginToGoogleDriveView.superview)
  234. [self.loginToGoogleDriveView removeFromSuperview];
  235. _currentPath = @"/";
  236. [self _requestInformationForCurrentPath];
  237. }
  238. #pragma mark - login dialog
  239. - (void)_showLoginPanel
  240. {
  241. self.loginToGoogleDriveView.frame = self.tableView.frame;
  242. [self.view addSubview:self.loginToGoogleDriveView];
  243. }
  244. - (IBAction)loginToGoogleDriveAction:(id)sender
  245. {
  246. if (![_googleDriveController isAuthorized]) {
  247. _authorizationInProgress = YES;
  248. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  249. } else {
  250. [_googleDriveController logout];
  251. }
  252. }
  253. @end